-
I want to make an archery minigame in my roblox game. I want to fire the arrow with the script below.
-
When the function runs, the arrow fires and goes through the at the expected path, but seems to be stuck in an orientation permanently facing in the positive X direction.
-
I rewatched the tutorial (BrawlDev’s physics tutorial) and checked my code. I tried using a few print statements that turned out as expected, and also looked at some more tutorials about CFrames so i could better understand the code, but I still don’t understand the issue. The code is below for reference.
local function FireArrow(Speed, TargetPosition, ArrowRoot)
local PointsToAdd = 0
local Origin = ArrowRoot.Position
local Direction = (TargetPosition - Origin).Unit
local GravityForce = Vector3.new(0, -workspace.Gravity, 0) * 0.5
local Position = Origin
ArrowRoot.CFrame = CFrame.new(Origin, Origin + Direction)
local HeartbeatFunction
local Velocity = Direction * Speed
HeartbeatFunction = RS.Heartbeat:Connect(function(DeltaTime)
local StepVelocity = Velocity + GravityForce * DeltaTime
local StepDisplacement = (Velocity + StepVelocity) / 2 * DeltaTime
Velocity = StepVelocity
local NextPos = Position + StepDisplacement
ArrowRoot.CFrame = CFrame.new(NextPos, NextPos + Velocity.Unit)
Position = NextPos
end)
end
The code is triggered on a pre-existing arrow with speed set to 100 and TargetPosition being the mouse’s hit pos. I have also tried inverting the Velocity.Unit to see if it did anything to which way the arrow was pointing, and that didn’t work.
Edit : I have found that pointing the mouse left of the starting position of the arrow makes it face to the left, and right of the start pos to the right.