0
votes

I'm creating a basic AI script for my enemies in Unity and I have most of it working the way I want it to. The way I have my enemies set up they contain 2 colliders, a polygon collider that destroys the player when touched, and an empty game object that's a child of the enemy that is a circle collider that acts as a trigger. There's a game object that's tagged Straight Road and when the circle collider comes in contact with it, it should run a function called StopMovement(); that sets the enemies movement to 0. I used to Debug.Log(); to check to see if the collider recognizes that it's touching Straight Road and it doesn't. This is my code below. I'm hoping someone has a suggestion.

public class DogAI : GenericController {

    public Transform target;
    public float chaseRange;
    public float maxDistance;

    private Vector3 targetDirection;
    private float targetDistance;

    // Use this for initialization
    void Start()
    {
        base.Start();
    }

    // Update is called once per frame
    void Update()
    {
        base.Update();
        if (target.transform != null)
        {
            targetDirection = target.transform.position - transform.position;
            targetDirection = targetDirection.normalized;
            targetDistance = Vector3.Distance(target.position, transform.position);

            if (targetDistance <= chaseRange)
            {
               SetMovement(targetDirection);
            }

            Vector3 enemyScreenPosition = Camera.main.WorldToScreenPoint(transform.position);

            if (targetDistance > maxDistance)
            {
                Destroy(gameObject);
            }
        }
    }

    void StopMovement()
    {
        SetMovement(new Vector2(0,0));
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Straight Road"))
        {
            Debug.Log("Stop! There's a road!");//This never shows up in the log?
            StopMovement();
        }
    }

    void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            DestroyObject(other.gameObject);
        }
    } 

Generic Controller script below containing the SetMovement() function.

public abstract class GenericController : MonoBehaviour
{
    public float movementSpeed = 20;
    float animationSpeed = 1;

    protected Rigidbody2D rigidbody;
    protected Animator animator;

    Vector2 movementVector;
    float currentSpeed;

    protected bool needAnimator = true;

    // Use this for initialization
    protected void Start()
    {
        rigidbody = GetComponent<Rigidbody2D>();

        if (needAnimator)
        {
            animator = GetComponent<Animator>();
            animator.speed = animationSpeed;
        }
    }


    protected void FixedUpdate()
    {
        rigidbody.velocity = movementVector;
        currentSpeed = rigidbody.velocity.magnitude;

        if (needAnimator)
            animator.SetFloat("Speed", currentSpeed);
    }

    public void SetMovement(Vector2 input)
    {
        movementVector = input * movementSpeed;
    }

    public void SetMovement(int x, int y)
    {
        SetMovement(new Vector2(x, y));
    }
1
Did you try printing other.gameObject.tag? Incase its a prefab or something, there might be a name difference. Like "Straight Road(Clone)" or something of that sortAnil
No it's not that. I placed a road in the scene for testing so that wouldn't happen. I just recently figured out why the debug wasn't showing in the console though. I ended up attaching a 2D Box Collider to the road. It shows the debug, but it still doesn't run StopMovement()AwesomeBob2341
can you share the setmovement code? might as well have look in there and any other place where movement code is attached? also did you check if your code runs in update after you said stop movement? i can see if (target.transform != null) will still be there if you did not made target.transform = null which might also result in overriding your stopmovement in updatekiller_mech
I can add the SetMovement code. It's in a separate class another guy in my team created. I have if (target.transform != null) in there to just make sure we didn't mistakenly forget to add the player into the scene. Not sure if that is or isn't part of the issue though.AwesomeBob2341
Why CompareTag? Shouldn't it be other.gameObject.tag?Thalthanas

1 Answers

2
votes

From the documentation:

MonoBehaviour.OnTriggerEnter2D(Collider2D)

Sent when another object enters a trigger collider attached to this object (2D physics only).

The keyword here is enters. In other words, a trigger is for when something goes inside the area of the collider, like a player entering a region of the map, where the region is a trigger collider. If you want something to happen when a collider collides with the road, i.e. when your CircleCollider comes in contact with the road, then you want the collider to not be a trigger, and you want the functionality to be inside OnCollisionEnter2D.