How to move parts independently on a spaceship?

Ive been trying to make a mining laser beam that moves where your mouse does, i want it to move independently from a moving spaceship whilest still being attached to it.

Tweenservice cant work as the part is unanchored
and bodyGyro cant work as the part is constraintwelded

ive tried using cframe but it moves the entire ship rather than just the mining beam

image

heres a diagram, ship moves forward, mining beam looks in direction of the mouse.

2 Likes

If the part is attached to the ship with a weldconstraint, you can move it without moving the ship by changing it’s position property instead of cframe property. If you also need to rotate it, and you have the rotation in the cframe you want the part to get, you can rotate the part by setting its orientation.

local cf = -- the cframe the part should have
part.Position = cf.Position

-- *180/math.pi converts the angles given in radians to degrees
part.Orientation = Vector3.new(cf:ToOrientation())*(180/math.pi)
2 Likes

I don’t know why I suddenly thought that you wanted the laser beam to look towards the mouse but here’s my solution

Before we get into coding, let’s get our ingredients ready;

  • A motor6D that connects the gun and the base

  • A seat attached on the spaceship

  • Using RigEdit Lite plugin, make sure the ‘ball’ of the motor6D (or I call it pivot) be from where you want the barrel to rotate

  1. Get the mouse position from a local script by using remote functions to invoke it from a server script (assuming you want the rotation to be seen on server)

For this, I use this code in a local script;

local remoteFunc = game:GetService("ReplicatedStorage"):WaitForChild("RemoteFunctions")
local OnSeat = remoteFunc:WaitForChild("OnSeat")

local localPlr = game.Players.LocalPlayer
local mouse = localPlr:GetMouse()

OnSeat.OnClientInvoke = function()
	return mouse.Hit.Position
end
  1. Then, we get the CFrame we want using CFrame.lookAt
    For this, I use this code;
local mouseDirection = OnSeat:InvokeClient(plr)
local this = CFrame.lookAt(Vector3.new(), mouseDirection- char.HumanoidRootPart.Position)+ motor6D.C1.Position

motor6D.C1 = this
  1. Then, we connect the code we wrote so that it activates when the player is in the spaceship.
    For this, I use this script;
local replicated = game:GetService("ReplicatedStorage")
local remotes = replicated:WaitForChild("RemoteFunctions")
local OnSeat = remotes:WaitForChild("OnSeat")

local seat = script.Parent:WaitForChild("Seat")

local model = script.Parent
local barrel = model:WaitForChild("Barrel")
local motor6D = barrel:WaitForChild("Motor6D")
local body = model:WaitForChild("Body")


seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant ~= nil then
		local char = seat.Occupant.Parent
		local plr = game.Players:GetPlayerFromCharacter(char)
		if plr then
			while seat.Occupant ~= nil do
				local mouseDirection = OnSeat:InvokeClient(plr)
				local this = CFrame.lookAt(Vector3.new(), mouseDirection- char.HumanoidRootPart.Position)+ motor6D.C1.Position

				motor6D.C1 = this
				game["Run Service"].Heartbeat:Wait()
			end
		end
	end
end)

Do it right and you get this;

2 Likes

Thanks for the lengthy response, this will be of great help.