Jump to content
  • Advertisement
Sign in to follow this  

How to find the downward slope of a plane in 3D space

This topic is 2039 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was programming a game in Unity using C# where the character needs to start sliding, which would change the controls when the slope is too steep. I did a few searches on the web, and I couldn't find a thing. So, I dug out my Calculus book, and looked up min/max points on a multidimension equation, when I realized that the information that I get from the object is a plane, and that I should be using the equation of a plane to solve this problem.

Here's what I came up with:

// The variables
private RaycastHit groundBelow;     // stores the raycast information
private Vector3 downwardDirection;  // stores the vector information of the downward slope


// This function sets the downwardDirection to a normalized vector down the slope.
// (In Unity the y-axis is the vertcal component)

if(Physics.Raycast(transform.position, Vector3.down, out groundBelow))


    downwardDirection = new Vector3(groundBelow.normal.x,

           (groundBelow.normal.x * groundBelow.normal.x + groundBelow.normal.z * groundBelow.normal.z) /
                                            -groundBelow.normal.y ,


How it works:

Using the Physics.Raycast() function takes the postion where the ray is cast from, the direction that the ray is cast, and the variable which stores the raycast information. The RaycastHit variable type contains the normal vector of the first plane it hit. So how does that help? Well, the equation of a slope that contains the origin is:

This isn't the complete equation that takes into account the planes that don't pass through the point  (0,0,0).

0 = a*x + b*y + c*z


We can find an equation that is useful to us using the normal vector of the plane. The normal vector with components <A,B,C> defines the plane when we subtitute -A=a, -B=b, -C=c in the equation of the plane. So:

0 = a*x + b*y + c*z 
subtitute -A=a, -B=b, -C=c
0 = -A*x + -B*y + - C*z

Now that we have the equation for the plane (which, as in the code, our character's position is directly above), and it's normal vector, we can project the normal vector "down" (more specifically, along the y-axis) onto the plane. Why do we project the normal vector down onto the plane? The reason we use the normal vector to do this is; the normal vector moves away from the plane in the fastest possible direction. So if we want to go "down" the plane as fast as possible, we have to travel in the same left/right and forward/backward direction, but still be on the plane in the up/down direction (in this case "down"). So we have the 'x' and 'z' components of the direction we are traveling in the components of our normal vector, but we need to find the 'y' component on our plane. So we solve the equation of the plane for 'y' to get an equation for how far down we go after traveling so far left/right and forward/backward.

0 = -A*x + -B*y + -C*z
slove for 'y'
y = (-A*x + - C*z) / B
y = -(A*x + C*z) / B

y = (A*x + C*z) / -B

Now we plug in or left/right, and forward/backward components into 'x' and 'z' respectively. AGAIN, these happen to be our components of our normal vector.  So:

y = (A*x + C*z) / -B

plug A in x, and C in z

y = (A*A + C*C) / -B

y = (A^2 + C^2) / -B

Yay, we have a usable equation for our 'y' component! Now we have to shove all our components into a vector so we have:

<A, [(A^2 + C^2) / -B], C>

which is a vector traveling in the direction that goes down the y-axis as fast as possible.

AND for good measure, I normalized it.

normalizing doesn't mean finding a normal vector, it means making the length of the vector = 1. Confusing, right?

[hint] If you want to find the normal vector of a plane defined by three point use the cross product and make sure two things, that the three points aren't on the same line and that the direction of your vector is going the correct +/- direction you expect it to. You can use this if you aren't simply hand the correct normal vector like Physics.Raycast() does.

That's it. I also used this eqation to find the change in height my character is moving (so he doesn't skip down hills) by replacing 'A' and 'C' with the character's right/left, forward/backward movement (respectively).

I hope this helps you out, it certainly would have helped me. wink.png

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!