Body following mouse choppy

I tried to make a script that makes your character rotate to your body, but it looks very choppy when moving the cursor from the nearest object to the furthest object, here’s how it looks:
https://gyazo.com/b757b61c6dbe4999fe512ff2c56c5215

My script:

local rs = game:GetService("RunService") -- gets runservice
local player = game.Players.LocalPlayer -- player
local mouse = player:GetMouse() -- player's mouse
local char = player.Character -- player's character

if not char then -- check if player has a character
	player.CharacterAdded:Wait()
	char = player.Character
end

local rp = char:WaitForChild("HumanoidRootPart") -- gets humanoidrootpart

rs.Stepped:connect(function() -- main function
	if equipped == true then

		local MousePos = mouse.hit.p

		local lookVector = Vector3.new(MousePos.X,rp.CFrame.Y,MousePos.Z)

		rp.CFrame = CFrame.new(rp.CFrame.p,lookVector)	
	end	
end)

I tried using some other methods from the dev forum, but when I look at objects that are very far from the player, the player’s character is facing backward for some reason
Also, is there a way so the player would turn a bit longer to the position? Not too much, so you wouldn’t feel like you’re rotating a tank

For the mouse, try using this Part pointing towards mouse sometimes goes backwards - #5 by dthecoolest. This method allows you to get the position of the mouse while also being able of using RaycastParams (since it’s using raycasting), meaning you can easily remove some or any parts that can make the character have that effect.

I’m guessing you mean that you want the player to turn a little slower instead of just turning immediately. To obtain that effect, you should probably use CFrame:Lerp.

RunService.RenderStepped:Connect(function()

	local inset = GuiService:GetGuiInset() 
	local mouseLocation = UserInputService:GetMouseLocation() - inset

	local cameraRay = game.Workspace.CurrentCamera:ScreenPointToRay(mouseLocation.X, mouseLocation.Y)

	local raycastResult = game.Workspace:Raycast(cameraRay.Origin, cameraRay.Direction * 50, raycastParams)
	
	local lookvector = Vector3.new(mouseLocation.X, rp.CFrame.Y, rp.CFrame.Z)

	if raycastResult then
		if equipped == true then
			rp.CFrame = CFrame.new(rp.CFrame.p, lookvector)
		end
	end
end)

Here’s what I got, no idea why does it not work

Are you looking for something like this?
https://gyazo.com/cd4bcb5998dac1cc92aa3c47ea0e4d37

Yep. That’s what I tried to do.

I used the way I told you with lerping the HRP CFrame towards the mouse position, which can be found with the function I sent in the other answer.

1 Like
while equipped do
	function Mouse.HitPosition(raycastParams, distance)
		local mousePos = userInput:GetMouseLocation()
		local viewportMouseRay = cam:ViewportPointToRay(mousePos.X, mousePos.Y)
		local rayCastResult = workspace:Raycast(viewportMouseRay.Origin, viewportMouseRay.Direction * (distance or RAY_DISTANCE), raycastParams)
		local hitPosition
		
		if rayCastResult then
			hitPosition =  rayCastResult.Position
			local lerp = rp.CFrame:Lerp(hitPosition, 1)
			rp.CFrame = lerp
		else
			hitPosition = viewportMouseRay.Origin+viewportMouseRay.Direction * (distance or RAY_DISTANCE)
		end
		return hitPosition
	end
	return Mouse
end

No clue why does it not work.

I managed to make that it prints the hitposition, everything seem to work without any errors but the character still doesn’t lerp? Here’s the current code:

while equipped do
	local mousePos = userInput:GetMouseLocation()
	local viewportMouseRay = cam:ViewportPointToRay(mousePos.X, mousePos.Y)
	local rayCastResult = workspace:Raycast(viewportMouseRay.Origin, viewportMouseRay.Direction * (distance or RAY_DISTANCE), raycastParams)
	local hitPosition
	
	
	if rayCastResult then
		hitPosition = rayCastResult.Position
		rp.CFrame = rp.CFrame:Lerp(CFrame.new(hitPosition), 1)
	else
		hitPosition = viewportMouseRay.Origin+viewportMouseRay.Direction * (distance or RAY_DISTANCE)
	end
	wait(0.1)
end

Could you send the script you’re using here?

Sure, here it is:

local char = game.Players.LocalPlayer.Character;
local hrp = char:WaitForChild("HumanoidRootPart");
local hum = char:WaitForChild("Humanoid");
local uis = game:GetService("UserInputService");

local raycastParams = RaycastParams.new();
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist;
raycastParams.FilterDescendantsInstances = {};

local function GetHitPosition(distance)
   local mousePos = game:GetService("UserInputService"):GetMouseLocation();
   local viewportMouseRay = workspace.CurrentCamera:ViewportPointToRay(mousePos.X, 
   mousePos.Y);
   local rayCastResult = workspace:Raycast(viewportMouseRay.Origin, viewportMouseRay.Direction * 
   (distance or 1000), raycastParams);
   local hitPosition;

   if rayCastResult then
      hitPosition = rayCastResult.Position;
   else
      hitPosition = viewportMouseRay.Origin+viewportMouseRay.Direction * (distance or 1000);
   end;

   return hitPosition;
end;

game:GetService("RunService").RenderStepped:Connect(function()
   local pos = GetHitPosition();
   hrp.CFrame = hrp.CFrame:Lerp(CFrame.new(hrp.Position, Vector3.new(pos.X,0,pos.Z)), 0.05);
end);
1 Like