1
\$\begingroup\$

I have made a flying cat that follows the player, but I want the cat to be a separate object. Cat is following the player and as soon as it sees a collectible it grabs it for the player and then returns following our player. I have it working, but I need the cat to be a different object, it only triggers properly when it's a child of the player object. I have made two scripts for the cat, one for detection, following, flying and the other for destruction of collectibles.

AI script

void OnTriggerStay2D(Collider2D col){
    if (col.CompareTag ("Collectable")) {
        //Destroy(col.gameObject, 1F);
        if(positionList.Count <= 0){
        positionList.Add(col.transform.position);
        timerDelay = 1F;
        mustReturn = false;
        }
    }
}

void OnTriggerEnter2D(Collider2D col){
    if (col.CompareTag ("Collectable")) {
        positionList.Add(col.transform.position);
        timerDelay = 1F;
        mustReturn = false;
    } 

}
void Update () {

    if (mustReturn) {
    //  positionList.RemoveAt(0);
        transform.position = Vector3.MoveTowards (
            transform.position, player.transform.position, Time.deltaTime * speed);

    }

    if (timerDelay > 0) {
        timerDelay-= Time.deltaTime;
        idleDelay = 0;

        if(positionList.Count > 0) {
        foreach (Vector3 nextPos in positionList)
        {
            transform.position = Vector3.MoveTowards (
            transform.position, nextPos, Time.deltaTime * speed);

        }
    }


}



    if (timerDelay <= 0) {

        if(positionList.Count > 0) {
            positionList.Clear();
            timerDelay = 1F;
        }

        if(positionList.Count <= 0)
        mustReturn = true;
    }


    if (idleDelay >= 3F) {
        idleAnimation();
    }


    /*Debug.Log ("pos count is " + positionList.Count);

    if(timerDelay > 0)
        Debug.Log ("delay is " + timerDelay);*/

    idleDelay += Time.deltaTime;

}

void idleAnimation(){


}

void OnTriggerExit2D(Collider2D col){
    if (col.CompareTag ("Collectable")) {
        mustReturn = true;
    }
}

Destruction class

void OnTriggerStay2D(Collider2D col){
    if (col.CompareTag ("Collectable")) {
        Destroy (col.gameObject, 0.5F);
    }
}

void OnTriggerEnter2D(Collider2D col){
    if (col.CompareTag ("Collectable")) {

    } 

}
void OnTriggerExit2D(Collider2D col){
    if (col.CompareTag ("Collectable")) {

    }
}

Question: Why won't the cat function on it's own???

Collectibles DO have that tag, and they DO have a Box Collider 2D along with a 'Trigger: true'

Cat: cat colliders inspector

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

When an object has a Collider set to Trigger, but no rigid body, it's called a "Static Trigger Collider". This is likely the scenario for both your collectables and your cat.

If you look at the "Trigger messages are sent upon collision" chart near the bottom of this page

enter image description here

You'll see that two Static Trigger Colliders will not interact with each other. If you want your collectibles to have their trigger methods called, your cat needs to be one of the following:

  • Rigidbody Collider
  • Kinematic Rigidbody Collider
  • Rigidbody Trigger Collider
  • Kinematic Rigidbody Trigger Collider

Notice anything about all those options? They're all rigidbody types! You need to add a RigidBody2D component to your cat!. It's up to you if you want the non-kinematic or kinematic.

  • Non-kinematic: will collide with objects and should be controlled via the physics system (i.e. applying forces to it)
  • Kinematic object isn't simulated in the physics and should be moved around by modifying its transform (position, rotation).
\$\endgroup\$
2
  • \$\begingroup\$ Oh my god, you're genius! Now everything works perfectly. I also got told that I should use Coroutines instead of Update(), I rewrite the code there but right now, I'll stick to what I have. Tyvm again. \$\endgroup\$
    – Aleksei D
    Commented May 16, 2015 at 16:22
  • \$\begingroup\$ Sure thing. Switching to coroutines now is a bit premature optimization. Just get your game made first, then see if you even need to optimize the updates. \$\endgroup\$
    – House
    Commented May 16, 2015 at 16:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .