Week 24: Rotating Up and Down
Back

Early in development I realised that while my movement worked just fine on flat surfaces, the same could not be said about moving on slopes. This particular problem stumped me for the longest time and I was extremely happy when I figured it out.

Now this will require some specific language that might be hard to understand, but I’ll try my best to explain it. My character moves in whatever direction the Vector 3 connected to his movement tells him to go. A Vector3 is essentially a piece of data that contains X,Y and Z coordinates. This means that my script essentially takes where the character is, looks at which direction the Vector is telling it to move to and moves it.

With a joystick, it is extremely easy to give the character X and Z values. If the stick is pushed up, you will ad Z values to the Vector. If it’s pushed to the left or right, you will ad values to the X value in the Vector. However, as you might have noticed by now, this does not consider Y values at all.

Essentially, what this means, is that when my character walked up or down slopes, the movement vector was always straight. This caused him to detach from the ground when going down a slope and walking straight into one when going up.

To fix this, one must understand what a normal is. Essentially a normal is a Vector that will always point in the opposite direction from the ground a character is standing on.

https://answers.unity.com/storage/temp/12218-normal.png

If a character is standing on even ground, the normal will always point up. If they’re standing on a slope, the normal will always point opposite the direction the slope is facing.

Why are normals important? Because we can essentially use them to rotate our character depending on their values, and by doing so, we also end up rotating our movement vector, allowing the character to traverse slopes without problems.

Back