Move a gameobject smoothly without update, without coroutine

Hello,

Been searching for a while but all solutions for moving appear to use a coroutine. I too can successfully get an object to move to a point based on the position of a mouse click using a coroutine.

The problem I’m having is that a coroutine works outside of the update function, so the update can still do it’s thing. Currently this means the user can click on the object, and other objects, to do stuff to the object, while the object is moving.

The behaviour I’m trying to achieve is make the game wait for the movement to complete before allowing the user input or before things can be done to the object or others.

I’ve tried not using a coroutine but the results of the movement are instantaneous. It doesn’t provide linear movement. I’ve tried to use a variable as a means to store the objects state to busy, so as to prohibit other actions during this state, to no avail. The closest I’ve come to the busy variable solution is creating a cycle in the the moveLinear function, whereby I tried to set the Players state to busy in the moveLinear function but this created a cycle as the Player object calls moveLinear through a coroutine.

I may be going about this the wrong way. I’m new to game logic.

Here is the update code for the Player.

function Update () {
	
	if (Input.GetMouseButtonDown (1)) {
		if (isSelected && common.getLocationFromTap() != null)
		{
			var goal : Vector3 = common.getLocationFromTap();
            StartCoroutine(move.moveLinear(object, goal, rateOfMovement));
		}
	}
}

And the moveLinear code

function moveLinear(object: GameObject, goal: Vector3, rateOfMovement: float)
{
    while(true)
    {
    	start = object.transform.position;
        if (start == goal)
    		break;
    	object.transform.position = Vector3.MoveTowards(start,goal,Time.deltaTime*rateOfMovement);
    }
}

I cannot test it since I don’t have all of your code, but this should work:

#pragma strict

private var bMoving = false;

function Update () {
 
    if (!bMoving && Input.GetMouseButtonDown (1)) {
       if (isSelected && common.getLocationFromTap() != null)
       {
         var goal : Vector3 = common.getLocationFromTap();
            StartCoroutine(move.moveLinear(object, goal, rateOfMovement));
       }
    }
}


function moveLinear(object: GameObject, goal: Vector3, rateOfMovement: float)
{
	bMoving = true;
    while(true)
    {
        start = object.transform.position;
        if (start == goal)
           break;
        object.transform.position = Vector3.MoveTowards(start,goal,Time.deltaTime*rateOfMovement);
    }
    bMoving = false;
}

Ah, of course!

I simply replaced the condition “if (!isBusy)” in the players update function with “if (!move.busy)” and it works!

Thanks for the help!