- Home /
Rigidbody2D freezes
Hey guys! I'm making a simple 2d character controller to move around a couple of platforms. It works ok despite the different heights of the jump x), but sometimes when you're pressing either A or D, it just freezes on the direction you're moving, and until you move on the opossite direction you can't move again on that one :( . Here's the code :
void FixedUpdate()
{
float movement = Input.GetAxis("Horizontal"); //Take the input of the horizontal axis
if(active)
{
body.velocity = new Vector2(movement * speed, body.velocity.y); //Apply movement and dont change velocity on Y axis
if(Input.GetKeyDown(KeyCode.Space) && grounded)
{
body.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
grounded = false;
}
}
}
To manage the grounded bool is just a OnCollisionEnter2D with the gameObjects with the tag "Tile".
Answer by $$anonymous$$ · May 11, 2015 at 02:50 AM
I am not sure of this one but here what I would have done.
Vector2 pos = new Vector2(0.1f , 0f);
void Update{
if(Input.GetKey(Keycode.A))
player.transform.position-=pos;
if(Input.GetKey(Keycode.D)){
player.transform.position+=pos;
}
}
The logic is simple : you define a vector having 0.1 unit in X-axis.. now when you press A it subtracts 0.1 unit from player's x position and gives it the new position to the left and similarly for pressing D.