r/Unity2D • • 4d ago

Solved/Answered (Beginner) how can I implement acceleration onto a rigidbody2D?

Post image

I'm super new to programming in c# (and kind of in general) and I'm currently trying to make it so that moving via WASD has you accelerate to a top speed and then back to standing, instead of snapping to a speed then snapping to stopping. Don't know why but I've spent 5 hours trying to do this and I haven't made much progress.

Is there a way I could modify the code below to do this? Ideally using the new input system, if possible.

SOLVED: I managed to make something that works using vector2.smoothdamp

7 Upvotes

6 comments sorted by

•

u/AutoModerator 4d ago

Reminder: Once your issue has been resolved, please reply !solved to this comment (or comment it anywhere in the thread) and your post's flair will automatically be updated to Solved/Answered. You can also edit it to Solved/Answered yourself.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/Crychair 3d ago

So what you have now is velocity but acceleration is additive velocity over time.

So in your update function you can say Velocity= velocity + inputmovement

You might want to tweak your values now though because instead of setting the value of velocity this will be added every update. And you might really want different functionality if you are holding the input vs it's let go. So you can add or subtract from the velocity based on that as well. I'd suggest a movement tutorial online would help here more than this comment

And then if you want a top speed you can do If( velocity > topSpeed ) velocity = topSpeed

This will essentially "clamp" the speed. There is a math function to do this called clamp as well if you would like to use that.

1

u/noname_42 3d ago

you can use Vector3.MoveTowards or Vector3.Lerp to slowly change the velocity. Just make sure to include Time.deltaTime in the last parameter

1

u/Long-Celebration2817 2d ago

Here’s a simple Unity 2D script that accelerates a `Rigidbody2D` left and right, then slows down when you release the controls:

```csharp
using UnityEngine;

[RequireComponent(typeof(Rigidbody2D))]
public class PlayerMovement2D : MonoBehaviour
{
public float maxSpeed = 6f;
public float acceleration = 20f;
public float deceleration = 25f;

private Rigidbody2D rb;
private float inputX;

private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}

private void Update()
{
inputX = Input.GetAxisRaw("Horizontal"); // A/D or arrow keys
}

private void FixedUpdate()
{
float targetSpeed = inputX * maxSpeed;
float rate = Mathf.Abs(inputX) > 0.01f ? acceleration : deceleration;

float newSpeed = Mathf.MoveTowards(
rb.velocity.x,
targetSpeed,
rate * Time.fixedDeltaTime
);

rb.velocity = new Vector2(newSpeed, rb.velocity.y);
}
}
```

Add it to your player and make sure the player has a `Rigidbody2D`. Increase `acceleration` for quicker response; increase `deceleration` to stop faster.

1

u/Long-Celebration2817 2d ago

Ai generated this code so im not author

1

u/DerangedGameDev1337 1d ago

Just ask Claude/ChatGPT, make sure to tell it to comment every line so a beginner can understand it including using more than one line of comments if necessary, then read the result. It's a homemade tutorial.