- Home /
Using while statements
Hello guys,
I am facing a little issue at the moment, I am trying to make a player control script witch makes the player move to the touched position, unfortunately the while statement doesn't work when placed within the update function (unity freezes). I was wondering if somebody here could help me with this problem, this is the script I am using right now:
#pragma strict
var pos : Vector2;
var playerposx = GameObject.Find("player2").transform.position.x;
var playerposy = GameObject.Find("player2").transform.position.y;
static var speed : float = 10;
function Start () {
}
function Update() {
pos = Camera.main.ScreenToWorldPoint(Vector2(Input.mousePosition.x, Input.mousePosition.y));
if (Input.mousePosition.x > (Screen.width/2)) {
while (Input.mousePosition.y > playerposy)
{
rigidbody2D.velocity.y = speed;
}
while (Input.mousePosition.y < playerposy)
{
rigidbody2D.velocity.y = speed * -1;
}
while (Input.mousePosition.x < playerposx)
{
rigidbody2D.velocity.x = speed * -1;
}
while (Input.mousePosition.x > playerposx)
{
rigidbody2D.velocity.x = speed;
}
}
}
The question is, how do I make this script work?
I think the problem is that the while statements ARE working. Each of those whiles will cause it to pause until the the condition isn't met. But at least one of them will be met whenever the mouse is somewhere other than (playerposx, playerposy), Replacing all the whiles with ifs is probably the answer.
if(Input.mousePosition.y > playerposy)
{
rigidbody2D.velocity.y = speed;
}
else if(Input.mousePosition.y < playerposy)
{
rigidbody2D.velocity.y = speed * -1;
}
else if(Input.mousePosition.x < playerposx)
{
rigidbody2D.velocity.x = speed * -1;
}
else if(Input.mousePosition.x > playerposx)
{
rigidbody2D.velocity.x = speed;
}
@gcoope Two issues with that... First, you probably don't want movement in y direction to prevent movement in x direction, so the second else is unwanted. Second, the first and third elses are superfluous because a value cannot be simultaneously greater and smaller than another value (it may improve efficiency by bypassing a test you know is going fail, but it won't change the functionality).
If yo write it like that it surely will. Now it's only moving the x coordinate if the y coordinate matches with the mouse position.
You need to remove at least the 2nd "else"
// move on y axis if not at cursor position
if(Input.mousePosition.y > playerposy)
{
rigidbody2D.velocity.y = speed;
}
else if(Input.mousePosition.y < playerposy)
{
rigidbody2D.velocity.y = speed * -1;
}
// move on x axis if not at cursor position
if(Input.mousePosition.x < playerposx)
{
rigidbody2D.velocity.x = speed * -1;
}
else if(Input.mousePosition.x > playerposx)
{
rigidbody2D.velocity.x = speed;
}
It should make sense if you just "read it aloud"
If cursor is higher than player, move up. If not (else), and if cursor is below player move down...
@NDPW Can you explain further? Satisfying one of the ifs shouldn't stop it checking subsequent ones (unless you use else if
as in @gcoope's comment). Try simply replacing the every while
with if
(or you can use else if
but only to bypass tests you know are going to fail... in other words where @Nose$$anonymous$$ill's comment shows them)
Your answer
Follow this Question
Related Questions
Variables in GUI not updating/changing? (javascript) 1 Answer
WaitForSeconds in Update() function 4 Answers
Getting updated variables from another script 2 Answers
Script is causing immense lag, and I don't know what's causing it. 2 Answers
A while type statement in the Update function, Yield? 1 Answer