for some reason i couldnt find a devforum post on this so im making this
im trying to make a laser that charges up for a set amount of time, and i need it to be put where the target will be after that amount of time has passed, but i suck at calculating stuff like this so ive turned to the devforum, im sure its easy to calculate but i just cant find any posts on this.
You can just get the target position. I don’t know why would you calculate it.
If you really want to do that for some reason, you can get the target velocity using basepart. AssemblyLinearVelocity then multiply it by the second passed, ONLY works if the speed is constant
Here is some code I wrote that can hopefully help you.
local bluePart = workspace.BluePart -- the laser
local redPart = workspace.RedPart -- the target
local function predictPosition(time)
local redVelocity = redPart.Velocity
return redPart.Position + redVelocity * time
end
local function aimAt(targetPosition)
local direction = (targetPosition - bluePart.Position).unit
bluePart.CFrame = CFrame.new(bluePart.Position, targetPosition)
end
local function aimAndShoot(timePassed)
local targetPosition = predictPosition(timePassed)
aimAt(targetPosition)
return targetPosition
end
Additionally, here is a demo where you can see it in action. LaserDemo.rbxl (53.5 KB)
How it works: A white part will appear in the red part’s path. This part will turn blue when the red part is estimated to hit it by using calculations. In the video, it is estimating the red part’s position after 0.5 seconds (as seen below). If friction is absent, it will be much more accurate.
--Example call
local timePassed = .5 -- Change this to any value you desire
local targetPosition = aimAndShoot(timePassed)