What a coincidence, I was just doing something like this.
To find a position 5 studs in front of a part is simple enough. You can multiply the lookVector of a CFrame by 5 to get a vector in the same direction as the lookVector, but with a magnitude of 5 rather than 1 (lookVector is a unit). After that, just add it to the CFrame of the original part's CFrame to get a position 5 studs in front of the part.
local baseCF = part1.CFrame
local inFront = baseCF + (baseCF.lookVector * 5)
part2.CFrame = inFront
#### puts the second part exactly 5 studs in front of the center position of your part1, but if part1 has a large size and part2 is small, part2 won't be visible because it will be inside of your part1. To correct for this, you can add half of the z Size to the number of studs we want to have the part2 away from part1
local inFront = baseCF + (baseCF.lookVector * (5 + part1.Size.Z/2))
Getting a Vector for velocity is similar, but we only have to worry about positions, not rotation.
local baseCF = part1.CFrame
local vel = baseCF.lookVector * 5
bodyVelocity.Velocity = vel
To have the velocity constantly be 5 studs in front, just put it into a loop. That way if the rotation of the part changes your velocity will correct for that.
while true do
local baseCF = part1.CFrame
local vel = baseCF.lookVector * 5
bodyVelocity.Velocity = vel
wait()
end
|