Rotate player towards mouse

https://gyazo.com/4723b4ad698593054010d6358d6f27a2

Whenever the right or left mouse is held down, the player should rotate towards the mouse.
However, this doesn’t work when the player approaches a surface such as a wall, as shown in the video above, it seems the mouse still seems to hit the wall.

	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	local getCameraRot = CFrame.new(hrp.Position) * CFrame.Angles(0, -x, 0) * CFrame.Angles(y, 0, 0)

	Camera.CFrame = getCameraRot * CFrame.new(0, 3, CameraScroll)
	FollowMouse = math.clamp(FollowMouse + dt, 0, 1)
	if RMBHeld() or LMBHeld() then
		--local one = math.floor(math.deg(Char.HumanoidRootPart.CFrame.LookVector.X)+0.5)	-- Where player orginally facing
		--local two = math.floor(math.deg(Camera.CFrame.LookVector.X)+0.5)
		--math.floor(n + 0.5)
		
		-- lerp character towards the mouse
		hrp.CFrame = hrp.CFrame:lerp(CFrame.new(hrp.Position, Vector3.new(mouse.Hit.p.x, hrp.Position.Y, mouse.Hit.p.z)), DelayVar)
	end
1 Like

what is the x and y variables in getCameraRot

Oh I’m sorry I should’ve mentioned that.

UserInputService.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement then	-- Player moved mouse
		FollowMouse = 0
		x = x + input.Delta.X/sens
		y = math.clamp(y-(input.Delta.Y/sens), -1, 1)
	end	
end)

I did not understand your code, but here is my idea on this.

UserInputService.InputChanged:Connect(function(input)
  if input.UserInputType == Enum.UserInputType.MouseMovement then
    local RotX = input.Delta.X / sens

    RootPart.CFrame = RootPart.CFrame * CFrame.Angles(0, RotX, 0)
  end
end)

at the other hand i think putting BodyGyro in RootPart would be the best choice

Except I don’t believe that’s the root cause of my problem though.
If you looked at the video in my original post, you’d see in the output that mouse.Target would continue to print out the parts of the wall, despite the fact that I had my camera turned away from the wall.

I want the player to always turn their backs from the camera when the left or right mouse button is being held down.

I have watched it again, made a small script in my studio, you want to achieve something like this?

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")

local RootPart = script.Parent:WaitForChild('HumanoidRootPart')
local LocalPlayer = Players.LocalPlayer
local Camera do
	while not Workspace.CurrentCamera do
		Workspace.Changed:Wait()
	end
	
	Camera = Workspace.CurrentCamera
end

local function SyncHeights(p0, p1)
	local Flat = Vector3.new(1, 0, 1)
	return p0 * Flat, p1 * Flat
end

local function Update()
	local TargetPosition = Camera.CFrame.Position + (Camera.CFrame.LookVector * LocalPlayer.CameraMaxZoomDistance * 2)
	local LookerFrame = CFrame.lookAt(SyncHeights(RootPart.Position, TargetPosition))
	local Frame = LookerFrame - LookerFrame.Position + RootPart.Position
	
	RootPart.CFrame = Frame
end

UserInputService.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		Update()
	end
end)

UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter

Put in LocalScript in StarterCharacterScripts

EDIT: Calling update on RenderStepped is a better idea

5 Likes

I’d like to achieve something similar, but could you just briefly explain the script?
What exactly does the function SyncHieghts do, and what is TargetPosition?

SyncHeights makes two vectors the same height, meaning it makes the Y-axis 0 on both. It calculates where the camera would be if it was a couple of blocks ahead of the player. (I calculated the distance using CameraMaxZoomDistance times 2 but it’s really a custom value, just make sure it’s after the player)

Now we have 2 vectors in the same height which are, the player’s position and the position we calculated. We just use CFrame.lookAt to get the CFrame that contains the rotation we want to achieve, we set its position to 0, 0, 0 by subtracting its position from itself because we only need the rotation, then adding the player’s position to it since we don’t want it changed.

The end result is we have the CFrame of rotation with our player’s position, we just set the RootPart CFrame to it.

Thank you, the general concept worked very well when incorporated into my code.

1 Like