Search Unity

Help needed - AI Movement randomly timing out?

Discussion in 'Scripting' started by Gojiragh, Sep 17, 2021.

  1. Gojiragh

    Gojiragh

    Joined:
    Jun 6, 2021
    Posts:
    29
    Hey there,

    I have an AI character which goes left until he hits a wall then switches direction to right on collision, then left on collision and so on.

    I'm having an issue with him stopping movement instantly at seemingly random points in the game. Sometimes before a collision, sometimes after and all depends on his starting position?

    I even found a specific starting position where he never stops...

    Debug.Log(rigidbody.velocity) shows the character knows and believes it's moving as the velocity doesn't stop, but he does stop! I've also checked my colliders and can't find any impact points.

    Anyone have any idea why this might be happening?

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Gumbo : MonoBehaviour
    4. {
    5.  
    6.     Rigidbody2D rigidbody;
    7.  
    8.     float dirX;
    9.     float moveSpeed;
    10.  
    11.     void Start()
    12.     {
    13.         rigidbody = GetComponent<Rigidbody2D>();
    14.         dirX = -1f;
    15.         moveSpeed = 2f;
    16.     }
    17.  
    18.     private void OnCollisionEnter2D(Collision2D collision)
    19.     {
    20.         if (collision.gameObject.transform.position.y < rigidbody.position.y)
    21.         {
    22.             return;
    23.         }
    24.  
    25.         else  if (collision.gameObject)
    26.         {
    27.             dirX *= -1f;
    28.             Debug.Log("Change direction triggered");
    29.         }
    30.     }
    31.  
    32.     void FixedUpdate()
    33.     {
    34.         rigidbody.velocity = new Vector2(dirX * moveSpeed, rigidbody.velocity.y);
    35.     }
    36. }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    OnCollisionEnter2D is good for telling you if you hit something, but not great for telling you where you hit something. I'd remove it entirely and use a raycast in the direction the enemy is moving in. If you hit something, and it's a wall (not the player), flip around. That will significantly more reliable.
     
    Gojiragh and Lethn like this.
  3. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    To add to @GroZZleR 's comment The most obvious thing to me is that your if statement with the return code is likely triggering at strange points and it could be a result of your collider setup too, to confirm this put a debug.log before the return and check if the code is activating at the points that your character is stopping. Other than that it may be that your rigidbody physics itself is behaving oddly and reacting with other things in the scene that have rigidbodies if it's not the if statement itself. You don't seem to be making use of tags or collision layers so it could be interacting with anything else in the scene that has a collider and a rigidbody.
     
    Gojiragh likes this.
  4. Gojiragh

    Gojiragh

    Joined:
    Jun 6, 2021
    Posts:
    29
    Thank you both so much for these replies!

    Absolutely no collisions picking up on Debug.Log and velocity isn't changing at all.

    I'll absolutely make note of these though, I'm still noob so haven't got to Raycasting but definitely have it on my list to learn as I have to sort some FOV's. I'm doing a Udemy course atm and want to get to it there so I can learn it, rather than Youtube it etc.

    Grozzler, i've made note to implement your improvement once I actually learn raycasting.

    BTW - I fixed this by changing from a Box Collider 2d to an edge collider? Can't remember the specifics but someone somewhere mentioned that Unity can freak out when Box Colliders 2D's are used?