Search Unity

Issues with LookAt and Moving AI

Discussion in 'Physics' started by syvirx, May 5, 2020.

  1. syvirx

    syvirx

    Joined:
    Apr 10, 2020
    Posts:
    2
    So I'm currently trying to make a stealth game and am having issues with moving my enemy. I have a method to evaluate whether the player is visible to my enemy (I used raycasts for this), if the player is visible I make the enemy LookAt the player. Now the issue is, I just want my character to move toward the player when this occurs, but in fact it moves backwards. It moves backwards when I change the movement speed to be negative or positive, it also does this when I try to get it to move in any other direction.

    It does however, move forward whenever my player is above a certain Y-value threshold. I've oriented my object to be facing the correct way (the blue arrow faces in the direction I want the entity to face), and there shouldn't be any other things that factor into the direction it would be moving in. I've also tried just getting it to move forward without any check to see if the player has been spotted and it does move in the correct direction. For that check, I just instantiated my Rigidbody and added a force in the transform.forward direction.

    Here are snippets of my code.

    This is what I'm using primarily, it moves backwards no matter what values I put in whenever the player is spotted, I've used both what is shown here and (Rigidbody) body.addForce(transform.forward*movespeed):

    Code (CSharp):
    1.    public void FixedUpdate()
    2.     {
    3.         Rigidbody body = GetComponent<Rigidbody>();
    4.         if(spotted())
    5.         {
    6.             transform.LookAt(player);
    7.             transform.position = transform.position + transform.forward.normalized*moveSpeed;
    8.         }
    9.     }
    This is the spotted method that was called in my if statement, in case anyone wants to check it:

    Code (CSharp):
    1.     public bool spotted()
    2.     {
    3.         RaycastHit hit;
    4.         float angleBetween = Vector3.Angle(transform.forward, (player.position - transform.position).normalized);
    5.         if(angleBetween <= angle)
    6.         {
    7.             Ray ray = new Ray(transform.position, player.position - transform.position);
    8.             if(Physics.Raycast(ray, out hit, radius))
    9.             {
    10.                 if(hit.transform == player)
    11.                 {
    12.                     return true;
    13.                 }
    14.             }
    15.         }
    16.         return false;
    17.     }
     
    Last edited: May 5, 2020
  2. norilalatoli

    norilalatoli

    Joined:
    Aug 9, 2018
    Posts:
    12
    You've gotta remember that LookAt will rotate the transform to point the Z-Axis of the transform (local Z-axis) toward the transform. That means your entire player is rotated along the X and Y axes. Assuming that the player and AI guy are on the same Y-level though, that means LookAt will only rotate along the Y-Axis.

    The AI guy should be traveling properly though, according to your code. Are there any colliders in play here? Perhaps there is some funky physics going on.

    Don't use a normalized vector to move the AI guy. Its already normalized. Use Debug.Log to see what direction transform.forward is actually going. That's my advice.
     
    syvirx likes this.
  3. syvirx

    syvirx

    Joined:
    Apr 10, 2020
    Posts:
    2
    Thanks for the tips! I do have colliders that could possibly be affecting this as I'm not really sure how exactly to implement them cleanly. I basically just made a box that encompasses the entirety of my objects.

    Anyways, I just made a blank game object that serves as a pathing element for the cat, keeping it always level with the cat's y position. It's inelegant, but it'll do for now. Thanks again for the help!