- Home /
How to prevent diagonal movement on 2d-rigidbody script?
Hello!
I have this script, attached to a simple cube player, and it is working well, detecting collision and objects (thanks to rigid body). Problem is that i cannot manage to let him move "just" one direction.. I mean, if i press another key while moving, the movement is diagonal, instead i would like to have him stopping one direction and only enabling the new one.
If someone can help me, i would really appreciate!
#pragma strict
//how fast the player walks
var walkSpeed:float = 14.0;
function Update () {
if(Input.GetKey("a") || Input.GetKey("d") || Input.GetKey("left") || Input.GetKey("right"))
{
if(Input.GetKey("a") || Input.GetKey("left"))
{
if(rigidbody.velocity.x > 0)
{
rigidbody.velocity.x = 0;
}
if(rigidbody.velocity.x > -walkSpeed)
{
rigidbody.velocity.x -= 48*Time.deltaTime;
}
}
if(Input.GetKey("d")|| Input.GetKey("right"))
{
if(rigidbody.velocity.x < 0)
{
rigidbody.velocity.x = 0;
}
if(rigidbody.velocity.x < walkSpeed)
{
rigidbody.velocity.x += 48*Time.deltaTime;
}
}
}
else
{
rigidbody.velocity.x = 0.0;
}
if(Input.GetKey("w") || Input.GetKey("s") || Input.GetKey("up") || Input.GetKey("down"))
{
if(Input.GetKey("s") || Input.GetKey("down"))
{
if(rigidbody.velocity.z > 0)
{
rigidbody.velocity.z = 0;
}
if(rigidbody.velocity.z > -walkSpeed)
{
rigidbody.velocity.z -= 48*Time.deltaTime;
}
}
if(Input.GetKey("w")|| Input.GetKey("up"))
{
if(rigidbody.velocity.z < 0)
{
rigidbody.velocity.z = 0;
}
if(rigidbody.velocity.z < walkSpeed)
{
rigidbody.velocity.z += 48*Time.deltaTime;
}
}
}
else
{
rigidbody.velocity.z = 0.0;
}
}
Ps: i'm learning Unity and C#, though i found the script among a js compilation... If someone could help me translate this also, it would be a really nice favor!
Answer by Fattie · Sep 27, 2013 at 03:55 PM
Just click the button in the inspector ..
look at the rigidbody in the Inspector in the editor.
Which button? Sorry, i'm quite newbie, cannot catch this... Is there a simple solution? Thanks
Look at unity, look at inspector, look at rigidbody, look at CONSTRAINTS.
awesome eh !
Yeah, awesome! I understood that i should (maybe?) mark all rotations + position.x/position.z depending on what it is happening. But i cannot get well that bitwise thing, considering, that rotations are all locked by default, have i to write some code like this?
if (Get$$anonymous$$eyDown("a") || Get$$anonymous$$eyDown("d"))
rigidbody.constraints = RigidbodyConstraints.FreezePositionY;
if (rigid body.velocity.x == 0)
rigidbody.constraints = RigidbodyConstraints.None;
is it correct? Thanks again Fattie!
I got the process you were referring to, thanks a lot, i managed to have a nice movement controller!
Answer by robertbu · Sep 27, 2013 at 02:40 PM
You can put lines 42 - 67 just below line 40 in the 'else' clause. This will eliminate diagonal movement, but it gives dominance to horizontal movement. That is if a vertical and a horizontal key are pressed, your character will move horizontally.
It is fairly complex to recode this so that the first key (horizontal or vertical) is what is done. You can switch the logic and embed the horizontal in the else clause of the vertical if you wanted to give vertical preference.
Thanks for your help, it is surely a starting. I tried different solutions (i'm a newbie, so i went on without too much knowledge!) as using a boolean value, repeating rigid body.velocity lines, but nothing... Do you have any suggestion to achieve that result? A method i could try myself to set up, or you think is too complex? Thanks again!
I did not read your question/code carefully enough. In addition to the changing the key code, you will need to zero out the movement on the other axes. For example, when processing a vertical key (z), set velocity.x = 0;
Thanks however robertbu, i appreciated a lot your help even if i used Fattie method!
Your answer
Follow this Question
Related Questions
4 Direction Ball Movement 1 Answer
Disable diagonal movement in 3D 1 Answer
Touch movement not working 1 Answer
How to let a GameObject generate force but not be affected by certain forces? 0 Answers
Rigidbody climbing stairs 2 Answers