Answered by
4 years ago Edited 4 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:
02 | local Players = game:GetService( "Players" ) |
03 | local UserInputService = game:GetService( "UserInputService" ) |
04 | local PhysicsService = game:GetService( "PhysicsService" ) |
06 | local localPlayer = Players.LocalPlayer |
07 | local mouse = localPlayer:GetMouse() |
08 | local localChar = localPlayer.CharacterAdded:wait() |
09 | local localCharHumRootPart = localChar:WaitForChild( "HumanoidRootPart" ) |
10 | local part = script.Parent |
11 | local targetPart = game.Workspace:FindFirstChild( "TargetPart" ) |
17 | local function PreparePart(whichPart) |
19 | local bodyPosition = Instance.new( "BodyPosition" , whichPart) |
20 | local bodyGyro = Instance.new( "BodyGyro" , whichPart) |
22 | bodyPosition.MaxForce = Vector 3. new( math.huge , math.huge , math.huge ) |
23 | bodyPosition.P = speed |
24 | bodyPosition.D = dampening |
25 | bodyPosition.Position = whichPart.Position |
26 | bodyGyro.MaxTorque = Vector 3. new( math.huge , math.huge , math.huge ) |
27 | bodyGyro.CFrame = whichPart.CFrame |
31 | local ex = Instance.new( "Explosion" ) |
32 | ex.Position = whichPos |
36 | local function DetectHit(whichPart) |
37 | whichPart.Touched:Connect( function (hit) |
38 | if not hit:IsDescendantOf(localChar) then |
39 | CreateExplosion(whichPart.Position) |
46 | local function LaunchPart(whichPart, whichPosition) |
48 | whichPart.Anchored = false |
50 | local bodyPosition = whichPart:FindFirstChild( "BodyPosition" ) |
52 | bodyPosition.Position = whichPosition |
55 | local function CreatePart() |
56 | local part = Instance.new( "Part" ) |
58 | part.Size = Vector 3. new( 2 , 2 , 2 ) |
59 | part.BrickColor = BrickColor.new( "Really red" ) |
61 | local humRootPartCFrame = localCharHumRootPart.CFrame |
62 | local humRootPartLookVector = humRootPartCFrame.LookVector |
63 | part.CFrame = humRootPartCFrame + (humRootPartLookVector * 2 ) |
65 | PhysicsService:SetPartCollisionGroup(part, "Objects" ) |
67 | part.Parent = game.Workspace |
71 | local function FireProjectile(input) |
72 | print ( "Keyboard was pressed" ) |
73 | if input.UserInputType = = Enum.UserInputType.MouseButton 1 then |
74 | print ( "Firing projectile" ) |
75 | local newPart = CreatePart() |
78 | local position = mouse.Hit.p |
79 | LaunchPart(newPart, position) |
85 | UserInputService.InputBegan:Connect(FireProjectile) |
And here's my server script (Goes in ServerScriptService):
01 | local PhysicsService = game:GetService( "PhysicsService" ) |
04 | local function CreateCollisionGroups() |
05 | local players = "Players" |
06 | local objects = "Objects" |
08 | PhysicsService:CreateCollisionGroup(players) |
09 | PhysicsService:CreateCollisionGroup(objects) |
12 | PhysicsService:CollisionGroupSetCollidable(players, objects, false ) |
16 | CreateCollisionGroups() |
19 | local function GiveCharCollisionGroup(whichChar) |
20 | if not whichChar then return end |
22 | local charChilds = whichChar:GetChildren() |
24 | for _, eachChild in pairs (charChilds) do |
25 | if eachChild:IsA( "BasePart" ) then |
26 | PhysicsService:SetPartCollisionGroup(eachChild, "Players" ) |
30 | whichChar.DescendantAdded:Connect( function (descendant) |
31 | if descendant:IsA( "BasePart" ) then |
32 | PhysicsService:SetPartCollisionGroup(descendant, "Players" ) |
37 | game.Players.PlayerAdded:Connect( function (player) |
38 | player.CharacterAdded:Connect( function (char) |
39 | GiveCharCollisionGroup(char) |
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.