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

Part of Player need to LookAt Mouse position ?

Asked by 3 years ago
Edited 3 years ago

Hey, My Character is a Tank. Right now my tank is moving with W,A,S,D.

https://i.imgur.com/VeZzInb.gif

I need the canon to always face the Mouse.

I saw that script here https://scriptinghelpers.org/questions/74839/character-wont-look-at-mouse-pointer And I tried to make it fit my needs, but it didnt work

local TankHead = game.StarterPlayer.StarterCharacter.CanonHead
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local rs = game:GetService("RunService").RenderStepped

rs:Connect(function()
    local hrp = TankHead:WaitForChild("Tank_Cannon_1")

    local lookAt = Vector3.new(mouse.Hit.p.X, hrp.Position.Y, mouse.Hit.p.Z)
    TankHead:SetPrimaryPartCFrame(CFrame.new(hrp.Position, lookAt))

end)

https://i.imgur.com/cPQpMEh.png

Someone Have an idea ?

IS it because the Canon is Rig ?

1 answer

Log in to vote
1
Answered by
Elyzzia 1294 Moderation Voter
3 years ago

you're modifying the StarterCharacter instead of the player's actual character

StarterCharacter is just cloned when the player spawns, modifications to it aren't applied to the player's actual character until the player respawns

-- make sure this script is in StarterCharacterScripts or else it's not going to work when the player respawns
local player = game.Players.LocalPlayer
local TankHead = player.Character:WaitForChild("CanonHead")
local mouse = player:GetMouse()

local rs = game:GetService("RunService").RenderStepped

rs:Connect(function()
    local hrp = TankHead:WaitForChild("Tank_Cannon_1")

    local lookAt = Vector3.new(mouse.Hit.p.X, hrp.Position.Y, mouse.Hit.p.Z)
    TankHead:SetPrimaryPartCFrame(CFrame.new(hrp.Position, lookAt))

end)

also, you might want to disable Humanoid.AutoRotate so the character doesn't rotate at all when you press w, a, s, or d

Ad

Answer this question