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.
  2. Dismiss Notice

Question How to make sure that the Agent is always Perpendicular to the walkable surface?

Discussion in 'Navigation' started by soleron, Mar 14, 2022.

  1. soleron

    soleron

    Joined:
    Apr 21, 2013
    Posts:
    522
    How can we make sure the Agent is always perpendicular to the walkable surface to avoid having the character going through the surface?

    upload_2022-3-14_13-59-43.png

    Thank you.
     
  2. SunnyValleyStudio

    SunnyValleyStudio

    Joined:
    Mar 24, 2017
    Posts:
    67
    Hey!

    Usually you want to have a trigger collider or a raycast that shoots from the Agents downwards. This helps you to check if the Agent is grounded but in terms of a Raycast you will get back RaycastHit struct with all the info about the object that you have detected - in this case it would be the surface.

    If you take a look at the RaycastHit documentation https://docs.unity3d.com/ScriptReference/RaycastHit.html you will find that it have a Vector3 normal which is just a vector coming out of the surface mesh upwards and generally gives you the perpendicular direction "up" (I am fairly sure that you can get similar info rom a trigger collider).

    You can rotate / align your Agents transform to have its transform.up match the normal of the surface which should make your Agent always perpendicular to the surface.

    I think that you can use Quaternion.FromToRotation to find the correct rotation.

    You can check out this thread to learn more https://forum.unity.com/threads/character-align-to-surface-normal.33987/

    I hope it helps!
     
    soleron likes this.
  3. soleron

    soleron

    Joined:
    Apr 21, 2013
    Posts:
    522
    Thank you for your help!