title says it all i am made a custom character controller and i want to use velocity to move the character
self.RunService.RenderStepped:Connect(function()
if Character then
local LowerTorso = Character.LowerTorso
local X , Y , Z = 0 , 0 , 0
Z = Z + (Movement.Front and -1 or 0) + (Movement.Back and 1 or 0)
X = X + (Movement.Left and -1 or 0) + (Movement.Right and 1 or 0)
LowerTorso.Velocity = --Heres where i am stuck at
end
end)
Velocity is a speed with a direction, you can grab the direction through CFrame.LookVector and define a speed. Then multiply the speed across all three component vectors for the appropriate velocity:
I seem to have missed the point of the question. I’m actually still not sure what your desired behavior is. Is your character’s movement independent of the camera (D would move your character to your character’s right), or would pressing D move your character to the right relative to the camera?
With the assumption that Movement is defined relative to the camera, I would convert your new X, Y, Z into world space as your direction component and apply a speed on top of it.
self.RunService.RenderStepped:Connect(function()
if Character then
local LowerTorso = Character.LowerTorso
local X , Y , Z = 0 , 0 , 0
Z = Z + (Movement.Front and -1 or 0) + (Movement.Back and 1 or 0)
X = X + (Movement.Left and -1 or 0) + (Movement.Right and 1 or 0)
LowerTorso.Velocity = Camera.CFrame:VectorToWorldSpace(Vector3.new(X, 0, Z).unit) * 1
end
end)
I think it has to do with setting .Velocity every step; try swapping to a bodymover or a constraint force.
Velocity is (NaN, NaN, NaN) when no input is provided; e.g. all Movement members are false/nil and I attempt to get the unit vector out of (0, 0, 0). I whipped up a hacky fix to correct the NaNs, but it’s ugly. Have a look:
function nonands(v)
local x = v.X
local y = v.Y
local z = v.Z
if x ~= x then x = 0 end
if y ~= y then y = 0 end
if z ~= z then z = 0 end
return Vector3.new(x, y, z)
end
game:GetService("RunService").Stepped:Connect(function()
local X, Z = 0, 0
local velocity
Z = Z + (Movement.Front and -1 or 0) + (Movement.Back and 1 or 0)
X = X + (Movement.Left and -1 or 0) + (Movement.Right and 1 or 0)
velocity = camera.CFrame:VectorToWorldSpace(Vector3.new(X, 0, Z).unit) * 10
velocity = nonands(velocity)
print(velocity)
actor.PrimaryPart.Velocity = velocity
end)
actor is the character, and I replaced LowerTorso with .PrimaryPart when testing because I thought it had to do with moving non-primaryparts; I’m probably mistaken.
Flatten the world space vector before applying speed by multiplying it with Vector3.new(1, 0, 1). Apply the speed after and then nonand it at the end. I’ll be stepping out of this now so I don’t cause any more damage.
edit: Here’s where I was testing if anyone wants it. Oof it’s an old version, pre-nonand. asdf.rbxl (15.1 KB)
This example might be what you’re looking for. It covers NAN and camera situations when you’re looking up and down.
--Some variables
local zeroVector3 = Vector3.new()
local Camera = workspace.CurrentCamera
local Player = game.Players.LocalPlayer
--movement testing
Movement = {
Front = true,
Right = true,
}
function OnStep(step)
--Gotta make sure our character and primary part exists you know
local character = Player.Character
local primaryPart = character and character.PrimaryPart
if primaryPart then
--Create the xz velocity
local Z = (Movement.Front and -1 or 0) + (Movement.Back and 1 or 0)
local X = (Movement.Left and -1 or 0) + (Movement.Right and 1 or 0)
local velocity = Vector3.new(X,0,Z).Unit
--Do a mag check in case we're not moving or we have NAN
local mag = velocity.Magnitude
if mag ~= mag then
--We DONT want to set the velocity because we're either:
--a. not moving (we would get nan either way if that's happening)
--b. our magnitude somehow is already nan
return
end
--gotta normalize our camera's look vector to give proper results
local lookVector = Camera.CFrame.LookVector
lookVector = Vector3.new(lookVector.x,0,lookVector.z)
--Convert our look vector into a useable cframe
lookVector = CFrame.new(zeroVector3,lookVector)
--Finally get our velocity and apply it
velocity = lookVector:VectorToWorldSpace(velocity)*10
velocity = Vector3.new(velocity.x,primaryPart.Velocity.y,velocity.z)
primaryPart.Velocity = velocity
end
end
game:GetService("RunService"):BindToRenderStep("VelocityStep",Enum.RenderPriority.First.Value,OnStep)
Keep in mind that you’re using velocity, which is inclined to being induced by friction. If you’re standing on Baseplate with this script, your character may not move because of the friction of the baseplate. If that’s the case, either set the baseplate’s friction to 0, use a speed > 10, or calculate how much more velocity you would need to break friction of the part you’re standing on.
Alternatively, you could also solve this by instead switching over to a BodyVelocity, having the max force set to Vector3.new(math.huge,0,math.huge), and then setting its velocity to what was created here.
-- move to the right
LowerTorso.Velocity = LowerTorso.CFrame.RightVector * speed
-- move to the left
LowerTorso.Velocity = LowerTorso.CFrame.RightVector * -1 * speed