碰到球删除球
参照实例化预制体,把预制体球添加标签Player.
移动脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move : MonoBehaviour {
private Rigidbody ri;
public float speed = 5.0f;
// Use this for initialization
void Start () {
ri = this.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update ()
{
if (Input.GetKey(KeyCode.W))
{
ri.MovePosition(transform.position + Vector3.forward * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
ri.MovePosition(transform.position + Vector3.back * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
ri.MovePosition(transform.position + Vector3.left * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
ri.MovePosition(transform.position + Vector3.right * speed * Time.deltaTime);
}
}
void OnCollisionEnter(Collision coll)//被碰撞的形参
{
if (coll.gameObject.tag == "Player")//如果碰到标签为"Player"的物体,就销毁它
{
Destroy(coll.gameObject);
}
}
}
实例化物体脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class copysp : MonoBehaviour {
public GameObject ga;
// Use this for initialization
void Start () {
for (int i = 0; i < 10; i++)
{
Vector3 ve3 = new Vector3(Random.Range(-5.0f, 5.0f), 2.0f, Random.Range(-5.0f, 5.0f));
Instantiate(ga, ve3, Quaternion.identity);
}
}
// Update is called once per frame
void Update () {
}
}