- Home /
Best way to move a 2D Character
I was just wondering what everyone thought was the best method to move a 2D Character? For years I've always done something like:
if(Input.GetKey(KeyCode.D))
{
transform.position += Vector3.right * moveSpeed * Time.deltaTime;
spriteRenderer.flipX = false;
}
else if(Input.GetKey(KeyCode.A))
{
transform.position += Vector3.right * -moveSpeed * Time.deltaTime;
spriteRenderer.flipX = true;
}
Though I've also seen a lot of people simply change the characters velocity manually to move them.
Is my method of moving a 2D character valid or should I be doing it another way?
This is a very bad way of moving a character, it does not provide physics detection, which isn't costly. Velocity is the absolute best way of moving a character. Also using keys is a bad way to go unless you create your own key-mapping system. Proper movement would look like this.
public float speed;
public float jumpSpeed;
public body Rigidbody;
private float vertical;
private float horizontal;
private Vector3 movement;
void Awake()
{
movement = new Vector3;
if (body == null)
body = GetComponenent<Rigidbody>();
}
void FixedUpdate()
{
vertical = Input.GetAxis("Vertical");
horizontal = Input.GetAxis("Horizontal");
movement.x = horizontal * speed;
movement.y = vertical * jumpSpeed;
body.velocity = movement;
}
EDIT: Unfortunately since Vector3 is a struct, it is probably more efficient to make a new Vector3 every update. As horrible as it sounds. Like
movement = new Vector3(horizontal * speed, vertical * jumpSpeed, 0);
Using Input.GetAxis gives you more control over multi-platform inputs and keyboard/joysticks input damping, etc..
Although, assigning the velocity property is really not advisable, it would cancel gravity and momentum every frame!
You want to use AddForce() and AddRelativeForce().
Answer by UnityCoach · May 06, 2017 at 11:45 AM
When you're not using Physics, you can either assign the Transform.position property or use the Transform.Translate() method. The latter is preferred though, as it allows you to use Local VS World coords.
When using Physics, it's the same, you can assign the Rigidbody2D.velocity property, of use the Rigidbody2D.AddForce() or Rigidbody2D.AddRelativeForce() methods. Again, the latter is preferred for the same reasons.
I don't understand the world vs local coordinates. Isn't there just one scene. That could be just me touting my ignorance however. Thanks for your answer about force for movement @UnityCoach!
Answer by Retropaint · Jan 14, 2018 at 03:46 PM
I don't think the difference between choices is big, thought if I were u I would write it like this:
if(Input.GetButton("GoRight")
{
transform.position += Vector3.right * moveSpeed * Time.deltaTime;
spriteRenderer.flipX = false;
}
else if(Input.GetButton("GoLeft"))
{
transform.position += Vector3.right * -moveSpeed * Time.deltaTime;
spriteRenderer.flipX = true;
}
If you don't know what GetButton is, read about the Input Manager in the Unity manual. The manager allows the play to assign their own buttons rather than making it hardcoded (like how you did with "KeyCode.A" and "KeyCode.D"). For example, the player might want F to "GoRight" and S to be "GoLeft".
Your answer