- Home /
This post has been wikified, any user with enough reputation can edit it.
How to disable diagonal movement?
I am trying to disable diagonal movement in unity 2D but it doesn't work.
In this code, what is problem?
void Update () {
if (Input.GetKey (KeyCode.D)) {
East = true; West = false; South = false; North = false;
Direction ();
//xMove = moveSpeed * Time.deltaTime;
//yMove = 0 * moveSpeed * Time.deltaTime;
//this.transform.Translate (xMove, yMove, 0);
//yMove = 0 * moveSpeed * Time.deltaTime;
//public Vector3 UpWalk=transform.Translate.position(new Vector3(moveSpeed*Time.deltaTime,0.0,0.0));
animator.Play ("Right_Move");
} else if (Input.GetKey (KeyCode.A)) {
East = false; West = true; South = false; North = false;
Direction ();
//xMove = -moveSpeed * Time.deltaTime;
//yMove = 0 * moveSpeed * Time.deltaTime;
//this.transform.Translate (xMove, yMove, 0);
//yMove = 0 * moveSpeed * Time.deltaTime;
//public Vector3 =transform.Translate.position(new Vector3(-moveSpeed*Time.deltaTime,0.0,0.0));
animator.Play ("Left_Move");
}
if (Input.GetKey (KeyCode.W)) {
East = false; West = false; South = false; North = true;
Direction ();
//xMove = 0 * moveSpeed * Time.deltaTime;
//xMove = 0 * moveSpeed * Time.deltaTime;
//yMove = moveSpeed * Time.deltaTime;
//this.transform.Translate (xMove, yMove, 0);
//transform.Translate.position(new Vector3(0.0,moveSpeed*Time.deltaTime,0.0));
animator.Play ("Up_Move");
} else if (Input.GetKey (KeyCode.S)) {
East = false; West = false; South = true; North = false;
Direction ();
//xMove = 0 * moveSpeed * Time.deltaTime;
//xMove = 0 * moveSpeed * Time.deltaTime;
//yMove = -moveSpeed * Time.deltaTime;
//this.transform.Translate (xMove, yMove, 0);
//transform.Translate.position(new Vector3(0.0,-moveSpeed*Time.deltaTime,0.0));
animator.Play ("Down_Move");
}
/*if (Input.GetKey (KeyCode.A) && Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.D) && Input.GetKey (KeyCode.W)) {
animator.Play ("Up_Move");
} else if (Input.GetKey (KeyCode.A) && Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.D) && Input.GetKey (KeyCode.S)) {
animator.Play ("Down_Move");
}*/
if (Input.GetKeyUp (KeyCode.D) || Input.GetKeyUp (KeyCode.A) || Input.GetKeyUp (KeyCode.W) || Input.GetKeyUp (KeyCode.S)) {
xMove = 0 * moveSpeed * Time.deltaTime;
yMove = 0 * moveSpeed * Time.deltaTime;
this.transform.Translate (xMove, yMove, 0);
//transform.Translate.position(new Vector3(0,0,0));
animator.Play ("Playeridle");
}
/*if(Input.GetKey(KeyCode.A)&&Input.GetKey(KeyCode.S)||Input.GetKey(KeyCode.A)&&Input.GetKey(KeyCode.W)||Input.GetKey(KeyCode.D)&&Input.GetKey(KeyCode.S)||
Input.GetKey(KeyCode.D)&&Input.GetKey(KeyCode.W)){
//this.transform.Translate (xMove,yMove,0);
xMove = 0 * moveSpeed * Time.deltaTime;
yMove = 0 * moveSpeed * Time.deltaTime;
this.transform.Translate (xMove,yMove,0);
animator.Play ("PlayerIdle");
}*/
}
void Direction(){
if (West == true) {
xMove = -moveSpeed * Time.deltaTime;
yMove = 0 * moveSpeed * Time.deltaTime;
this.transform.Translate (xMove, yMove, 0);
} else if (East == true) {
xMove = moveSpeed * Time.deltaTime;
yMove = 0 * moveSpeed * Time.deltaTime;
this.transform.Translate (xMove, yMove, 0);
} else if (South == true) {
xMove = 0 * moveSpeed * Time.deltaTime;
yMove = -moveSpeed * Time.deltaTime;
this.transform.Translate (xMove, yMove, 0);
} else {
xMove = 0 * moveSpeed * Time.deltaTime;
yMove = moveSpeed * Time.deltaTime;
this.transform.Translate (xMove, yMove, 0);
}
}
Comment
Answer by bobisgod234 · Apr 28, 2017 at 06:11 AM
You are calling Direction() twice in an update.
Your code in update, greatly simplified, looks like this:
if (Input.GetKey (KeyCode.D)) {
Direction ();
} else if (Input.GetKey (KeyCode.A)) {
Direction ();
}
if (Input.GetKey (KeyCode.W)) {
Direction ();
} else if (Input.GetKey (KeyCode.S)) {
Direction ();
}
So if you hold D and W at at the same time, it will call Direction() and move east, then it will call Direction() again and move north. Try changing
if (Input.GetKey (KeyCode.W)) {
to an else if
else if (Input.GetKey (KeyCode.W)) {
Your answer
![](https://koobas.hobune.stream/wayback/20220612120827im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to move rigidbody Diagonal with movePosition 3 Answers
How can I draw the diagonals of a screen? 2 Answers
Diagonal Movement 1 Answer
How do I move an object along an ellipse path on diagonal? 2 Answers