主要介绍:通过实例化物体生成一面墙,在摄像机处朝墙的方向发射子弹,产生碰撞。
赋予摄像机的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bulletCube : MonoBehaviour
{
public GameObject cube;
public GameObject bullet;
private RaycastHit hit;
private Ray ray;
// Use this for initialization
void Start () {
for (int i = -5; i < 5; i++)
{
for (int j = 0; j < 7; j++)
{
GameObject go = Instantiate (cube, new Vector3 (i, j, 0), Quaternion.identity);
go.GetComponent<MeshRenderer> ().material.color = new Color (Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f));
}
}
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown (0))
{
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Physics.Raycast(ray,out hit))
{
GameObject gm=Instantiate(bullet,transform.position,Quaternion.identity);
Vector3 dir=hit.point-transform.position;
gm.AddComponent<Rigidbody> ().AddForce (dir * 200f);
}
}
}
}