Search Unity

Executing instance specific actions

Discussion in 'Scripting' started by Chapi, Sep 19, 2014.

  1. Chapi

    Chapi

    Joined:
    Aug 27, 2013
    Posts:
    13
    I'm quite lost here, I'm making a RTS game where you have different units (players) and you select them and use right click to give them the action to move towards certain point.

    The thing is that I've set up everything but the problem is that if I select one of my units and right click to send him to a point, and while its moving I select another unit and send it to another point, the first unit turns around and starts moving to the new destination.

    This is inside the unit.js which is added to the players that move around

    Code (JavaScript):
    1.   function giveAction(actionType : int, target : Transform)
    2.      {
    3.  
    4.          if (actionType == 1 && actionSent == false)
    5.          {
    6.              
    7.              currentAction = 1; //Walk to
    8.              currentTarget = target; //Walk to point
    9.              actionSent = true;
    10.      
    11.          }
    12.  
    13.      }
    14.  
    15.   function Update ()
    16.   {
    17.   if (currentAction == 1)
    18.        {
    19.          if(transform.position != currentTarget.position)
    20.            {
    21.              transform.position = Vector3.MoveTowards(transform.position, currentTarget.transform.position , Time.deltaTime * 50);
    22.                
    23.                  if(transform.position == currentTarget.transform.position)
    24.                        {
    25.                        currentAction = 0;
    26.                        }
    27.            }
    28.      
    29.        }
    30.      
    31.   }
    32.  
    This is where the function is called


    Code (JavaScript):
    1.  function  Update () {
    2.  
    3.   selectedUnits = GameObject.FindGameObjectsWithTag("isSelected");
    4.  
    5.  
    6.   //Debug.Log("Mouse x: " /*+ Input.mousePosition.x +"Mouse y: " + Input.mousePosition.y*/);
    7.  
    8.  
    9.   if (selectedUnits.Length > 0)   //Move the player to point marked by Ray
    10.        {
    11.      
    12.            if(Input.GetMouseButtonDown(1))
    13.            {
    14.          
    15.                for(var i : int = 0; i < (selectedUnits.Length); i++)
    16.               {
    17.               print("Unit number: " + selectedUnits[i] + "has been sent to destination");
    18.               if( i == selectedUnits.Length - 1)
    19.               {
    20.               print("  a");
    21.               }
    22.              
    23.               selectedUnits[i].GetComponent(unit).giveAction(1,moveToTarget);
    24.              
    25.               }
    26.          
    27.          
    28.          
    29.          
    30.          
    31.            }
    32.        }
    33.   }
    This is what makes the player move inside the unit.js script inside every player
    Code (JavaScript):
    1. if (currentAction == 1)
    2.         {
    3.             if(transform.position != currentTarget.position)
    4.                 {
    5.                     transform.position = Vector3.MoveTowards(transform.position, currentTarget.transform.position , Time.deltaTime * 50);
    6.                            
    7.                             if(transform.position == currentTarget.transform.position)
    8.                                         {
    9.                                         currentAction = 0;
    10.                                         actionSent = false;
    11.                                         }
    12.                 }
    13.        
    14.         }
     
  2. Chapi

    Chapi

    Joined:
    Aug 27, 2013
    Posts:
    13
    I finally figured it out, I changed the way of storing the information of the destination from a transform to a simple Vector3 and now it's not getting changed anymore :)