Unity實現桌面反彈的示例代碼
一:演示視頻
二:代碼實現
using UnityEngine; public class Ball : MonoBehaviour { private Rigidbody rigid; private Vector3 lastDir; public float speed = 30; private void Awake() { rigid = GetComponent<Rigidbody>(); rigid.velocity = new Vector3(1, 0, 1) * speed; } private void LateUpdate() { lastDir = rigid.velocity; } private void OnCollisionEnter(Collision other) { if (other.gameObject.tag == "Wall") { Vector3 reflexAngle = Vector3.Reflect(lastDir, other.contacts[0].normal); rigid.velocity = reflexAngle.normalized * lastDir.magnitude; } } }
也可以添加
創建物理材質
修改值就可以發生反彈碰撞瞭
到此這篇關於Unity實現桌面反彈的示例代碼的文章就介紹到這瞭,更多相關Unity 反彈內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!