Unity3D-模拟物理完全弹性碰撞反弹

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

    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;
        }
    }

你可能感兴趣的:(Unity)