- Home /
Question by
Wolf_dead_inside · Jul 27, 2021 at 05:32 PM ·
2dacceleration
2D acceleration code does not work properly
i found some code on the questions forum for this purpose (can be found here) however there are some strange problems with it.
first of all, the character will continue accelerating past top speed. the code looks like it should be able to handle this but it doesnt for some reason. my fixes own have had little success as well.
my second issue is that this method increases speed at a static rate, and id like it to go logarithmically. I could probably figure this out on my own but you guys are smarter than me so i figured id throw this in too to save myself some time
here is code:
if (movementInputDirection == -1 && (speed < maxSpeed) && !(rb.velocity.x == 0))
{
speed = speed - acceleration * Time.deltaTime;
}
else if (movementInputDirection == 1 && (speed > -maxSpeed) && !(rb.velocity.x == 0))
{
speed = speed + acceleration * Time.deltaTime;
}
else
{
if (speed > deceleration * Time.deltaTime)
{
speed = speed - deceleration * Time.deltaTime;
}
else if (speed < -deceleration * Time.deltaTime)
{
speed = speed + deceleration * Time.deltaTime;
}
else
{
speed = 0;
}
}
transform.Translate(new Vector2(speed, 0));
thank you!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613040853im_/https://answers.unity.com/themes/thub/images/avi.jpg)