(JS) Best way to make it so that a player can only issue a "move click" once?

Hello everyone, I’m pretty new to coding in general, but have a basic grasp on things. As of now, I have it so that you can click on a unit, and then it brings up several buttons. One of these buttons enables the player to “move” the unit by clicking where they want it to go. Everything works fine up to this point.

My issue is that I need to find a way so that when the player issues a move order, they can’t keep moving around (by clicking). I’ve been trying to change isMoving to false (which I should rename to canMove at one point, because that’s pretty much what it’s become) once a move issue is ordered, but that just makes the unit stop halfway through movement. I could really use some help from somebody who actually knows what they’re doing, haha.

Please excuse my awful indentation, I’ve been working on it:

var Selected : boolean;
var isAtkButtonVisible : boolean = false;
var isMvButtonVisible : boolean = false;
var isMoving : boolean = false;

var moveSpeed:float = 20;
private var targetPosition:Vector3;
private var targetDistance:float;

var speed = 3;

var buttonRectangle : Rect = Rect(100, 100, 100, 50);

function Start(){
Selected = false;
isAtkButtonVisible = false;
isMvButtonVisible = false;
targetPosition = transform.position;
}

function OnMouseOver(){
	if(Input.GetMouseButtonDown(0)){
		Selected = true;
		isAtkButtonVisible = true;
		isMvButtonVisible = true;
		
//		Commands();
	}
}

function MoveUnit(){ 
		
		yield WaitForSeconds (0.5);	
		targetDistance = Vector3.Distance(targetPosition, transform.position);
	
	if(targetDistance < 1){
		moveSpeed = 0;
	}
	else if(targetDistance > 1){
		moveSpeed = 5;
	}
	
	if(Input.GetKeyDown(KeyCode.Mouse0) && (isMoving)
	{
		var playerPlane = new Plane(Vector3.up, transform.position);
		var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		var hitdist:float = 0.0;
 
		if (playerPlane.Raycast (ray, hitdist)) {
			targetPosition = ray.GetPoint(hitdist);
		}
	}
 
	if(targetDistance > 1){
		transform.position += (targetPosition - transform.position).normalized * moveSpeed * Time.deltaTime;
	}
	transform.position.y = 1;
}


function Update(){
	if(Input.GetKeyDown(KeyCode.Escape)){
		Selected = false;
		isAtkButtonVisible = false;
		isMvButtonVisible = false;
	}
	
	if(isMoving){
	MoveUnit();
	}
}

  
  
function OnGUI(){
if(isAtkButtonVisible)
{

    if(GUI.Button(Rect(Screen.width/6 - 75,Screen.height/1.1 - 25,150,50),"Attack"))
    {
        isAtkButtonVisible = false;
        isMvButtonVisible = false;
    }
}

if(isMvButtonVisible){
if(GUI.Button(Rect(Screen.width/4,Screen.height/1.1 - 25,150,50),"Move"))
    {
        isMvButtonVisible = false;
        isAtkButtonVisible = false;
        isMoving = true;
    }
  }
}

tl;dr- I need to make it so that a player can only issue a “click move” command once after they click the “move” button (one move command per button click).

Thanks in advance!

I didn’t read all your script stuff in great detail but this should be a fairly simple problem to solve I think.

I like to use a lot of bools in pairs because many times it seems one bool is simply not enough to cover the logic correctly.

So for your problem you may like to try something like this:

bool unitMoveButtonBool;
bool unitMoveActiveBool;

So the unitMoveButtonBool would turn true when you press the move command button, and it would turn false if you cancel the move command (like switching to attack) or deselect the unit before issuing a move location.

The unitMoveActiveBool would turn true when you issue the movement location for your unit and they begin to move.

So while unitMoveActiveBool is true you need to set up your logic so your button press won’t work.

Both bools should have to be false for you to press the movement button, if one of them is true you either already have the movement button waiting for a destination command or your unit is currently moving.

Once the unit reaches their location both the bools will turn back to false allowing you to use the move button again.

So you have one bool to recognize when the button is pressed and ready for a destination command, and another bool to recognize when the unit starts and stops moving.

Whenever the unit is moving you cannot press the button which should solve your problem. It’s just a matter of using the 2 bools correctly within your logic to fully control the unit’s behavior and eliminate any behaviors which you don’t want.

I hope that makes sense I’m not sure if I described that very clearly and I didn’t read your example closely enough to know if you already tried this.

Edit: So after I answered this I was thinking about it and realized that maybe I did not describe the solution for your exact problem.

If you want the player to be able to issue a new movement command while they are moving as long as they press the move button again then you will need to alter the bool conditions.

Make it so that the unitMoveButtonBool turns false once they are moving (right after you select the location) instead of when they stop moving and reach their destination. And you would only need to check if unitMoveButtonBool is false to determine if you can press the move button instead of both the bools.

So you could only issue new movement commands when unitMoveButtonBool is true, and you could only press the move button if unitMoveButtonBool is false.

Hope that makes sense I feel like I’m on the verge of rambling at this point.