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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

NavMeshAgent - LookAt while moving?

Discussion in 'Navigation' started by ian-coetzer, Jan 29, 2018.

  1. ian-coetzer

    ian-coetzer

    Joined:
    Oct 11, 2015
    Posts:
    25
    Hi

    While my NavMeshAgent navigates around bumps I want it to look at a target point that it is trying to navigate to, instead of it looking straight ahead.

    how can i have it 'LookAt' a target (locking its eyes / direction on the target's position) while it does it navigation to that target?

    thank you
     
  2. duisti

    duisti

    Joined:
    Nov 29, 2017
    Posts:
    52
    Put the mesh under a pivot. Move the main parent while rotating the pivot.
     
  3. ian-coetzer

    ian-coetzer

    Joined:
    Oct 11, 2015
    Posts:
    25
    Mm not sure i understand will it not walk off the pivot?
     
  4. duisti

    duisti

    Joined:
    Nov 29, 2017
    Posts:
    52
    Hierarchy should look like this
    Code (CSharp):
    1. GameObject(Your parent, put agent component here)
    2.     ->Pivot(rotate this)
    3.           ->Mesh
     
  5. ian-coetzer

    ian-coetzer

    Joined:
    Oct 11, 2015
    Posts:
    25
    mm thank you, i think i see, i will test this and feedback
     
  6. ian-coetzer

    ian-coetzer

    Joined:
    Oct 11, 2015
    Posts:
    25
    I have been battling a lot recently with instantiating an object in front of another object at the 'local' rotation of the object... ended up with this complex function whenever i have to do this:

    Code (CSharp):
    1.  
    2.         Quaternion rotation;
    3.         Vector3 targetPos  = ComputeDestination (90, 0.05f, transform.position, transform.forward);;
    4.  
    5.         rotation = Quaternion.LookRotation (targetPos - transform.position);
    6.  
    7.         GameObject newBullet = Instantiate (BulletPrefab, targetPos, rotation);


    Code (CSharp):
    1. Vector3 ComputeDestination(float degrees, float distance, Vector3 origin, Vector3 forward)
    2. {
    3.     Quaternion rotation;
    4.     Vector3 direction;
    5.     Vector3 result;
    6.  
    7.     //compute the rotation
    8.     rotation = Quaternion.AngleAxis(degrees, Vector3.up);
    9.     //add distance to teh direction
    10.     direction = rotation * forward * distance;
    11.     //add the desired distance and direction to the origin point
    12.     result = origin + direction;
    13.  
    14.     return result;
    15. }