- Home /
Simple 2D Character Movement does not work properly
Hey guys! Got a bug i cannot solve in my 2D character movement.
This is my code public class Movement : MonoBehaviour {
public Rigidbody2D rb;
public float speed = 2f;
void Start () {
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate () {
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
Vector2 vel = new Vector2(x * speed, y * speed);
rb.velocity = vel;
}
}
This scripts is active on my character. My character has a Rigidbody2D (dynamic, mass and gravityscale = 1) and a box collider 2D,not changed anything so everything is quite standard.
Now when my movement script is active, my character seems to fall quite too slowly, as soon as i deactive it the character falls quite normale. I cannot see the reason for this behavior, but i am quite new to programming as well, so i do not know too much =D
I`ve tried to use different commands to move the character, but the problem was always the same
Thanks for any kind of help
Answer by OneCept-Games · Jan 02, 2018 at 07:43 PM
If you are using the Physics/Rigidbody, then even in 2D you should not modify the y-value, but just copy it from the object you are about to move, like:
Vector2 vel = new Vector2(x * speed, transform.position.y);
Your answer
Follow this Question
Related Questions
2D Detect collisions of a 2D block only on left/right (not top/bottom) 0 Answers
Problem with 2D movement. My character is moving by himself between two points... :) 1 Answer
How to make slider joint immovable by the player 1 Answer
Creating a delay after being grounded so the player can still jump while falling. 2 Answers