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

Rocket Propulsion to Position?

Asked by 3 years ago

Hi, I was just wondering if it is possible to make the target of rocket propulsion a position?

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

Yes it is possible. You add a BodyPosition to the part (you'll use this to move the part) and a BodyGyro so that the part can maintain it's orientation.

Next, you give a new vector3 position to the body position (which is the target you define) and it will take care of moving the rocket to the set position.

While trying to make you a good example for you to learn on, I got a little bit into it and ended up making this: https://gyazo.com/6101070a14ad802ce698164e7bd9a5cc

I basically get the 3d position in the world from the mouse hit then I create a part and give it a BodyPosition and a BodyGyro and I define the position of BodyPosition to the position of the mouse.Hit. Finally, I add a touch interest to the part so that it gets destroyed when it hits something other than the character and create an effect explosion.

Next, to make sure that the part is CanCollide false with the player that shoots the projectile, I created 2 collision groups (One for the objects and the other for the player) and set collisions between both groups to false. I then added every basepart of the character in the player collision group and made it so each spawned projectile's collision group is the Objects group.

Here's my local script (This goes in StarterPack) so it resets everytime the player dies:

-- Services
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local PhysicsService = game:GetService("PhysicsService")
-- Variables
local localPlayer = Players.LocalPlayer
local mouse = localPlayer:GetMouse()
local localChar = localPlayer.CharacterAdded:wait()
local localCharHumRootPart = localChar:WaitForChild("HumanoidRootPart")
local part = script.Parent
local targetPart = game.Workspace:FindFirstChild("TargetPart")
-- Configs
local speed = 3000
local dampening = 500
-- Functions
-- Create the required body movers to the part that we need to move
local function PreparePart(whichPart)
    -- Body movers
    local bodyPosition = Instance.new("BodyPosition", whichPart)
    local bodyGyro = Instance.new("BodyGyro", whichPart)
    -- Body movers settings
    bodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
    bodyPosition.P = speed
    bodyPosition.D = dampening
    bodyPosition.Position = whichPart.Position
    bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
    bodyGyro.CFrame = whichPart.CFrame
end
--
local function CreateExplosion(whichPos)
    local ex = Instance.new("Explosion")
    ex.Position = whichPos
    ex.Parent = workspace
end
-- Detect hit, on hit delete part and create explosion
local function DetectHit(whichPart)
    whichPart.Touched:Connect(function(hit)
        if not hit:IsDescendantOf(localChar) then
            CreateExplosion(whichPart.Position)
            whichPart:Destroy()
            whichPart = nil
        end
    end)
end
-- Move the part to the position of the target part
local function LaunchPart(whichPart, whichPosition)
    -- Make sure the part is not anchored
    whichPart.Anchored = false
    -- Change the position of the BodyPosition
    local bodyPosition = whichPart:FindFirstChild("BodyPosition")
    -- Get the position of the target part
    bodyPosition.Position = whichPosition
end
-- Create a part
local function CreatePart()
    local part = Instance.new("Part")
    part.Shape = "Ball"
    part.Size = Vector3.new(2,2,2)
    part.BrickColor = BrickColor.new("Really red")
    -- Spawn the part a few units in front of the character (Where he is looking)
    local humRootPartCFrame = localCharHumRootPart.CFrame
    local humRootPartLookVector = humRootPartCFrame.LookVector
    part.CFrame = humRootPartCFrame + (humRootPartLookVector * 2) 
    -- Assign the collision group of the part to objects so it doesn't hit the player
    PhysicsService:SetPartCollisionGroup(part, "Objects")

    part.Parent = game.Workspace
    return part
end
-- Fire projectile
local function FireProjectile(input)
    print("Keyboard was pressed")
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        print("Firing projectile")
        local newPart = CreatePart()
        PreparePart(newPart)
        -- Get the position of the mouse
        local position = mouse.Hit.p 
        LaunchPart(newPart, position)
        -- Give touch interest to the part
        DetectHit(newPart)
    end
end

UserInputService.InputBegan:Connect(FireProjectile)

And here's my server script (Goes in ServerScriptService):

local PhysicsService = game:GetService("PhysicsService")

-- Create collision groups so the projectile cannot hit the player
local function CreateCollisionGroups()
    local players = "Players"
    local objects = "Objects"

    PhysicsService:CreateCollisionGroup(players)
    PhysicsService:CreateCollisionGroup(objects)

    -- Make collision between players and objects false
    PhysicsService:CollisionGroupSetCollidable(players, objects, false)
end

for i=1,1 do
    CreateCollisionGroups()
end

local function GiveCharCollisionGroup(whichChar)
    if not whichChar then return end

    local charChilds = whichChar:GetChildren()

    for _, eachChild in pairs(charChilds) do
        if eachChild:IsA("BasePart") then
            PhysicsService:SetPartCollisionGroup(eachChild, "Players")
        end
    end

    whichChar.DescendantAdded:Connect(function(descendant)
        if descendant:IsA("BasePart") then
            PhysicsService:SetPartCollisionGroup(descendant, "Players")
        end
    end)
end

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        GiveCharCollisionGroup(char)
    end)
end)

I used a body position and a body gyro to move the part and keep it's orientation. But keep in mind you could also simply use the tween service to tween either the position of the part or the CFrame of the part.

0
I will work on it tonight. TheReverseGaming 29 — 3y
0
Let me know how it goes. Don't forget to set the question as answered though if this helped you :) DevMaster333 215 — 3y
0
I'm confused, how would I use this. I am trying to make a pet go beside the player. TheReverseGaming 29 — 3y
0
Nvm I got it to work TheReverseGaming 29 — 3y
Ad

Answer this question