Body Velocity Direction

Hello Developers!

I’ve been working with BodyVelocities lately, and I noticed that inserting a BodyVelocity with a script into the HumanoidRootPart makes the player lose control completely. BodyVelocity thrusts the player forward in one direction, therefore the player cannot turn and proceed in another axis. Is there a strategy to enable movement whilst having a BodyVelocity in the player?

BodyVelocity:

local BodyVelocity = Instance.new("BodyVelocity",Humrp)
BodyVelocity.MaxForce= Vector3.new(math.huge,math.huge,math.huge)
BodyVelocity.P = math.huge
BodyVelocity.Velocity = Humrp.CFrame.lookVector * 10

Ice Slide VFX in progress:

https://streamable.com/r4kiu9

You can get a glimpse that I’m trying to turn with a BodyVelocity. It’s orientating me, but not moving me in the direction I am facing. Evidently from the block code above, I am missing something, but I’m not quite sure if there is an efficient way to check a player’s (with a BodyVelocity) direction.

All helpful answers are appreciated! Thanks. :slight_smile:

That’s more than likely cause you’re only changing the Velocity property once, I mean you could put it through a loop and make it work that way instead I believe

while true do
    wait()
    BodyVelocity.Velocity = Humrp.CFrame.lookVector * 10
end

I personally don’t think this is the best way to approach it, but it’s just an example

1 Like

Yeah while loops are extremely nonoptimal considering that you’re updating the Velocity constantly. I’m looking for any other way to be able to turn without looping through one Velocity.

Maybe try using CFrames?
Anchor humanoidrootpart, then use CFrame.new():Lerp() do to the trick?

If the player presses an input like A make the direction go right like:

local directions = {
 Vector3.new(1, 0, 0) -- left
 Vector3.new(0, 0, -1) -- back
 Vector3.new(0, 0, 1) -- front
 Vector3.new(-1, 0, 0) -- right
 Vector3.new(0,0,0) -- none?
}

local currentDirection = 0
Runservice:BindToRenderStepped(key, priority, function()
  hrp.CFrame = hrp.CFrame:Lerp(CFrame.new(directions[currentDirection], 0.5))
end)

uis.InputBegan:Connect(function(i, gpe)
 if gpe then return end
 if i.KeyCode == Enum.KeyCode.A then
  currentDirection = 1
 elseif i.KeyCode == Enum.KeyCode.W then
  currentDirection = 3
 elseif i.KeyCode == Enum.KeyCode.S then
  currentDirection = 2
 elseif i.KeyCode == Enum.KeyCode.D then
  currentDirection = 4
 end
end)

?

2 Likes

It’s not as bad as you think it is. You could probably use BodyForce or BodyThrust, but it will be much harder to control than BodyVelocity. I would just set the velocity of the BodyVelocity on heartbeat.

1 Like

For now, the ideal way would be a while loop/RunService. Turns out there is no other way to optimize updating a BV’s Velocity. Thanks for the help!

That’s a good approach, but the BV is currently on the server. Also there would only be 4 axis (left, back, front, right) and if a player were to move diagonally it would be impossible?

I think you could also refer to this post if it helps much?

For Up+Left or A+W is I guess:
Vector3.new(1, 0, 1)

Makes me go in one direction :thinking:

As of now, what is the code you have in place for the BodyVelocity?

It’s above, I included it in the post.

3 Likes