Player turns to cursor, please help

I’m making a 2D-Shooter game called GEN. VENT where you “escape the matrix” by fighting tough dudes and entities and I want it to be where ever the cursor is on the screen for PC and maybe Console, the player turns the entire body like those few games do but scripting isn’t really my specialty so I’m in a pickle here, does anyone have a idea on how to achieve this? Also, down below is what I want to achieve in this game if anyone’s curious.

I want to achieve this:

  • Get working weapons and hostile NPC’s

  • Make a map with maybe gamepasses and badges

  • Add music and a tutorial to the game.

  • Design the UI and Settings

AND

  • Work on constant updates to keep players entertained.

Soon, I’ll update the post to leave the link for the Paid Beta when I actually FINISH the game. So yeah, if you have any ideas, please reply to the awfully long post. Thank you! :grinning:

2 Likes

from a local script

get player with

local plr = game.Players.LocalPlayer

then get their mouse

local mouse = plr:GetMouse()

then get the mouse position and turn the character to it in a loop

--while or runservice loop here

local mousePos = mouse.Hit.Position
plr.Character:SetPivot(CFrame.LookAt(plr.Character:GetPivot().Position, mousePos)) -- can also tween here for smooth movement

Where would I put the script though? Do I put it in StarterPlayerScipts or StarterCharacterScripts, or where do I put it?

Should be in StarterPlayerScripts i believe

Where you put the script really depends on what you think makes the most sense. StarterPlayerScripts is where I would normally put movement code. Also the script above will probably cause issues as mouse.Hit.Position is the position of the mouse in 3D space, which usually lies on the first object obstructed by the mouse. In your case, a 2D game, you could probably get the X, Y position of the mouse and the local players character on the players screen, then calculate a vector going from the given X, Y position of the player to the position of the mouse and lastly covert that vector into world space relative to your camera. The resulting vector is then the direction your character has to face.

1 Like

Just to add to my own post real quick: There are probably very different approaches to this based on the design of your game and how exactly it works. You don’t necessarily have to follow this approach.

1 Like

Since it references the character I recommend putting it in StarterCharacterScripts.

Just some quick notes: Anything you put in StarterCharacterScripts will be placed (as a child or direct descendant) under the player’s character in the workspace. When a character dies, their character will be removed from the workspace and reloaded. So if make a script that prints “Hi”, everytime that character dies, the script will run and print “Hi”.

If you put it that same print(“Hi”) script in StarterPlayerScripts, the player isn’t ever really removed for any reason, even if u die, so it will only print when the player enters the game, and the script will be parented by the player’s player in the Player Service. (But not directly, it will be placed in “PlayerScripts” under the player)

1 Like

Also, is this for R6 or R15 because this is an R6 and is it supposed to look like this because he separated the parts so I decided to try to put them together.
|
v

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local mousePos = mouse.Hit.Position
plr.Character:SetPivot(CFrame.LookAt(plr.Character:GetPivot().Position, mousePos))