Skip to main content
added 941 characters in body
Source Link
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;
    }
}
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 OnTriggerExit2D(Collider2D col){
    if (col.CompareTag ("Collectable")) {
        mustReturn = true;
    }
}
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;
    }
}
edited title
Source Link
House
  • 73.5k
  • 17
  • 188
  • 276

Unity2D. Trigger wont function unless is a child of the player How to properly activate trigger objects?

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 following,flying 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 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

Unity2D. Trigger wont function unless is a child of the player?

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. 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 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

How to properly activate trigger objects?

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 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

deleted 1935 characters in body
Source Link
House
  • 73.5k
  • 17
  • 188
  • 276

AI class

using UnityEngine; using System.Collections;

public class SparxAI : MonoBehaviour {script

public float speed = 3.0F;
Vector3 oldPosition;
Player player;
bool mustReturn;
ArrayList positionList = new ArrayList();
float timerDelay = 0F;
float idleDelay = 0F;



// Use this for initialization
void Start () {
    player = GameObject.FindGameObjectWithTag ("Player").GetComponent<Player> ();
}

// Update is called once per frame
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 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;
        

    

    /*  transform.position = Vector3.MoveTowards (
            transform.position, col.transform.position, Time.deltaTime * speed);

        if(transform.position == col.transform.position)
            mustReturn = true; */


        
    } 

} 

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

public class SparxCollider : MonoBehaviour {

// Use this for initialization
void Start () {
    
}

// Update is called once per frame
void Update () {
    
}

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")) {
        
        
    }
    
}

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

AI class

using UnityEngine; using System.Collections;

public class SparxAI : MonoBehaviour {

public float speed = 3.0F;
Vector3 oldPosition;
Player player;
bool mustReturn;
ArrayList positionList = new ArrayList();
float timerDelay = 0F;
float idleDelay = 0F;



// Use this for initialization
void Start () {
    player = GameObject.FindGameObjectWithTag ("Player").GetComponent<Player> ();
}

// Update is called once per frame
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 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;
        

    

    /*  transform.position = Vector3.MoveTowards (
            transform.position, col.transform.position, Time.deltaTime * speed);

        if(transform.position == col.transform.position)
            mustReturn = true; */


        
    } 

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

public class SparxCollider : MonoBehaviour {

// Use this for initialization
void Start () {
    
}

// Update is called once per frame
void Update () {
    
}

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")) {
        
        
    }
    
}

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

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 OnTriggerExit2D(Collider2D col){
    if (col.CompareTag ("Collectable")) {
        mustReturn = true;
    }
}
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")) {

    }
}

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

Source Link
Loading