Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a part point towards it's velocity?

Asked by
Drainhp 41
7 years ago

Hi, im trying to make a rocket launcher. I want the rockets of the rocket launcher to be affected by gravity, and so i also want the rockets to constantly point towards their own velocity when they are flying through the air, kinda like how an arrow flies.

I got a script here, and i'm not sure what's wrong with it.

tool = script.Parent
Handle = tool.Handle

launcher = tool.Launcher

tool.Equipped:connect(function(mouse)
    mouse.Button1Down:connect(function()
        local rocket = game.ServerStorage.Missile:Clone()
        rocket.CFrame = CFrame.new(launcher.CFrame.p, mouse.Hit.p)
        rocket.Velocity = rocket.CFrame.lookVector * 100
        rocket.Parent = workspace
        while true do
            rocket.Orientation = CFrame.new(rocket.Velocity).lookVector
            wait()
        end
    end)
end)

This is the bit that doesn't work:

while true do
    rocket.Orientation = CFrame.new(rocket.Velocity).lookVector
    wait()
end

This bit doesn't do what it's supposed to, it makes all the rocket continuously point towards 0, 0, -1. Can someone figure out why this is happening?

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

You're doing it wrong. When using Orientation you would use Vector3, but in this case we're using CFrame. And when you use lookVector it must have CFrame in front of it because it's a property of CFrame. So the script should look like this

while Wait(0) do
    rocket.Velocity = rocket.CFrame.lookVector * 5 -- Place any number here for the desired speed you want
end

Be careful though, because you should set the velocity to a high number otherwise the rocket would fall. Though using BodyPosition, BodyVelocity etc, will be a good idea to fix any issues and make it more efficient.

0
But this script makes the rocket go in a straight line, at that's not what i want. I want the rockets to fly like an arrow would. Drainhp 41 — 7y
Ad

Answer this question