- Home /
How to make my 2D Character roll
I'm trying to write a script to make my 2D character roll to a side, I am using Addforce for this, as I would do when jumping, but i can't make the rolling feel right. If I use a small amount of force, it barely moves but if I use a greater force it makes it look like I am teleporting. So my question how can I go about making it feel smoother?
the code I wrote is really simple and I'm not worried about the conditions of when to roll, I just want to make the rolling better. A quick nodge in the right direction would be nice, thanks.
public class PlayerControl : MonoBehaviour {
public float maxSpeed = 10f;
public float dodgeForce = 300f;
private Rigidbody2D body;
void Start () {
body = GetComponent<Rigidbody2D>();
}
void Update () {
if (Input.GetKeyDown(KeyCode.LeftShift)){
if (body.velocity.x > 0)
body.AddForce(new Vector2(dodgeForce,0));
else
body.AddForce(new Vector2(-dodgeForce,0));
}
}
void FixedUpdate(){
float movex = Input.GetAxis ("Horizontal");
float movey = Input.GetAxis ("Vertical");
body.velocity = new Vector2 (movex * maxSpeed, movey * maxSpeed);
}
}
Answer by SquarePieStudios · Apr 27, 2015 at 08:53 AM
The reason for this is because you're adding a whole second's worth of force each frame. Multiply dodgeForce, movex, and movey by Time.deltaTime and I think you'll see the results you're looking for.