- Home /
Uneven speed in 2d movement script
I have a very simple 2d game so far and im working on the movement. for some reason my a (left) is much slower than my d (right). Does anyone know why this happens? Also, should i include a forcemode2d? Thanks a lot, heres my code
using UnityEngine;
public class Movement : MonoBehaviour {
public float Speed;
public Rigidbody2D Rb;
public SpriteRenderer SpriteRen;
public Sprite PlayerR;
public Sprite PlayerL;
public Vector2 Coords = new Vector2(1, 1);
void Start()
{
Rb = GetComponent<Rigidbody2D>();
SpriteRen = GetComponent<SpriteRenderer>();
}
void FixedUpdate () {
if (Input.GetKey("a"))
{
Rb.AddForce(Coords * -Speed);
SpriteRen.sprite = PlayerL;
}
if (Input.GetKey("d"))
{
Rb.AddForce(Coords * Speed);
SpriteRen.sprite = PlayerR;
}
}
}
Answer by michaelsivill · Jul 25, 2018 at 09:56 PM
Your Coords Vector2 is translating in both the x and the y coordinates, which will add a force pointing to the upper right when you push d, and a force pushing to the lower left when you push a. The slowness is probably due to the extra frictional force that would be created by pushing your object into the ground.
Try changing Coord to (1,0) That way your force is acting solely along the X axis.
Answer by Gavin_TFI · Jul 25, 2018 at 11:43 PM
@michaelsivill Thanks so much man! It totally worked and not makes a ton of sense. I didnt know how to work the AddForce but i remembered how to use it slightly from a 3d game.