unity 桌球碰撞反弹

1.根据入射方向得到反射方向,并保留相同速率

private Vector3 m_preVelocity = Vector3.zero;//上一帧速度

public void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.name == "wall")
    {
        ContactPoint contactPoint = collision.contacts[0];
        Vector3 newDir = Vector3.zero;
        Vector3 curDir = transform.TransformDirection(Vector3.forward);
        newDir = Vector3.Reflect(curDir, contactPoint.normal);
        Quaternion rotation = Quaternion.FromToRotation(Vector3.forward, newDir);
        transform.rotation = rotation;
        rigidbody.velocity = newDir.normalized * m_preVelocity.x / m_preVelocity.normalized.x;
    }
}

原文:https://blog.csdn.net/wsc122181582/article/details/52790317/
版权声明:本文为博主原创文章,转载请附上博文链接!

2.也可以添加
unity 桌球碰撞反弹_第1张图片

unity 桌球碰撞反弹_第2张图片

你可能感兴趣的:(Unity)