Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Resolved Determing slope direction with DOTS physics

Discussion in 'DOTS Physics' started by Serenian, Nov 22, 2021.

  1. Serenian

    Serenian

    Joined:
    Jan 18, 2018
    Posts:
    4
    Hello, this should be pretty easy to figure out; however, for the life of me I cannot seem to get anything to work correctly.

    Problem: I'm trying to adjust the angle of the treads of my robot player on a slope. However, I cannot seem to figure out how to determine if the slope is positive of negative depending on where the character is facing. (See Image)

    upload_2021-11-21_23-15-15.png

    My Current Solution: I am able to figure out the normal from the ground collider hit and the overall angle; however, I have no clue how to get a signed value nor do I know how to do this appropriately. Here are my two code blocks that I attempted to use to solve this issue:

    upload_2021-11-21_23-19-54.png

    Bottom line objective: I'd like to adjust the treads to match the ground slope.

    Any guidance or help would be greatly appreciated. Thank you!
     
    Last edited: Nov 22, 2021
  2. Serenian

    Serenian

    Joined:
    Jan 18, 2018
    Posts:
    4
    SOLUTION

    Alright, I figured it out. I was using the wrong axis when I was determining the sign of the angle. Here is my code for future folks who may run into the same problem:

    Code (CSharp):
    1.     var normal = characterBody.GroundHit.Normal;
    2.     var forward = MathUtilities.GetForwardFromRotation(rotation.Value);
    3.     var projection = MathUtilities.ProjectOnPlane(forward, normal);
    4.          
    5.     var axis = MathUtilities.GetRightFromRotation(rotation.Value);
    6.  
    7.     var angle = MathHelpers.Angle(forward, projection);
    8.     var angleSigned = MathHelpers.AngleSigned(forward, projection, axis);
    9.  
    Mind you, that you will need to create these helper functions (which the source code can be adapted from the
    Vector3
    class found on Untiy's GitHub repo.
     
unityunity