- Home /
Cubed movement
How can I move a character in a cube-like movement that moves 1 unit when you hold w a s or d?
I have done: function Start () {
for(var n : int; n==0;n=0) {
move.x = Input.GetAxisRaw("Horizontal");
move.y = Input.GetAxisRaw("Vertical");
while(move == Vector3.zero) {
move.x = Input.GetAxisRaw("Horizontal");
move.y = Input.GetAxisRaw("Vertical");
yield;
}
if(move.x > 0 && !Physics.Linecast(transform.position,transform.position+Vector3(1,0,0)))
transform.Translate(1,0,0);
else if(move.x < 0 && !Physics.Linecast(transform.position,transform.position+Vector3(-1,0,0)))
transform.Translate(-1,0,0);
if(move.y > 0 && !Physics.Linecast(transform.position,transform.position+Vector3(0,1,0)))
transform.Translate(0,1,0);
else if(move.y < 0 && !Physics.Linecast(transform.position,transform.position+Vector3(0,-1,0)))
transform.Translate(0,-1,0);
yield WaitForSeconds(1f/moveSpeed);
}
}
But I was wondering if there was another method I could do this, that doesn't require for loops.. (I have this for a 2D game using the X and Y axis)
Answer by sed · Oct 09, 2013 at 10:17 AM
Use the
var amMoving : boolean = false;
function Update()
{
direction.x = Input.GetAxisRaw("Horizontal");
direction.y = Input.GetAxisRaw("Vertical");
if(direction != Vector3.zero && !amMoving) Move(direction);
}
function Move(direction : Vector3)
{
amMoving = true;
var xAxis = Vector3(direction.x, 0, 0).Normalize();
var yAxis = Vector3(0, direction.y, 0).Normalize();
yield StartCoroutine(LinecastAndMove(xAxis));
yield StartCoroutine(LinecastAndMove(yAxis));
amMoving = false;
}
function LinecastAndMove(direction : Vector3)
{
if(direction != Vector3.zero
&& !Physics.Linecast(transform.position,transform.position+direction))
{
transform.Translate(direction);
yield WaitForSeconds(1f/moveSpeed);
}
}
Update function is ment for things that need to execute in every frame (like checking controls for movement).
Thats basically what happens here under the hood, because you have turned your Start function into a Coroutine (which might be helpful to read about also).
I have updated the code. Here is a little generalised version of your code w/o use of 'for' for main game loop.
Yes, but I cannot use yield in update which I need so you move a certain speed with the moving in a grid, I do not want it able to go in between two different grid squares.
Doh! You want the diagonal movement to be possible ins$$anonymous$$d of only the orthogonal directions, right?
@CanCo: Of course you can use Update. While you may not be able to use yield
, you have use the Time.time, or Time.deltaTime values to keep track of time in Update, and then you can only run the movement code after a given time. It's generally not a good idea to create a coroutine in Start to act as your update loop.
Also, one more thing, if you want to make a loop that never ter$$anonymous$$ates, don't use for loops, just write: while(true)
. While loops are better than for loops if you're not using the parameter in the for loop.
Your answer
Follow this Question
Related Questions
Object Squeezing Between Tiles 0 Answers
Megaman Movement 0 Answers
Character doesn't jump repeatedly 1 Answer
Simple movement problem. Need units to stop dead on collision. 3 Answers