Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Moving from one point to another? [With pictures]

Discussion in '2D' started by FirefIy, May 15, 2015.

  1. FirefIy

    FirefIy

    Joined:
    Apr 15, 2015
    Posts:
    23
    Hello friends,

    I have this crazy idea right now, where I have flying cat following my dragon, and whenever the cat reaches a certain range it grabs the red orb (collectible) and flies back to our hero. I made it work perfectly whenever there's only one object, but whenever there's multiple objects that he must grab, he grabs the first one, afks, and starts following our hero, yet the 2nd object is being destoried without our cat even touching it! Any ideas how to solve this?

    This is my Flying's cat script
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SparxAI : MonoBehaviour {
    6.  
    7.     public float speed = 3.0F;
    8.     Vector3 oldPosition;
    9.     Player player;
    10.     bool mustReturn;
    11.     ArrayList positionList = new ArrayList();
    12.     float timerDelay = 0F;
    13.  
    14.  
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         player = GameObject.FindGameObjectWithTag ("Player").GetComponent<Player> ();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update () {
    23.  
    24.         if (mustReturn) {
    25.         //    positionList.RemoveAt(0);
    26.             transform.position = Vector3.MoveTowards (
    27.                 transform.position, player.transform.position, Time.deltaTime * speed);
    28.  
    29.         }
    30.  
    31.         if (timerDelay > 0) {
    32.             timerDelay-= Time.deltaTime;
    33.         }
    34.         if (timerDelay == 0 && positionList.Count > 0) {
    35.             positionList.RemoveAt(0);
    36.             Debug.Log ("Removing list item");
    37.             mustReturn = true;
    38.         }
    39.  
    40.         Debug.Log ("delay is " + timerDelay);
    41.         Debug.Log ("pos count is " + positionList.Count);
    42.  
    43.     }
    44.  
    45.     void OnTriggerStay2D(Collider2D col){
    46.         if (col.CompareTag ("Collectable")) {
    47.  
    48.             foreach (Vector3 nextPos in positionList)
    49.             {
    50.                 transform.position = Vector3.MoveTowards (
    51.                 transform.position, nextPos, Time.deltaTime * speed);
    52.                 Destroy(col.gameObject, 1F);
    53.             }
    54.  
    55.  
    56.          
    57.         }
    58.      
    59.     }
    60.  
    61.     void OnTriggerEnter2D(Collider2D col){
    62.         if (col.CompareTag ("Collectable")) {
    63.  
    64.             positionList.Add(col.transform.position);
    65.             timerDelay = 1F;
    66.             mustReturn = false;
    67.          
    68.  
    69.      
    70.  
    71.         /*    transform.position = Vector3.MoveTowards (
    72.                 transform.position, col.transform.position, Time.deltaTime * speed);
    73.  
    74.             if(transform.position == col.transform.position)
    75.                 mustReturn = true; */
    76.  
    77.  
    78.          
    79.         }
    80.  
    81.     }
    82.     void OnTriggerExit2D(Collider2D col){
    83.         if (col.CompareTag ("Collectable")) {
    84.  
    85.             mustReturn = true;
    86.         }
    87.      
    88.     }
    89.  
    90.  
    91. }
    92.  




    (Ignore the purple orb flying. I have that attack set on left Ctrl, same button where I do my Ctrl+Print screen for a cropped printsreen image)
     
  2. FirefIy

    FirefIy

    Joined:
    Apr 15, 2015
    Posts:
    23
    No one? 2D part of the Unity forums is literally dead. Are people making 2D games in something else?