armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

• 0

# How can I determine if I'm moving up/down a slope?

## Question

So I have the math done already for the movement on the slope but I only wan't to call DescendSlope() if I am actually moving down the slope.

I have the normal of the slope and the player's move direction. These should be enough to determine if I am moving up/down the slope I think.

I have tried getting the Sign of these vectors respectively and comparing them, like so:

The problem with this is that the sideways direction of the player (can be x or z depending on the rotation) is in one case not the same as the slope's normal direction.

Basically the if statement is not true when I'm strafing. Do I have to do something with the Please login or register to see this link.  or the Please login or register to see this link.  product of these directions?

Edited by UnityGamesRoland

## Recommended Posts

• 0

By comparing the flat direction of the hit normal (x, 0, z) with the flat direction of the player. I see people using crosses and atans all the time for stuff like this but if your constantly calculating these values, your gonna killing peoples computers. Flat vectors are cheap and give you the same outcome

edit: I.e. If the player is moving in the direction of the normal flat vector, your going downhill, if it's going in the opposite, your going up, if your going at 90 degrees (0.5) your going across the slope.

##### Share on other sites

• 0

Woah that is definitely some serious unneeded math going on. All you need is: Vector3.angle(Vector3(direction.x, 0, direction.z), hit.normal). That will compare the angle between which way the playing is moving and the direction of the hit normal. No need for all those sign calculations

##### Share on other sites
• 0

I don't get it, how does this help me tell if the player is moving up or down the slope?

##### Share on other sites
• 0

Ah man I spent like 15 minutes staring at the code trying to figure out why don't I get consistent values, and then I realized that I accidentally made the vector with the Z value being the Y of the direction...

Got it working, thanks @OcularCash

##### Share on other sites
• 0

Np. I get turned around sometimes as well when i spend a lot of time blender and go back into unity since there y and z is flip flopped. Good to hear you got it working

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.