How to move a part using velocity relative to its lookvector?

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)

X and Z are the inputs

3 Likes

not very good with these things but you could try

LowerTorso.Velocity = Vector3.new(X, 0, Z)

not sure though

1 Like

that will work but not relative to the character it will be relative to the world

1 Like

you could always try looking in the roblox’s modules and see what they do

1 Like

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:

LowerTorso.Velocity = LowerTorso.CFrame.LookVector * speed

3 Likes

i want to move the character sideways too

1 Like

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.

Velocity = Camera.CFrame:VectorToWorldSpace(Vector3.new(X, 0, Z).unit) * speed

edit: I wrote broken code, oops. I changed it, but I’m going to test it in a bit.
edit2: I hope that fixed it, apologies.

1 Like

camera’s right and your code seems to be throwing an error
edit1: now my character just teleports to nan , nan , nan

1 Like

Make sure X and Z aren’t NaN? Could I see your .Velocity = line?

1 Like
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)
1 Like

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.

ok it works but the character is able to fly by looking upwards

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)

I’m confused do you want the player to move in the direction of lookVector?

wdym by flat? return Y = 0 on the Nonands function? if then thats cause my character to slow down when i look up or down

yes and in sideways

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.

1 Like

To make the character move sideways, I’d do:

-- move to the right
LowerTorso.Velocity = LowerTorso.CFrame.RightVector * speed

-- move to the left 
LowerTorso.Velocity = LowerTorso.CFrame.RightVector * -1 * speed
3 Likes

Ok so velocity is not doing well for my custom character so i tried using CFrames but i am still stuck at this

my goal is to have a simple character that can move , jump and go up of wedges

I wouldn’t particularly use part velos, What i usually do is use body movers they are far more reliable and smoother.