- Home /
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);
}
}
Answer by eaj · Mar 17, 2013 at 01:54 AM
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!
Answer by robertbu · Mar 16, 2013 at 11:44 PM
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;
}
Hi, thanks for the prompt response!
The moveLinear function is a member of the movement script so it doesn't have access to the Players' b$$anonymous$$oving variable.
For clarity, the player script is called Player and the movement script is called $$anonymous$$oveController.
Here is the Player script
#pragma strict
var common: Common;
var move: $$anonymous$$oveController;
var object : GameObject;
var isSelected: boolean = false;
var isBusy: boolean = false;
var rateOf$$anonymous$$ovement: float = 0.0001;
var health: float = 100;
function Awake () {
object = gameObject;
}
function Update () {
if (!isBusy)
{
// toggle selection
if (Input.Get$$anonymous$$ouseButtonDown (0))
{
if (common.getObjectNameFromTap() == object.name)
isSelected = true;
else
isSelected = false;
}
// move selected object
if (Input.Get$$anonymous$$ouseButtonDown (1))
{
if (isSelected && common.getLocationFromTap() != null)
{
var goal : Vector3 = common.getLocationFromTap();
StartCoroutine(move.moveLinear(object, goal, rateOf$$anonymous$$ovement));
playerSelected(false);
}
}
}
}
And this is the movement script
#pragma strict
public class $$anonymous$$oveController {
@HideInInspector
var start: Vector3;
var busy: boolean = false;
function moveLinear(object: GameObject, goal: Vector3, rateOf$$anonymous$$ovement: float)
{
while(true)
{
start = object.transform.position;
if (start == goal)
break;
object.transform.position = Vector3.$$anonymous$$oveTowards(start, goal, Time.deltaTime*rateOf$$anonymous$$ovement);
yield;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613094538im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
C# yield not working. 2 Answers
Help with waiting / coroutine c# 1 Answer
Make a wait function 1 Answer
How to properly use Yield for a coroutine? 3 Answers
Wait inside method until user action 3 Answers