打砖块
< 返回列表时间: 2020-03-21来源:OSCHINA
创建一个Plane

添加一个空对象绑定脚本用来创建墙
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 () { } }
创建一个球体绑定rigidbody组建并建立脚本使它在发射后的三秒内消失将这个球体作为预制件 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Balldestroy : MonoBehaviour { // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { Destroy(gameObject, 3f); } }
建立脚本绑定在摄像机上使它可以移动并发射刚刚制作的球体 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); } } }
热门排行