Creating a projectile

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.

Thanks!!!

local absoluteTargetCFrame = CFrame.new(CFrame.new(direction).LookVector * rayLengh)

I don’t see why you made the direction into a new CFrame only to get its lookvector, since the lookvector would always be 0,0,-1.

And if you remove that bit, you’d just be multiplying the direction by the ray length again, which i also don’t see why you do that.

I think you should just make the tween’s CFrame equal to your direction variable

I have indeed tried tweeting my projectile to the “direction” variable instead (if this is what you meant). Although it still didn’t work.

you should just make a variable called destination which is (direction*distance) + origin

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.

try this

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

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.