首页 > 编程知识 正文

Unity Rigidbody组件,vue组件

时间:2023-05-04 20:21:26 阅读:238717 作者:3074

Unity 学习笔记汇总
Rigidbody官方API使用文档

文章目录 1. 碰撞器11.1. 前台1.2. 代码1.3. 结果 2. 碰撞器23. 碰撞器33.1. 前台3.2. 代码3.3. 结果

1. 碰撞器1 1.1. 前台 创建GameObject3D ObjectCube、GameObject3D ObjectSphere、GameObject3D ObjectPlane给Cube和Sphere添加ComponentPhysicsRigidbody将C#脚本挂载到Cube上

1.2. 代码 using System.Collections;using System.Collections.Generic;using UnityEngine;public class NewBehaviourScript : MonoBehaviour{ // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } void OnCollisionEnter(Collision collision) { Debug.Log("检测到了碰撞"); } void OnCollisionStay(Collision collision) { Debug.Log("碰撞停留"); } void OnCollisionExit(Collision collision) { Debug.Log("碰撞结束"); }} 1.3. 结果

在控制台上会实时输出碰撞状态。

2. 碰撞器2 using System.Collections;using System.Collections.Generic;using UnityEngine;public class NewBehaviourScript : MonoBehaviour{ // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } void OnCollisionEnter(Collision collision) { Debug.Log("检测到了碰撞"); //collision: 指的是身上没有该函数脚本的那个物体 Debug.Log(collision.gameObject.name); Destroy(collision.gameObject); //销毁的是未挂载当前脚本的物体 Destroy(gameObject); //销毁的是挂载当前脚本的物体 } void OnCollisionStay(Collision collision) { //Debug.Log("碰撞停留"); } void OnCollisionExit(Collision collision) { Debug.Log("碰撞结束"); }} 3. 碰撞器3 3.1. 前台

创建一个Capsule的预制体,并与脚本中的变量进行绑定

3.2. 代码 using System.Collections;using System.Collections.Generic;using UnityEngine;public class NewBehaviourScript : MonoBehaviour{ public GameObject contractPrefab; GameObject clone; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } void OnCollisionEnter(Collision collision) { //Debug.Log("检测到了碰撞"); collision: 指的是身上没有该函数脚本的那个物体 //Debug.Log(collision.gameObject.name); //Destroy(collision.gameObject); //销毁的是未挂载当前脚本的物体 //Destroy(gameObject); //销毁的是挂载当前脚本的物体 ContactPoint[] points = collision.contacts; if (clone == null) { clone = Instantiate(contractPrefab, points[0].point, contractPrefab.transform.rotation) as GameObject; } else { Destroy(clone.gameObject, 0.5f); //clone = Instantiate(contractPrefab, points[0].point, // contractPrefab.transform.rotation) as GameObject; } } void OnCollisionStay(Collision collision) { //Debug.Log("碰撞停留"); } void OnCollisionExit(Collision collision) { Debug.Log("碰撞结束"); }} 3.3. 结果

Sphere发生碰撞后,会生成一个Capsule的预制体

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。