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

How to get an AI to look at player without Navmesh ?

Discussion in 'Scripting' started by Mystique, Jan 8, 2015.

  1. Mystique

    Mystique

    Joined:
    Nov 19, 2014
    Posts:
    40
    I'm currently using lookAt but it does not work because when the player passes the 270* angle the enemy rotates all the way in the opposite direction to face the player instead of just turning 1*.

    All the tuts I've seen only show AI navigation and rotation on the Navmesh Agent, but I need to seperate the AIs movement and rotational destinations.
    I'm still a rookie with Quaternions and Vectors, can someone please offer some insight to making an object look at something else without the weird problems mentioned above, thanks.
     
    Last edited: Jan 8, 2015
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
  3. A_never_kill

    A_never_kill

    Joined:
    Jan 7, 2014
    Posts:
    81
    Hello,
    I think i can recognize your problem.You can do the code instead of "lookat".
    Here is the code:
    var target : Transform;
    function Update ()
    {
    var relativePos = target.position - transform.position;

    var rotation = Quaternion.LookRotation(relativePos);

    transform.rotation = rotation;

    }

    looking forward to your reply.

    Thanks
     
  4. Mystique

    Mystique

    Joined:
    Nov 19, 2014
    Posts:
    40
    floating.PNG
    It did not work because the AI is rotating on all axis, and now is floating (even with rigidbody attatched).

    I tried transform.rotation.y = rotation.y. But that did not work because like mentioned before it causes the object to stop tracking when the player moves into the 250* spot.

    I need for th AI to only rotate on the Y axis towards the players general area, I've been experimenting for a while and not much is working, any advice is welcome

    Edit :
    NavMeshAgent.Move
    I have temporarily found a fix for this problem but it is still not well suited for what I'm doing.
     
    Last edited: Jan 9, 2015
  5. ezjm427

    ezjm427

    Joined:
    May 17, 2014
    Posts:
    100
    Example using a character controller - but the Vector functions could be used the same way without CharacterControllers


    Code (CSharp):
    1. public float movementSpeed = 0.03f;
    2. public Vector3 movementVelocity = Vector3.zero;
    3. public float turnRate = 1.11f;
    4.  
    5. CharacterController cmp_cc;          
    6.  
    7.     void Start ()
    8.     {
    9.         cmp_cc = GetComponent<CharacterController>();
    10.     }
    11.  
    12.    //move player one step (movement speed) toward a point
    13.     public void moveToward( Vector3 targetPoint)
    14.     {
    15.  
    16.         Quaternion rotation = Quaternion.LookRotation(targetPoint - transform.position);
    17.         rotation.x = 0f; //don't change (x) up/down rotation
    18.         rotation.z = 0f; //don't change (z) side rotation
    19.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * turnRate);
    20.  
    21.         Vector3 movementVelocity = transform.forward * movementSpeed;
    22.  
    23.         movementVelocity.y = -.08f; //apply some gravity
    24.  
    25.         cmp_cc.Move ( movementVelocity );
    26.  
    27.     }
     
  6. Mystique

    Mystique

    Joined:
    Nov 19, 2014
    Posts:
    40
    Solved, setting the rotation.x and rotation.z to 0 solved the problem I was having with the AI not rotating properly on an individual axis. I thought of trying that method before but I thought it wouldn't work because it would be the same as setting: transform.rotation.y = rotation.y;
    But it seems that weren't the case - thanks for the help
     
    Last edited: Jan 9, 2015