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

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.

What kind of bodymovers and how?

Try using a bodyVelocity object and setting the velocity of that. By doing part.Velocity, it is only temporary.

ive already tried using bodyvelocity and this is the result and how does humanoids move the character?

Carry on with using body movers they are way smoother, Also you can just change the velocity to make it go faster ofc. (velocity as in the property of body velo)

When you use a bodyvelocity try this;

local vel = Instance.new("BodyVelocity")
vel.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
vel.P = 1000
val.Velocity = humanoidRoot.CFrame.lookVector * 50

should be smoother since you’re setting the maxforce to a high number.

2 Likes

the maxforce is set to a high number

When Dynamese said flatten he did mean set y = 0… The slowdown is because the camera vectors for x and y are lower when looking up or down. You will want to even out the camera’s LookVector on your camera.CFrame line like so:
velocity = CFrame.new(camera.CFrame.p, camera.CFrame.p+(camera.CFrame.LookVector*Vector3.new(1, 0, 1)).Unit):VectorToWorldSpace(Vector3.new…
The reason for the .Unit is to compensate for the lost Magnitude when removing the Y component.

Edit: When I say the x and z components are lower I mean that the Magnitude is lower since you remove a whole component of the Vector.

1 Like

I don’t recommend using BodyMovers in new work as they are considered legacy and have been superseded by constraint-based movers:

3 Likes

then whats the best way todo it? i just want basic movements i dont want acceleration and all just basic , i am open to using Cframes and how does a humanoid move the characters ?

i tried using your code it works nicely but when the character hits a wall the character just gets flung most of the time the bodyvelocity’s maxforce = math.huge , 0 , math.huge i made the Y 0 because the
character kept flying with that on math.huge

ok guys i found out that use CFrames is the way to go its soo much better than using velocity it even replicates very well