Unity 模拟车辆在地形上移动时姿态的计算

车辆模型需要放在父物体下 

private Ray ray;

private RaycastHit hit;
// Update is called once per frame
        
void Update()
{
            //计算车辆姿态
    ray = new Ray(transform.position+ transform.up, -transform.up * 10);
    Debug.DrawRay(transform.position + transform.up, -transform.up * 10, Color.yellow);
    if (Physics.Raycast(ray, out hit))
    {
        Debug.DrawLine(hit.point, hit.point + hit.normal, Color.red);
        Quaternion NextRot = Quaternion.LookRotation(Vector3.Cross(hit.normal, Vector3.Cross(MarkerModle.transform.forward, hit.normal)), hit.normal);
        MarkerModle.transform.rotation=(Quaternion.Lerp(MarkerModle.transform.rotation, NextRot, 0.1f));
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38062606/article/details/124297425