Hi Ive been trying to get my character to have a “dash” ability to move backwards and cant seem to get it to work.
(I am a high school student with no computer science background so I apologize if i make some easy errors)
so Ive tried some things such as these and tips on how to make it work?
rb.MovePosition(gameObject.transform.position - (Vector3.back * 5));
which got it moving 5 units in a set direction, but not sure how to make this in the “back” direction.
Vector3.back is “back” according to world coordinates. You want “back” according to your object’s coordinates. Try:
rb.MovePosition(gameObject.transform.position - (gameObject.transform.forward * 5));
The easiest way to do this is to use one of the inbuilt functions of the transform class.
Have a read over this: Unity - Scripting API: Transform
In the variables section you can see that we have forward, (written as transform.forward) which will get you the forward direction based on the Z axis (the blue arrow if you look at it through scene view). Now since there’s no backward property, using -transform.forward will give you what you’re looking for.
You should also note that using rigidbody’s MovePosition will instantly move your character backwards, as if they’ve teleported. This may be what you want, but if not I suggest you use rb.AddForce instead and then play around with the numbers to get the desired result.