Vector3 to always point forward of the Character, ignoring rotation

So, how would I go about calculating a Vector3 to always face “forward” of the player, while ignoring the rotation of the player, example:

To give context, I have this code,

bF.Force = (Vector3.new(0, gravMass * forceVector.Y, 0) + player.Character.PrimaryPart.CFrame:vectorToWorldSpace(Vector3.new(gravMass * -forceVector.Z, 0, gravMass * -forceVector.X))) / partCount

Which applies a force to the character, using a BodyForce, this code will always push the player “forward” of the characters rotation, e.g. if they are facing downwards, they’ll be pushed downward, if they are facing forwards, they will be pushed forwards.

Edit: Vector3.new(0, gravMas * forceVector.Y, 0) always faces upwards, since it doesn’t get affected by rotation.

Changing vectorToWorldSpace to vectorToObjectSpace will do what I intend, however only in 180degrees, in the other 180degrees it pushed the player the opposite way.

Any help would be appreciated. If you need me to explain more, please lmk.

Can you not use lookVector?

local force = player.Character.PrimaryPart.CFrame.lookVector * forceMultiplier

This produces the same effect as the original. (Pushed down, instead of “pushed forward”)

My bad, I gave you the exact opposite of what you wanted. Give me a second to test, but I think I know how to do this.

Here you go. Let me know if something doesn’t work.

local down = Vector3.new(0, -1, 0) -- put this at the top of the script so it only needs to run once.

local force = player.Character.PrimaryPart.CFrame.RightVector:Cross(down).Unit * forceMultiplier
1 Like

This seems to work correctly, so map X direction to -Y, how would I go about doing this for the Z direction?
(I don’t mean to come off as ungrateful, its 3am for me right now ahaha, thanks for all the help so far)

How do you mean? This takes the local X direction and the global Y direction to solve for a consistent Z. Do you need to solve for something else?

Sorry I meant meant the inverse, e.g. left and right.

I’m still not sure I follow. Can you explain in other terms? So this code gives you a constant forwards in the case of doing a front flip. What else do you need to do?

Solve for a consistent X, as well as a consistent Z.

            if (-forceVector.Z) < 0 then
				bF.Force += player.Character.PrimaryPart.CFrame.RightVector:Cross(Vector3.new(0, -1, 0)).Unit * (forceVector.Z * gravMass)
			elseif (-forceVector.Z) > 0 then
				bF.Force += player.Character.PrimaryPart.CFrame.RightVector:Cross(Vector3.new(0, 1, 0)).Unit * (-forceVector.Z * gravMass)
			end

This is what I use to do the calculations locally for the consistent Z, based on your response, so how would I go about doing a consistent X?

(Again sorry for any confusion, 3am)

Edit: I may not respond until tomorrow, sorry. Getting rather tired.

My code relies on X being consistent. Do you have a consistent axis for this next bit you need?

I don’t have a huge understanding of CFrames, I don’t exactly follow where the consistent X is coming from, afaik, the only thing consistent is Vector3.new(0, -1, 0)

The above code works for the Z force, using edited negative/positives for positive/negative Z Force.

The consistent X is because you are rolling forward. Your whole torso is rotating along the X axis (left/right), so the X axis is consistently pointing in one direction.

If you were to ignore the code above and do this completely seperately, how would you go about doing the same, but for along the X axis instead.

Nevermind, in the end, this is what I end up with, and it seems to be what I need;

            bF.Force = Vector3.new(0, forceVector.Y * gravMass, 0)
			
			if (-forceVector.Z) < 0 then
				bF.Force += player.Character.PrimaryPart.CFrame.RightVector:Cross(Vector3.new(0, -1, 0)).Unit * (forceVector.Z * gravMass)
			elseif (-forceVector.Z) > 0 then
				bF.Force += player.Character.PrimaryPart.CFrame.RightVector:Cross(Vector3.new(0, 1, 0)).Unit * (-forceVector.Z * gravMass)
			end
			
			bF.Force += player.Character.PrimaryPart.CFrame:VectorToWorldSpace(Vector3.new(forceVector.X * gravMass, 0, 0))

Thanks for all the help and putting up with my confusion and tiredness, marking your answer as solution now.

1 Like