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

[JS ]Making a unit unable to move after a move order has already been given.

Discussion in 'Scripting' started by slimabob, Aug 10, 2014.

  1. slimabob

    slimabob

    Joined:
    Jul 7, 2013
    Posts:
    23
    So at the moment, the script is supposed to be able to detect if the unit is clicked- and then select it, after that, two buttons come up "move" and "attack". If move is selected, the player is supposed to be able to issue ONE move command by clicking where he wants the unit to move. Unfortunately, I've tried several things to prevent the unit from being able to move after a move order has already been issued, but to no avail. Hopefully someone here can help, because I'm all out of ideas.

    Code (JavaScript):
    1.     var Selected : boolean;
    2.     var isAtkButtonVisible : boolean = false;
    3.     var isMvButtonVisible : boolean = false;
    4.     var isMoving : boolean = false;
    5.     var unitMoveButton : boolean = false;
    6.  
    7.     var moveSpeed:float = 20;
    8.     private var targetPosition:Vector3;
    9.     private var targetDistance:float;
    10.  
    11.     var speed = 3;
    12.  
    13.     var buttonRectangle : Rect = Rect(100, 100, 100, 50);
    14.  
    15.     function Start(){
    16.     Selected = false;
    17.     isAtkButtonVisible = false;
    18.     isMvButtonVisible = false;
    19.     targetPosition = transform.position;
    20.     }
    21.  
    22.     function OnMouseOver(){
    23.         if(Input.GetMouseButtonDown(0)){
    24.             Selected = true;
    25.             isAtkButtonVisible = true;
    26.             isMvButtonVisible = true;
    27.          
    28.     //        Commands();
    29.         }
    30.     }
    31.  
    32.     function MoveUnit(){
    33.          
    34.  
    35.             targetDistance = Vector3.Distance(targetPosition, transform.position);
    36.  
    37.      
    38.         if(targetDistance < 1){ // prevents shaking when it reaches location
    39.             moveSpeed = 0;
    40.         }
    41.         else if(targetDistance > 1){
    42.             moveSpeed = 5;
    43.         }
    44.      
    45.         if(Input.GetKeyDown(KeyCode.Mouse0) && (unitMoveButton))
    46.         {
    47.             var playerPlane = new Plane(Vector3.up, transform.position);
    48.             var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    49.             var hitdist:float = 0.0;
    50.    
    51.             if (playerPlane.Raycast (ray, hitdist)) {
    52.                 targetPosition = ray.GetPoint(hitdist);
    53.             }
    54.         }
    55.    
    56.         if(targetDistance > 1){ // Prevents code running when it doesn't need to
    57.             transform.position += (targetPosition - transform.position).normalized * moveSpeed * Time.deltaTime;
    58.         }
    59.         transform.position.y = 1;
    60.      
    61.     }
    62.  
    63.  
    64.     function Update(){
    65.         if(Input.GetKeyDown(KeyCode.Escape)){
    66.             Selected = false;
    67.             isAtkButtonVisible = false;
    68.             isMvButtonVisible = false;
    69.         }
    70.      
    71.         if(isMoving){
    72.         MoveUnit();
    73.         }
    74.     }
    75.  
    76.    
    77.    
    78.     function OnGUI(){
    79.     if(isAtkButtonVisible)
    80.     {
    81.  
    82.         if(GUI.Button(Rect(Screen.width/6 - 75,Screen.height/1.1 - 25,150,50),"Attack"))
    83.         {
    84.             isAtkButtonVisible = false;
    85.             isMvButtonVisible = false;
    86.         }
    87.     }
    88.  
    89.     if(isMvButtonVisible){
    90.     if(GUI.Button(Rect(Screen.width/4,Screen.height/1.1 - 25,150,50),"Move") && (unitMoveButton == false))
    91.         {
    92.             isMvButtonVisible = false;
    93.             isAtkButtonVisible = false;
    94.             unitMoveButton = true;
    95.             isMoving = true;
    96.         }
    97.       }
    98.     }
     
  2. lrlelaldl

    lrlelaldl

    Joined:
    Jul 27, 2014
    Posts:
    75
    You could either make the unit start moving the second the order is given, and not allow orders to be given when isMoving is true, or you could add another boolean OrderGiven, and nto allow more orders to be given when that one is true.
     
  3. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Use
    Code (CSharp):
    1. NavMeshAgent.Stop();
    To stop the agent from moving.
     
  4. lrlelaldl

    lrlelaldl

    Joined:
    Jul 27, 2014
    Posts:
    75
    Wouldn't that just stop the unit from moving, ratehr than stopping the user from giving another command before the movement is over?
     
  5. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Ah yes, I've misread the question D:
     
  6. lrlelaldl

    lrlelaldl

    Joined:
    Jul 27, 2014
    Posts:
    75
    Happens to the best of us!