Using VectorForce on the HRP's LookVector

Hello! I’ve been trying to work on a movement system slightly from scratch using VectorForces. I have this set up when the player is pressing W:

local function moveForward()
	local VectorForce = HumanoidRootPart:FindFirstChild("VectorForce")
	local forceMultiplier = 5000
	local direction = HumanoidRootPart.CFrame.LookVector
	
	VectorForce.Force = direction * forceMultiplier
end

Whenever the player is pressing W, the Force property on the VectorForce changes based on the HRP’s LookVector (supposedly where the player is facing). However, there is some strange behavior when the player is not facing the negative Z direction. Here’s a visual representation of the problem I’m having:

Example

The black box represents the HumanoidRootPart. The orange arrow represents the direction the player is facing, and the blue arrow represents the direction of the vector force. The axes are also listed. For other information, I am using the standard player’s character and I have locked their camera to first person using Enum.CameraMode.LockFirstPerson.

What can I do to apply a VectorForce in the direction in which the HRP is facing without this strange directional behavior occuring?

2 Likes

So in this case, I think you can simply do:

local direction = Vector3.new(0,0,-1)

Or at least, that’s what seemed to kinda work for me.
Here’s a full test script.

local Players = game.Players

Players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local VectorForce = Instance.new("VectorForce", character.PrimaryPart)
	local forceMultiplier = 5000
	VectorForce.Attachment0 =  character.HumanoidRootPart.RootRigAttachment
	
	local direction = Vector3.new(0,0,-1) 
	VectorForce.Force = direction * forceMultiplier	
	
end)
2 Likes

Thank you so much! This works like a charm. However, I do not know why it necessarily works. Can you please explain how this works and how the force isn’t just moving along the -Z axis? Does it have something to do with the relation to the RootRigAttachment?

1 Like

No problem! Glad it worked!

Yes - it’s the default behavior, which is relative to the Attachment0.
It could be changed by setting the “RelativeTo” property.

Source:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.