- Home /
Disable diagonal movement on 2D character controller
Hey,
I am trying to disable diagonal movement in a really simple 2D character controller. I have been trying all sorts of things with no real luck at all.
Basically, when two movement keys (i.e. "w" & "d") are pressed at the same time I want the controller to ceither ontinue along the axis of whichever button was pressed first or just not move at all. Looking for something similar to 2D grid based movement without it being grid based.
Any help would be appreciated greatly.
Thanks!
var speed : float = 15;
function Update () {
if(Input.GetButton("Vertical")) {
transform.Translate(Vector3(0,speed,0) * Time.deltaTime);
}
if(Input.GetButton("Horizontal")) {
transform.Translate(Vector3(speed,0,0) * Time.deltaTime);
}
if(Input.GetButton("-Vertical")) {
transform.Translate(Vector3(0,-speed,0) * Time.deltaTime);
}
if(Input.GetButton("-Horizontal")) {
transform.Translate(Vector3(-speed,0,0) * Time.deltaTime);
}
}
If you put an 'else' in front of your last three 'if' statements, you will eli$$anonymous$$ate diagonal movement. Note the preference will be in the order of the 'if' statements. To capture "whichever button was pressed first" would be far more complicated.
@robster - what you're saying would work of course (as would many other changes). But don't you agree that what s/he "meant" was to have returns in there.
It's such a common mistake for beginners. Sometimes I think languages should just breakaway after an if !
Answer by Fattie · Sep 18, 2013 at 09:04 AM
What you are doing here is writing
breakaway code.
.
Quite simply, you forgot the "return" inside each clause. That's what you "meant" when you were thinking about it, you just forgot to type the "return" calls. It is an exceptionally common beginner mistake.
(Note that there are huge philosophical issues relating to whether one should write "breakaway code" and for people who do things like "think of algorithms" it is a topic of obsession. Furthermore your whole approach to picking up buttons like that may be done in utterly different and perhaps better ways. But the fact is in the situation at hand you simply "forgot the returns" - that's what you meant in your head, you wanted it to do one of those and that's it - right?)
Hope it helps.
function Update()
{
if(Input.GetButton("Vertical"))
{
transform.Translate(Vector3(0,speed,0) * Time.deltaTime);
return;
}
if(Input.GetButton("Horizontal"))
{
transform.Translate(Vector3(speed,0,0) * Time.deltaTime);
return;
}
if(Input.GetButton("-Vertical"))
{
transform.Translate(Vector3(0,-speed,0) * Time.deltaTime);
return;
}
if(Input.GetButton("-Horizontal"))
{
transform.Translate(Vector3(-speed,0,0) * Time.deltaTime);
return;
}
}
Thanks Fattie, that is indeed what I was thinking. You were a great help!
I'm sure once I learn more I'll try some different ways to create controllers.
Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613114349im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Modify movement script for continuous movement 1 Answer
Passing through walls. 2d toolkit 1 Answer
What is Happening here? please help. 0 Answers
(2d - top down) Characters drifting upon movement,Characters drifting upon movement (2d) 1 Answer
How to use multiple bone rigged sprites for 2d character animations 0 Answers