Help to make a following spotlight


I’m trying to make a following spotlight, we were thinking of using motor6ds to make it move but we had no idea how to make it follow a person. It’s going to emit a beam texture and a spotlight on the head.
I need some help, thanks!

2 Likes

I think the first step would be to find the angle to the player.
You might have to find two angles the up and down and left and right then tilt the motor6d using that.

I have never really understood Motor6ds so maybe it might be a little different. However the math is prettty simple.

local function GetYaxis(center, lookat) return math.atan2(lookat.X - center.X, lookat.Z - center.Z) end

The code is only for the left and right Y Axis

I am not sure if Motor 6ds follow orientation like movement. So I am not sure if the code will be applicable.

2 Likes

You can also use something like this, You might have to tweak some things but this should work.

local char = --define character
local camera = --Part that you want to face player 
while wait() do
     camera.CFrame = CFrame.new(camera.Position, char.HumanoidRootPart.Position)
end
1 Like

In this case I would use the HingeConstraint | Roblox Creator Documentation to rotate the camera. Make sure that it is set up as a Servo so you can then edit the HingeConstraint | Roblox Creator Documentation. I am unsure about the math to calculate the rotation however.

1 Like

Thank you everyone for the help! I’ll open this thread for a while, feel free to give better solutions!

Motor6D is much better than hingeconstraint though.

1 Like

If you despise anything that can relate to physics things from constraints/etc:

Just cframe it.

You might want to try CFrame.lookAt as well :man_shrugging:

1 Like

All you do is get the origin c0 then multiply it by the angle CFrame which faces the player?

1 Like

Is this correct? Am I supposed to unanchor the model?
I got an error.
image image

  1. Don’t use while wait() do for this situation, RunService’s events are more suitable here.
  2. OP wanted to move the arm together with the head, and they’re using Motor6Ds too, so setting the CFrame of the model is a final resort probably.
  3. CFrame.new(pos, lookAt) is deprecated, use CFrame.lookAt() instead.
2 Likes

Model is not a basepart. You need to use something actually in the model.

1 Like

For the character you need to fetch it from game.Players.PLAYERNAME.Character
and for the camera, you should be setting the model’s PrimaryPart’s CFrame instead.

1 Like

Yes make sure that the light is on the front face, because CFrame.lookat will look at it from the front face.

1 Like

If you want to use CFrame.lookAt then just know that it will rotate the camera from it’s center of mass. The best way you can avoid this is to do these steps:

  1. Place the main camera part inside of its own Model | Roblox Creator Documentation object
  2. Create an invisible part, anchor it, and place it in the camera parts model.
  3. Position the part in-between the arms that attach to the camera and in-line with the center of the camera part
  4. Orient the part so that it’s front face is facing the direction you want the camera to point
  5. Set the Model | Roblox Creator Documentation of model the camera part and the invisible part are in to the invisible part
  6. Unanchor the camera part and use a WeldConstraint | Roblox Creator Documentation to weld it to the invisible part
    6.1. Set the WeldConstraint | Roblox Creator Documentation to the camera part
    6.2. Set the WeldConstraint | Roblox Creator Documentation to the invisible part

Then you can use set the invisible bricks cframe using CFrame.lookAt and the camera part with move with it.