Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Resolved Determing slope direction with DOTS physics

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

  1. Serenian


    Jan 18, 2018
    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)


    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:


    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


    Jan 18, 2018

    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);
    5.     var axis = MathUtilities.GetRightFromRotation(rotation.Value);
    7.     var angle = MathHelpers.Angle(forward, projection);
    8.     var angleSigned = MathHelpers.AngleSigned(forward, projection, axis);
    Mind you, that you will need to create these helper functions (which the source code can be adapted from the
    class found on Untiy's GitHub repo.