I’m making a plugin and I have a button that makes a new rig. I’d love it if when it was clicked ,it moved the rig in front of the camera, similar to the animation editor. I guess it uses raycasting or something?
This is the code used by the new rig functionality in the animation editor to move the rig on front of the camera.
function getCameraLookat(maxRange)
if maxRange == nil then maxRange = 10 end
local cam = game.Workspace:findFirstChild("Camera")
if cam then
local ray = Ray.new(cam.CFrame.p, cam.CFrame.lookVector * maxRange)
local hit, pos = game.Workspace:FindPartOnRay(ray)
cam.Focus = CFrame.new(pos)
return pos
else
--Default position if they did weird stuff
print("Unable to find default camera.")
return Vector3.new(0,5.2,0)
end
end
rig:MoveTo(getCameraLookat(10))
Awesome, thanks!
Probably being silly, but I’m trying to make it face the camera now (This just moves it to the position). I don’t want it to be orientated at all on the X axis. I tried to just make the X axis 0 but that didn’t work. This is what I’m doing:
r15:SetPrimaryPartCFrame(CFrame.new((getCameraLookat(10)), Vector3.new(0, game.Workspace.Camera.CFrame.Position.Y, game.Workspace.Camera.CFrame.Position.Z)))
I’d probably just do something like this:
local cam = game.Workspace:findFirstChild("Camera")
if cam then
local newRigPosition = getCameraLookat(10)
local cameraPositionSameHeight = Vector3.new(cam.CFrame.p.X, newRigPosition.Y, cam.CFrame.p.Z)
r15:SetPrimaryPartCFrame(CFrame.new(newRigPosition, cameraPositionSameHeight))
end
I haven’t tested this but hopefully it is what you meant.
That’s exactly what I meant. Thank you so much!
Do you know why they use Camera instead of workspace.CurrentCamera?
There isn’t a good reason that I can think of, someone probably just wrote it that way without considering using CurrentCamera.
I also find it kind of weird how that same functionality was dropped when the toolbox was made a Lua widget. Hopefully a fix rolls around soon.