Fast way to find movement direction along a slope?

So I know these things:

// var moveDirection:Vector3 = Raw movement direction input (Vertical and Horizontal axis).
// var up:Vector3 = Normal from face below me.

I have used cross products to figure it out, but I think there might be a faster way to accomplish this, since I am doing it every FixedUpdate, I’d like to do it as efficiently as possible, like some kind of a built in Vector3 method, that I just don’t know about :smile:

Here is how I have done it:

// Vector3.Cross(Vector3.Cross(localMoveDirection, -up), up);

// get the local forward direction based on input
var localMoveDirection:Vector3 = transform.TransformDirection(moveDireciton.normalized);

// get the local right axis
var localMoveRightAxis:Vector3 = Vector3.Cross(localMoveDirection, -up);

// use the right axis and up to determine the new direction
var newDir:Vector3 = Vector3.Cross(localMoveRightAxis, up);

So now newDir is movement direction along the face I am standing on.

Is there a better way to do this? Or is there a build in Unity function that does this, that I am too noob to recognize or know how to use?

Far as I know, that’s the “base-est” way to do that. I use it myself to force players to slide down steep slopes. Haven’t encountered any performance issues with it.

Well for starters, you can just reverse the order of the cross product instead of flipping the up like that.

public function GetForwardTangent(moveDir:Vector3, up:Vector3):Vector3
{
	//return Vector3.Cross(Vector3.Cross(up,moveDir),up);
	var rght:Vector3 = Vector3.Cross(up,moveDir);
	var forw:Vector3 = Vector3.Cross(rght, up);
	return forw;
}

This is pretty much the simplest way to do it mathematically speaking.

Of course you might be able to eek out a little more efficiency if you wanted to by performing the math yourself. Breaking down the cross product yourself.

<a1,a2,a3> X <b1,b2,b3> =
<a2b3-a3b2, a3b1-a1b3, a1b2-a2b1>

cross that again by <a1,a2,a3> and you get this long ass thing
<(a3b1 - a1b3)a3 - (a1b2 -a2b1)a2, (a1b2 - a2b1)a1 - (a2b3 - a3b2)a3, (a2b3 - a3b2)a2 - (a3b1 - a1b3)a1>

or

public function GetForwardTangent(mv:Vector3, up:Vector3):Vector3
{
	//return Vector3.Cross(Vector3.Cross(up,moveDir),up);
	return new Vector3( (mv.z * up.x - mv.x * up.z) * mv.z - (mv.x * up.y - mv.y * up.x) * mv.y, 
			    (mv.x * up.y - mv.y * up.x) * mv.x - (mv.x * up.z - mv.z * up.y) * mv.z, 
			    (mv.y * up.z - mv.z * up.y) * mv.y - (mv.z * up.x - mv.x * up.z) * mv.x );
}

Though really, I don’t see it being all that much faster.

Anyways, in the end, no there is no Vector3 method built-in to the structure to do this for you. The methods on the Vector3 structure are general math processes for vectors, and this isn’t your general math abstraction… it’s a concatenation of two cross products.

What lordofduct said with the addition that unless you are performing this calculation on hundreds or thousands of objects in your scene, the performance impact of improving on your original code would probably be minimal in the overall system. Did you profile the performance impact? Is it causing an issue or is this just a gut instinct thing? This smacks of a micro-optimization because of a “feeling” that it is slow. Profile twice, refactor once. I am going to hazard a guess and say that just the time to invoke the LateUpdate function on the game object probably is on par with the computation done inside the function.

Hey thanks for the replies.

I was actually just kind of hoping there was a built in method that perhaps I overlooked or didn’t know how to use.

@lordofduct OMGLOLWTF??? That is some crazy math skills you have there bro. Thanks for the tip on going with Cross(Cross(up, move), up)

@justinlloyd I am just pre-optimizing at this point. I have only been using Unity for about a month now, and I figured this was a common enough goal, there might be a built in function to do it automagically.

I do have a tendency to micro-optimize though. Right now I am just working on my character movement, but I do plan to use the same code for enemies as well, so I thought I would check, before I called it ‘done’.