一:演示视频
二:代码实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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 反弹内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/LLLLL__/article/details/117266549