Make Character Rotate to Camera's Look Direction

Okay so, I want to make a Gliding feature with my character, but I’m having trouble with getting the HumanoidRootPart to rotate towards the Camera’s Look Direction on the X and Y Axis, here is the code I have for it

local function SetGyro(state)
if state then
	Gyro.MaxTorque = Vector3.new(1e5, 4e5, 1e5)
	Gyro.D = 2e3
	Gyro.P = 3e5
	Gyro.CFrame = CFrame.new(HumanoidRootPart.CFrame.Position, 
Vector3.new(Camera.CFrame.LookVector.X, Camera.CFrame.LookVector.Y, 0)*2e4)
elseif state == false then
	Gyro.MaxTorque = Vector3.new(0)
	Gyro.D = 0
	Gyro.P = 0
	Gyro.CFrame = CFrame.new()
end
end
local function returnDistanceFromGround()
local R = Ray.new(HumanoidRootPart.Position, (-HumanoidRootPart.CFrame.UpVector)*500)
local _, hitPosition = workspace:FindPartOnRayWithIgnoreList(R, ignoreList, false, true)
return (HumanoidRootPart.Position.Y - hitPosition.Y)
end

RunService.RenderStepped:Connect(function()
if pressingKey then
	local DistanceFromGround = returnDistanceFromGround()
	if DistanceFromGround > 20 then
		Humanoid.AutoRotate = false
		SetVelocity(true)
		SetGyro(true)
		
	else
		Humanoid.AutoRotate = true
		SetVelocity(false)
		SetGyro(false)

	end
else
	Humanoid.AutoRotate = true
	SetVelocity(false)
	SetGyro(false)
end
end)

here’s what happens https://gyazo.com/a70c109bb9f2e055f4ab87add1eb864f

It only rotates correctly on the Y axis, not the X, any way to solve this?

6 Likes

To clarify this is to be done in thrid person am I right.

Yeah, it is, I plan on using a Camera system like Batman Arkham Knight, I’m basically trying to remake this gliding feature: https://www.youtube.com/watch?v=k-T-stiSYgw

1 Like

So there’s actually a built-in way to do this, but it’s slightly obscure and appears to have been hacked in at the last minute when Roblox was porting the camera system into Lua back around 2014

UserGameSettings.RotationMode lets you overload the rotation behavior of whatever Humanoid happens to be the CameraSubject of the client’s camera, which is typically the humanoid being controlled by the player.

When set to “CameraRelative”, the subject Humanoid will automatically rotate to mirror the Y-axis of the camera’s rotation.

You can access the UserGameSettings via:
UserSettings():GetService("UserGameSettings")

Note that this value is updated during each frame of the camera’s step, and running a loop to overwrite it each frame won’t make the camera rotate with the mouse’s movement in third person. You may have to hook into Roblox’s PlayerModule to overload this behavior.

9 Likes

Alright thank you, I’ll try it out

Well it works, I did some hacky stuff, but the only problem is it only rotates on the X Axis, not the Y so if I look up it won’t rotate up RIP :frowning:

1 Like

I use Body Gyros for my flight system in my Dragon Ball game, and I typically do stuff with camera & rotating the character. What you want to do is have the body gyro point to a vector which is the Camera’s lookvector and multiply that value by something high, like 100000 I’ve tried math.huge and it does not work, so this will be more precise. BG.CFrame = CFrame.new(RootPart.Position, Vector3.new(Camera.CFrame.LookVector.X, Camera.CFrame.LookVector.Y, Camera.CFrame.LookVector.Z) * 10000000) This is the normal way I would do it. Also, why not just use math.huge for things like maxtorque etc? Incase you just want more info or something, here’s my main code for updating everything in a RenderStepped event:

It worked! Thank you

Gyro.CFrame = CFrame.new(HumanoidRootPart.Position, Camera.CFrame.LookVector.Unit * 100000000)

Albeit it’s kind of buggy when looking down, Need to find a solution for that now, but thank you
https://gyazo.com/813321da49413538493088ea029fa1ad

2 Likes

Instead of using a BodyGyro to make the character look up and down, I would advise altering the Motor6D CFrame based on the Y-axis of the camera’s LookVector.

1 Like

Although this “works”, it doesn’t work for the reason you might think.
You’re using this constructor:

CFrame.new(Vector3 origin, Vector3 lookAt)

The 2nd argument isn’t a LookVector, it’s a literal world-space position for the CFrame to look at from the provided Vector3 origin. Projecting it out that far will cause floating point errors due to rounding.

If you want a CFrame to face a specific direction precisely, you’d be better off doing something like this:

local cf = CFrame.new(origin, origin + lookVector)
20 Likes

Dude, that’s actually the solution and it worked, I never thought of it that way, I should’ve paid more attention to the BodyGyro wiki, thanks a lot for your help

1 Like

My solution works or his solution works? I’m mentioning this because he’s using the CFrame constructor incorrectly. Sorry if I’m mixing up what you meant.

Your solution works, his worked kinda, but it was buggy as it glitched all about.

1 Like

I said you wanted to make the Body Gyro point to a Vector, which is the Camera’s LookVector. The LookVector of a CFrame is just a normal Vector with x,y,z coordinate values that make up the direction the CFrame is facing foward, Never issued that the second argument is a LookVector.

I’m not using the constructor incorrectly, in a systematic standpoint. However in a logical standpoint you could say so based on your solution you have addressed:

All I’m suggesting is that it’s improper to multiply the LookVector of the Camera by a large value and use that as a target Vector3 to look at.

3 Likes