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: