- Home /
4 Way Movement Using Joystick Input
Hi All,
I'm trying to write a C# script for character movement that only allows my character to move in 1 of 4 directions at a time (up, down, left, right). The challenge is that I want this to use joystick input instead of keyboard.
I currently have a script which I found on these forums which works perfectly, except for the fact that it allows 8 way (ie. can move diagonally) movement instead of just 4.
Here is the script I'm currently using. Any tips on how I can update it to restrict diagonal movement would be great.
public class newMove : MonoBehaviour {
private float speed = 1f;
public Transform playergraphic;
// Update is called once per frame
void FixedUpdate()
{
Movement();
}
void Movement()
{
//Player object movement
float horMovement = Mathf.Round(Input.GetAxis("Horizontal"));
float vertMovement = Mathf.Round(Input.GetAxisRaw("Vertical"));
transform.Translate(transform.right * horMovement * Time.deltaTime * speed);
transform.Translate(transform.forward * vertMovement * Time.deltaTime * speed);
//Player graphic rotation
Vector3 moveDirection = new Vector3(horMovement, 0, vertMovement);
moveDirection.Normalize();
if (moveDirection != Vector3.zero)
{
Quaternion newRotation = Quaternion.LookRotation(moveDirection);
playergraphic.transform.rotation = Quaternion.Slerp(playergraphic.transform.rotation, newRotation, Time.deltaTime * 5);
}
}
}
Answer by HenryStrattonFW · Nov 06, 2015 at 12:39 PM
The simplest way I can think of to do this would be to discard one of the inputs.
So, you read in your horizontal and vertical axis as you are now, then you check the magnitude of each value, whichever axis has the largest absolute value gets used, and the other gets set to zero, then you apply those values to your movement.
Hi,
Thanks for the suggestion! I agree with your premise and it's along the same line as what I was thinking.
The challenge I have is that each axis can have an absolute value of 1 or -1, dependant on left/right and up/down. For example, when comparing up and right, I might have the values of 1 and -1. In this case, 1 would win every time, which causes problems.
This is where I'm stuck now. Can't figure out a way to compare 2 numbers regardless of sign (+/-) and get the highest.
Ah, see that's where Absolute comes in. Sorry I should have been clearer, if you do $$anonymous$$athf.Abs(value) then it will return value as exactly the same number if its positive, and if its negative it will return the number as positive as if it didn't have the -ve sign. So $$anonymous$$athf.Abs(-10) returns 10.
This absolute value gives you the magnitude of any number regardless of its sign.
Aha! That's what I was after. Thank you for clarifying your answer. I have other issues with the rotate reverting back when I release the axis trigger now, but that's for another post. Thanks for your help :)
Answer by YasinJavaid_ · Feb 06, 2018 at 09:14 AM
Late but works perfectly for me.
//Update _InputDir = joystick.GetInputDirection(); // Joy Stick input inputX = _InputDir.x; inputY = _InputDir.y; if (Mathf.Abs (inputX) > Mathf.Abs (inputY)) { inputY = 0; } else { inputX = 0; }