Pretty much I’m trying to get this part to go flying randomly (Slightly different heights and has some spread but won’t go backwards) over these rocks.
I would want it so the part goes up at first and then gravity would do it’s job and it’d come hit the ground. I have never used velocity before so any help to best achieve this would be greatly appreciated.
To make the part fly randomly over the rocks, you can use a combination of the math.random
function to generate a random height, and the Vector3.new
function to set the initial velocity of the part. You can then use a BodyVelocity
object to apply the velocity to the part and let gravity do the rest.
Here’s an example script that you can modify to achieve this effect:
local part = game.Workspace.Part -- replace "Part" with the name of your part
local initialHeight = 10 -- the initial height of the part
local velocitySpread = 10 -- the amount of spread for the initial velocity
local upwardForce = 100 -- the upward force to apply to the part
-- generate a random initial velocity for the part
local velocity = Vector3.new(
math.random(-velocitySpread, velocitySpread),
upwardForce,
math.random(-velocitySpread, velocitySpread)
)
-- apply the initial velocity to the part
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Velocity = velocity
bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge) -- set max force to allow full control of velocity
bodyVelocity.P = 5000 -- set P to control how fast the part reaches the desired velocity
bodyVelocity.Parent = part
-- add a wait to allow the part to gain some height before disabling the BodyVelocity
wait(0.5)
-- disable the BodyVelocity to let gravity take over
bodyVelocity:Destroy()
You can adjust the initialHeight
, velocitySpread
, and upwardForce
values to get the desired effect. The P
value of the BodyVelocity
object controls how quickly the part reaches the desired velocity, so you can adjust this to make the part move faster or slower. The wait
function before disabling the BodyVelocity
is optional, but it can give the part a chance to gain some height before gravity takes over.
You need to add a force into the object to move it. For example:
local Debrid = game:GetService("Debris")
local bodyforce= Instance.new("BodyForce") -- Create a new body force
bodyforce.Force = Vector3.new(math.random(-500, 500), 500, math.random(-500, 500)) -- Set the Force to a random amount, but keep Y value always positive. You will ned to tune these to the mass of your part
bodyforce.Parent = <your rock part>
Debris:AddItem(bodyforce, 0.5) -- Debris forec after 0.5s so it doesn't fly up into the sky forever
with this method in which direction would the part fly?
The part would fly upward in a random direction due to the randomly generated initial velocity vector that has a large upward force component and a smaller spread in the x and z directions.
it seems to work but part disappears I think force is to much what should I change?
Keep dividing the upwardforce by half until it works, then choose a value which would work for you.
EX: upwardforce = 50, upwardforce = 25, etc.
ok perfect but rn it gets flung up and to the right how can I change the direction in where it flys to?
Edit: Nevermind i figured it out
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.