I’m working with BodyVelocity but I understand that the speed of BodyVelocities depends on the ‘distance’ (unless I’m wrong). So my question is, how do I get their speeds to stay the same no matter the distance?
Also when I increase the speed too much the object that the force is parented to starts to curve instead of go in a straight line.
•
Update: I think the answer I’m really looking for is how to keep it a straight line.
At a speed set at 50, it looks like this
At a speed of 250, it looks like this
all I do is have BodyVelocity.Velocity = CFrame.new(Origin.p, lookAt.p).lookVector * Speed
2 Likes
this code will get the unit vector of the parent part’s CFrame LookVector
(i.e. a vector of magnitude 1 in the same direction of the part’s CFrame forward-direction component; basically where its FrontSurface
is facing), multiply it by a constant Speed
, and set BodyVelocity.Velocity
to that value:
BodyVelocity.Velocity = BodyVelocity.Parent.CFrame.LookVector.unit * Speed
(edited to fix slight typo)
2 Likes
It’s still doing the same thing… let me use a different gif recorder to show you what I mean
•
Okay, here’s the speed set at 50
https://gyazo.com/948b1988e6fa054b63f80d753c91ea03
and here’s the speed set at 250
https://gyazo.com/eb0650a5a9fc0976e131b1e71ce5f4a0
Here’s some code that should give the result you want:
local Mouse = game.Players.LocalPlayer:GetMouse()
local Debris = game:GetService("Debris")
Mouse.Button1Down:connect(function()
local Part = Instance.new("Part")
local PartPosition = game.Players.LocalPlayer.Character.HumanoidRootPart.Position
Part.Size = Vector3.new(2, 2, 2)
Part.Anchored = false
Part.CanCollide = false
Part.CFrame = CFrame.new(PartPosition, Mouse.Hit.p)
local BodyVelocity = Instance.new("BodyVelocity")
BodyVelocity.Parent = Part
BodyVelocity.Velocity = Part.CFrame.LookVector.unit * 25
BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge) -- infinite acceleration !
Part.Parent = game.Workspace
Debris:AddItem(Part, 10)
end)
note that the important part in there is really the BodyForce.MaxForce
bit - this makes it so the BodyVelocity
can apply an infinite amount of force to get the part moving along the goal velocity vector, meaning gravity, unanchored obstacles, etc. will all have no effect
4 Likes
MaxForce was definitely the problem! It shoots in a straight directory every time now, thank you so much!