Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I fix this? This local script should make the player face the mouse.

I'm making a top-down game and am using this script to make the player face the mouse: (Local script in StarterCharacterScripts)

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Root = Character.HumanoidRootPart

local Mouse = Player:GetMouse()
local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function()
	local RootPos, MousePos = Root.Position, Mouse.Hit.Position
	Root.CFrame = CFrame.new(RootPos, Vector3.new(MousePos.X, RootPos.Y, MousePos.Z))
end)

However, this makes the player spin rapidly and rotate even when completely still. Video example here: https://www.youtube.com/watch?v=mfO7S7niV4g

2 comments

0

I don't really understand what do you mean cause I don't know what a top down is but if you just want to make the characters head to look into the mouse location then you need to make a head variable and use it instead of the root part right? However if you want the character body then it will be hard

0

Try to use an orientation instead or position. not sure but I think you need a hard mathematics sine position and orientation aren't the same..

1 answer

i dont see whats a problem with

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Root = Character.HumanoidRootPart

local Mouse = Player:GetMouse()
local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function()
    local RootPos, MousePos = Root.Position, Mouse.Hit.Position
    Root.CFrame = CFrame.new(RootPos, Vector3.new(MousePos.X, RootPos.Y, MousePos.Z))
end)

this is my full code replicating this in a empty baseplate

local RunService = game:GetService("RunService")	
local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local Root = player.Character:WaitForChild("HumanoidRootPart")
local Mouse = player:GetMouse()

local CAMERA_OFFSET = Vector3.new(-1,90,0)
local CframeLerp = CFrame.new()
camera.CameraType = Enum.CameraType.Scriptable

local function onRenderStep(DT)
	-- Check the player's character has spawned
	if player.Character then
		local playerPosition = player.Character.HumanoidRootPart.Position
		local cameraPosition = playerPosition + CAMERA_OFFSET
		local RootPos, MousePos = Root.Position, Mouse.Hit.Position
		Root.CFrame = CFrame.LookAt(RootPos, Vector3.new(MousePos.X, RootPos.Y, MousePos.Z))
		camera.CFrame = CFrame.new(cameraPosition, playerPosition)
	end
end

RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, onRenderStep)

maybe you have something interfering with the script above

This discussion is preserved as it was. New answers, comments, and votes are closed.