Unity实现桌面反弹的示例代码
时间:2021-07-22 07:47:49|栏目:.NET代码|点击: 次
一:演示视频

二:代码实现
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;
}
}
}
也可以添加
创建物理材质

修改值就可以发生反弹碰撞了

上一篇:使用SNK密钥文件保护你的DLL和代码不被反编译教程
栏 目:.NET代码
本文标题:Unity实现桌面反弹的示例代码
本文地址:http://www.codeinn.net/misctech/160331.html






