小球打磚塊

生成磚塊ide

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

public class Brick : MonoBehaviour {
	public GameObject brick;
	private int columnNum = 8 ;
	private int rowNum = 6;
	// Use this for initialization
	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 {
	public GameObject shootPos;
	private float force=1000;
	public Rigidbody shootBall;
	private float speed=0.1f;

	// Use this for initialization
	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);
	}
}
相關文章
相關標籤/搜索