Jump to content
This site uses cookies! Learn More

This site uses cookies!

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
Sign in to follow this  
UnityGamesRoland

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:

Please login or register to see this code.

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?

 

Thanks in advance!

Edited by UnityGamesRoland

Share this post


Link to post
Share on other sites

5 answers to this question

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.

  • Thanks 1

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

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

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...