Search Unity

NavMesh Agent + Character Controller

Discussion in 'Navigation' started by pedronlemos, Oct 3, 2018.

  1. pedronlemos

    pedronlemos

    Joined:
    Oct 3, 2018
    Posts:
    2
    Hi,

    Im trying unsuccessufully when moving an agent after reaching the destination that the character faces an object at an exact angle of 0 degrees in 3D


    -----\
    \ P -- > []

    [] <-- P

    Sry for the top graphics :)
     
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Can you specify what exactly you mean by exact angle? Do you mean that the character and the object stand on a straight line along a world axis? Do you mean the character looks directly at the object?
     
  3. pedronlemos

    pedronlemos

    Joined:
    Oct 3, 2018
    Posts:
    2
  4. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Ah, now I understand.
    What you'll have to do is detect when you're done moving, if there's any object like a wall nearby, and rotate the entire gameObject towards the object you want.
    You can calculate the rotation you need with Transform.Lookat, the only challenge here being that your character will try to look at the object's pivot which can still result in an anglein the case of walls and other bigger objects. Your best course of action would be to first figure out the correct point and using that for your lookat.
    Top of my head you could do this by taking the target object and figure out if it's a wall, and if so what are its length and width axis. Look on what side your character is and take that side's normal. Then, flip that normal around and use it to cast a ray from your character, the resulting point should be the point your character will want to look at.
    Keep in mind this will complicate things as you add new objects your character can walk against, like spheres, cyllinders, and complex meshes. You will probably want to consider if this is something worth bothering with, and if it is try to find a workaround solution...