分别创建CameraMove,CreatWall,ZiDan三个脚本;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMove : MonoBehaviour
{
public GameObject other;
public Rigidbody rb;
public float speed;
public float Thrust;
// Start is called before the first frame update
void Start()
{
rb = other.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
transform.Translate(new Vector3(h, v, 0) * Time.deltaTime * speed);
if(Input.GetButtonDown("Fire1"))
{
Rigidbody ziDan = GameObject.Instantiate(rb, transform.position, transform.rotation);
ziDan.AddForce(transform.forward * Thrust);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CreatWall : MonoBehaviour
{
public GameObject cube;
public int x = 9;
public int y = 5;
// Start is called before the first frame update
void Start()
{
CreatBrick();
}
// Update is called once per frame
void Update()
{
}
void CreatBrick()
{
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
GameObject.Instantiate(cube, new Vector3(i - 4, j, 0), Quaternion.identity);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ZiDan : MonoBehaviour
{
public GameObject baoZha;
private void OnCollisionEnter(Collision collision)
{
GameObject baoZhaZhong = GameObject.Instantiate(baoZha, transform.position, transform.rotation);
//播放声音
gameObject.GetComponent<AudioSource>().Play();
gameObject.GetComponent<MeshRenderer>().enabled = false;
gameObject.GetComponent<SphereCollider>().enabled = false;
GameObject.Destroy(baoZhaZhong, 3.0f);
GameObject.Destroy(this.gameObject);
}
}