Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Resolved Applying ground tilt while rotating in place not working

Discussion in 'Physics' started by NimbleSprite, Jun 21, 2022.

  1. NimbleSprite

    NimbleSprite

    Joined:
    Nov 3, 2017
    Posts:
    34
    Thanks for any help, greatly appreciated... = )

    This short video shows the issue clearly:

    https://drive.google.com/file/d/1NjX9oAd3FjhF8akMfTdGRD4i2Cp-cSve/view?usp=sharing

    The tanks in the video are rotating in place, and they are supposed to continuously match the terrain normal. There is code that gradually rotates them on the Y axis to point where they are supposed to turn towards, which you can see they do. After this, we are trying to apply the correct ground tilt using the terrain normal on the remaining x and z axes before finally setting the rotation for real. The LerpedMove in this code is the tank GameObject. For some reason, it appears that the ground tilt is simply not being applied at all. The tanks, as you can see, just rotate about the Y axis and completely ignore the attempt to apply tilt. This exact method of applying ground tilt is the same one we use when the tanks are just moving forward, and it works perfectly in that scenario. The tanks normally tilt to match the ground perfectly when they are moving forward along their spline paths, as you can see in the video, it just applies tilt on the x and z axes to match the terrain and looks fine.

    //get ground tilt
    if (Physics.Raycast(new Vector3(LerpedMove.position.x, LerpedMove.position.y + 50, LerpedMove.position.z), Vector3.down, out hit, 150f, Terrain))
    {
    curNormal = hit.normal;
    }
    grndTilt = Quaternion.FromToRotation(Vector3.up, curNormal);

    //do some rotation on Y axis towards new end point
    Quaternion newRotation = Quaternion.Lerp(prevRotation, nextRotation, (float)rotationProgress);

    //modify the rotation to include the ground tilt, and set the final rotation
    newRotation = Quaternion.Euler(-grndTilt.eulerAngles.x, newRotation.eulerAngles.y, -grndTilt.eulerAngles.z);
    LerpedMove.rotation = newRotation;
     
  2. NimbleSprite

    NimbleSprite

    Joined:
    Nov 3, 2017
    Posts:
    34
    The issue was that the nextRotation was rotating the tank about the world Y axis instead of the normal.