Unity2020游戏物体的碰撞检测和触发检测

1.游戏物体的碰撞检测和触发检测

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class Player : MonoBehaviour
 6 {
 7     public float speed = 1;
 8     private Rigidbody rd;
 9 
10     // Start is called before the first frame update
11     void Start()
12     {
13         rd = GetComponent<Rigidbody>();
14     }
15 
16     // Update is called once per frame
17     void Update()
18     {
19         float h = Input.GetAxis("Horizontal");
20         rd.AddForce(new Vector3(h, 0, 0) * speed);
21         float v = Input.GetAxis("Vertical");
22         rd.AddForce(new Vector3(0, 0, v) * speed);
23     }
24 
25     private void OnTriggerEnter(Collider other)
26     {
27         if (other.gameObject.tag == "food")
28         {
29             Destroy(other.gameObject);
30         }
31     }
32 
33     private void OnCollisionEnter(Collision collision)
34     {
35         Debug.Log("Enter....");
36         Debug.Log("tag:" + collision.gameObject.tag);
37         if (collision.gameObject.tag == "food") {
38             Destroy(collision.gameObject);
39         }
40     }
41 
42     private void OnCollisionExit(Collision collision)
43     {
44         Debug.Log("Exit....");
45     }
46 
47     private void OnCollisionStay(Collision collision)
48     {
49         Debug.Log("Stay....");
50     }
51 }

 

posted @ 2021-12-16 23:39  灰灰5471  阅读(234)  评论(0)    收藏  举报