Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 2 years ago

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

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 AltairCelestia 47 — 2y
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.. AltairCelestia 47 — 2y

1 answer

Log in to vote
0
Answered by
Puppynniko 1059 Moderation Voter
2 years ago
Edited 2 years ago

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

Ad

Answer this question