打磚塊的源代碼

    磚塊生成腳本ide

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Brick : MonoBehaviour
{

    // Use this for initialization
    public GameObject brick;
    private int columnNum = 8;
    private int rowNum = 6;
    void Start () {
        for(int i =0 ;i < rowNum ; i++)
        {
            for (int j = 0;j < columnNum ; j++)
            {
                Instantiate(brick,new Vector3(j - 5,i),Quaternion.identity);
            }
        }
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}


 

小球射擊腳本this

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shoot : MonoBehaviour
{

    // Use this for initialization
    public GameObject ShootPos;
    private float force = 1000;
    public Rigidbody ShootBall;
    private float speed = 0.1f;
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        Rigidbody ball;
        if (Input.GetKeyDown(KeyCode.Space))
        {
            ball = Instantiate(ShootBall, ShootPos.transform.position, Quaternion.identity) as Rigidbody;
            ball.AddForce(force * ball.transform.forward);
        }
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            this.transform.Translate(Vector3.left * speed);
        }
        else if (Input.GetKey(KeyCode.RightArrow))
        {
            this.transform.Translate(Vector3.right * speed);
        }
        else if (Input.GetKey(KeyCode.UpArrow))
        {
            this.transform.Translate(Vector3.up * speed);
        }
        else if (Input.GetKey(KeyCode.DownArrow))
        {
            this.transform.Translate(Vector3.down * speed);
        }
    }
}

小球消失腳本code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BallDestory : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        Destroy(this.gameObject, 3f);
    }
}
相關文章
相關標籤/搜索