Hey! Kind of a stupid question… BUT I wanted to create a projectile for an enemy for my game that starts at the enemy position and then goes towards the players position for let’s say 100 studs before going away.
Weirdly I couldn’t find any other posts that helped me (perhaps of my goofy code but who knows.)
Basically what I have tried so far:
local originPos = startCFrame.Position
local playerPos = character.PrimaryPart.Position
local direction = (playerPos-originPos).Unit * rayLengh
local absoluteTargetCFrame = CFrame.new(CFrame.new(direction).LookVector * rayLengh)
local distance = (absoluteTargetCFrame.Position-originPos).Magnitude
local travelTime = distance / speed
local travelTween = tweenService:Create(newProjectile, TweenInfo.new(travelTime, Enum.EasingStyle.Linear), {CFrame = absoluteTargetCFrame})
I get the direction and then try to get a point along the direction line that is x [raylengh] studs away so it can be used for the tween.
All thought that didn’t really work so far.
I did this but now it takes some random position in the void as the end location. Not ideal
Here is what I did:
local direction = (playerPos-originPos).Unit * rayLengh
local absoluteTargetCFrame = CFrame.new(CFrame.new(direction).LookVector * rayLengh)
local distance = (absoluteTargetCFrame.Position-originPos).Magnitude
local travelTime = distance / speed
local destination = (direction*distance) + originPos
Perhaps the issue lies within the “AbsolutePos” variable’s calculation.
local direction = (playerPos-originPos).Unit * rayLengh
local destination= originPos + direction
local distance = direction.Magnitude
local travelTime = distance/speed
the problem is that you added the length of the ray twice at
and
a rule of thumb is that direction should always be one unit only