move toward in c#

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lulavin

so, i tried a heartbeast tutorial, and instead of doing it in gdscript like a normal non braindead human being, i decided to do it in c#. i got stuck on the second video where acceleration is used for a character because it says that the function MoveToward does not exist. here’s what i tried:

public void applyAcceleration(float amount) {
    velocity.x = MoveToward(velocity.x, 50 * amount, 20);

and the code in the video was:

func apply_acceleration(amount):
velocity.x = move_toward(velocity.x, amount * 50, 20)

thank you in advance.

:bust_in_silhouette: Reply From: juppi

MoveToward is a method of Vector2:

i already know that, thank you. i want to know how the code above should look like in c#.

lulavin | 2022-08-16 13:29

:bust_in_silhouette: Reply From: Galrent

I know you’ve probably moved on from this at this point, but I figure it could help someone in the future.

The Mathf library has a ‘MoveToward’ function that is based on a float instead of a vector. Hopefully that helps!

1 Like

just add Mathf

Mathf.MoveToward(velocity.X, 50 * amount, 20);