Question by
meksikietis222 · Nov 28, 2018 at 04:53 PM ·
2dmovement2d gametopdown
Adding small acceleration to 2d top down movement game?
Hello, how to add small acceleration when player starts moving but instantly stops if button is realeased? My current code without acceleration: { Rigidbody2D body; float horizontal; float vertical; float moveLimiter = 0.7f; public float runSpeed = 20;
void Start()
{
body = GetComponent<Rigidbody2D>();
}
void Update()
{
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
}
void FixedUpdate()
{
if (horizontal != 0 && vertical != 0)
{
body.velocity = new Vector2((horizontal * runSpeed) * moveLimiter, (vertical * runSpeed) * moveLimiter);
}
else
{
body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);
}
}
}
Comment