Align to Normal and look down slope...

Hi all,

I kinda got stuck on this and I feel there might be a nice way to do it.
I’m trying to align to the surface normal but also have the x axis always oriented to look down the slope of the surface it’s on.

Is there an easy way to do that?
Thanks,
Pete

Not sure how you are going to get the “look down slope” vector, but when you do you can use vector3.cross to “combine” with what you currently have. Will be a “look right of slope” vector when using vector3.cross though.

An alternative is using a series of rays (3 of them should do) to get the downward facing vect that way instead, although it would be more expensive.

What’s this for anyway?

Here’s a trick that might help you: when the y axis vector isn’t 0,||1||,0 the normalized vector2 of the x and z components is roughly the direction towards the “down” slope.

Vector3 yAxis = transform.up;
Vector3 xAxis = new Vector3(1f, 0f, 0f);
if (Mathf.Abs(yAxis.y) < 0.999f)
{
Vector3 towardsDown = new Vector3(yAxis.x, 0f, yAxis.z);
xAxis = Vector3.Cross(Vector3.Cross(yAxis, towardsDown), yAxis).normalized();
}

I might have the order of the components of cross mixed up as a warning, which might give you the vector going “up”, so you might have to move them around.

1 Like

Hey guys thanks for that.
I wanted to use something like this to potentially orient my character to the facing downhill based on where they were standing. I’m still just prototyping really so things are a little loose and I’m not sure if it’s going to work.
P.