- Home /
2D acceleration code doesnt work properly and needs slight adjustment for my needs
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 increases 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!
Your answer
![](https://koobas.hobune.stream/wayback/20220613040446im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D Animation does not start 1 Answer
2D acceleration code does not work properly 0 Answers
2D Rotate Object With Clamp Gradually. (C#) 2 Answers
2D Collect Item C# Increase Acceleration 2 Answers
acceleration in a 2D game (easy question, probably) 1 Answer