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

Better method than using a RocketPropulsion?

Asked by 5 years ago

I have a game where players can use magic staffs. One feature is that the staff shoots a fireball where they click. Currently, I'm using the RocketPropulsion object in my fireball by making an invisible part where they click and then setting that as the target.

My problem is that once the rocket gets there it stops, Is there any other way to propel a part towards a position and continue after that position in the same direction?

For example,

With RocketPropulsion: Player one's staff is at (0,10,0) and clicks (10,10,0), with the RocketPropulsion it travels in a horizontal line and stops at (10,10,0).

What I'm asking for: Player one's staff is at (0,10,0) and clicks (10,10,0), it travels in a horizontal line and even if it gets to the end position it keeps traveling indefinitely.

Any help would be graciously accepted!

0
Is the FireBall a Part or a Model? If its in a model, you could try to loop the primarypart cframe to move a set amount of studs indefinitely. SerpentineKing 3885 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

You can use the UnitRay property of the Mouse object, in combination with a BodyVelocity, to make an object travel along a vector indefinitely.

Here is a sample script with comments

local Players = game:GetService("Players"); -- Players Service
local LocalPlayer = Players.LocalPlayer; -- Get player
local Mouse = LocalPlayer:GetMouse(); -- Get player's mouse
local Part = workspace:WaitForChild("Part"); -- This is the reference to the fireball
local Speed = 10; -- Modify to desired speed

Mouse.Button1Up:Connect(function() -- Mouse event you are listening to
    local Velocity = Part:WaitForChild("BodyVelocity"); -- BodyVelocity inside fireball
    local UnitRay = Mouse.UnitRay.Direction; -- Vector3 Direction of the UnitRay
    Velocity.Velocity = UnitRay * Speed; -- Multiply Direction by desired speed to get final velocity
end)

Also note that your fireball needs to be unanchored for this to work. Because of this you might need to add a BodyForce to counteract gravity if you do not want it to influence the fireball. You would make the BodyForce's force value to:

Vector3.new(0, Part:GetMass()*Gravity,0)

BasePart:GetMass() returns the mass of the fireball, and gravity is the gravity of the game (Default is 196.2). This effectively counteracts the force of gravity (Force = Mass * Acceleration).

0
Nice answer, though for some reason bodyforce has never worked as intended for me, XD SerpentineKing 3885 — 5y
0
Thanks, and yeah bodyforce can be tricky :) GrandpaScriptin 148 — 5y
0
thanks, its been a long time since i posted this and a while since you replied, was a little busy with life, thanks though. Ill get back to you if it works! KamikazeJAM108867 38 — 5y
0
Glad to help, feel free to message me if you have any questions :) GrandpaScriptin 148 — 5y
Ad

Answer this question