- Home /
8 direction walking not working...
So, I was attempting to make an 8 direction walking, it doesn't work like it's supposed to. here's my code
function Update () {
var VeIn = Input.GetAxis("Vertical");
var HoIn = Input.GetAxis("Horizontal");
if(VeIn > 0 && HoIn == 0){
Move = MoveType.Up;
}
else if(VeIn > 0 && HoIn > 0){
Move = MoveType.UpRight;
}
else if(VeIn == 0 && HoIn > 0){
Move = MoveType.Right;
}
else if(VeIn < 0 && HoIn > 0){
Move = MoveType.RightDown;
}
else if(VeIn < 0 && HoIn == 0){
Move = MoveType.Down;
}
else if(VeIn < 0 && HoIn < 0){
Move = MoveType.DownLeft;
}
else if(VeIn == 0 && HoIn < 0){
Move = MoveType.Left;
}
else if(VeIn > 0 && HoIn < 0){
Move = MoveType.LeftUp;
}
else{
Move = MoveType.None;
}
switch (Move){
case MoveType.None:
CurrentSpeed = 0;
break;
case MoveType.Up:
transform.rotation.y = 45;
CurrentSpeed = MoveSpeed * Time.deltaTime;
break;
case MoveType.UpRight:
transform.rotation.y = 90;
CurrentSpeed = MoveSpeed * Time.deltaTime;
break;
case MoveType.Right:
transform.rotation.y = 135;
CurrentSpeed = MoveSpeed * Time.deltaTime;
break;
case MoveType.RightDown:
transform.rotation.y = 180;
CurrentSpeed = MoveSpeed * Time.deltaTime;
break;
case MoveType.Down:
transform.rotation.y = 225;
CurrentSpeed = MoveSpeed * Time.deltaTime;
break;
case MoveType.DownLeft:
transform.rotation.y = 270;
CurrentSpeed = MoveSpeed * Time.deltaTime;
break;
case MoveType.Left:
transform.rotation.y = 315;
CurrentSpeed = MoveSpeed * Time.deltaTime;
break;
case MoveType.LeftUp:
transform.rotation.y = 0;
CurrentSpeed = MoveSpeed * Time.deltaTime;
break;
}
print("Move = " + Move);
transform.Translate(0,0,CurrentSpeed);
}
well, when i press left and up together, it works, and down and right works, everything else is just weird.....
so well, if somebody could help, i would greatly appreciate it!
--LockonS483
Answer by syclamoth · Jan 31, 2012 at 05:29 AM
Wouldn't it be simpler to just do this?
var moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Horizontal"));
transform.LookAt(moveDirection);
if(moveDirection.magnitude > 1)
{
moveDirection = moveDirection.normalized;
}
transform.Translate(0, 0, MoveSpeed * Time.deltaTime * moveDirection.magnitude);
Your code just seems to be absurdly complicated for such a simple task. There are at least 4 problems with it just from looking- the least of which is attempting to manually assign to transform.rotation. Transform.rotation is a Quaternion- it doesn't work the way you seem to think it does.
If you don't like the way the axes get smoothed, you can fix that by using 'Input.GetAxisRaw(string)' This way, they will act as simple binary switches, ins$$anonymous$$d of faking joystick behaviour.
so, thanks for the reply, but that does not seem to be much of a 8-direction moving to me. I am not the most experienced person in the world so, i would like to ask, how do i change an objects rotation if not by "transform.rotation.y = x"? I would greatly appreciate an answer thank you again for your reply!
--LockonS
Like I said, tranform.rotation.y does not do what you think it does. transform.rotation isn't stored as a 3-axis rotation, it's a 4-axis quaternion!
In any case, did you even try the code I gave you? The 'transform.LookAt(direction)' is the part that rotates the character. And it rotates in 8 directions, because that is the number of combinations of the two axes, converted into a Vector2. Just because you don't understand what a bit of code does, doesn't mean it doesn't work.
well, thanks for your suggestion, i have fixed the problem, though. i used Waypoints that always follow the player, and turn to a specified waypoint whenever it needs to. Thanks for your help anyway, because i did not know that you could not change transform.rotation.y :P. Thank you again for your response!
Your answer
![](https://koobas.hobune.stream/wayback/20220613063556im_/https://answers.unity.com/themes/thub/images/avi.jpg)