Hey everybody who is reading this. I’ve been trying to make the HumanoidRootPart face the mouse without interrupting movement and the Y vector. It’s like the spell cast in Roblox Elemental Battlegrounds. Here’s a video of how I want it to work.
pretty sure that the game does it with allign orientation
Hello! The way you can move the player’s character to face the mouse is quite simple. If you’re looking for a way to constantly face the mouse, then you’ll want to do something like this:
-- Client -> StarterPlayer -> StarterCharacterScripts
local RunService = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
function UpdateCharacterRotation()
local RootPosition, MousePosition = HumanoidRootPart.Position, Mouse.Hit.Position
HumanoidRootPart.CFrame = CFrame.new(RootPosition, Vector3.new(MousePosition.X, RootPosition.Y, MousePosition.Z))
end
RunService:BindToRenderStep("RotateToMouse", 1, UpdateCharacterRotation)
This makes it so every frame the character’s HumanoidRootPart gets updated to look at the direction of where your mouse is looking while also not messing with the Y axis of your RootPart.
If you’re looking for a way to activate & deactivate this, you can make an InputService/ContextActionService bind to turn on and off a value that can be checked in the update function. It can be implemented like this:
local CanRotate = false
function UpdateCharacterRotation()
if not CanRotate then return end -- Checks if you can rotate
local RootPosition, MousePosition = HumanoidRootPart.Position, Mouse.Hit.Position
HumanoidRootPart.CFrame = CFrame.new(RootPosition, Vector3.new(MousePosition.X, RootPosition.Y, MousePosition.Z))
end
function InputReceived(ActionName, InputState, InputObject)
if InputState == Enum.UserInputState.Begin then
CanRotate = not CanRotate -- Inverses the boolean
end
end
RunService:BindToRenderStep("RotateToMouse", 1, UpdateCharacterRotation)
ContextActionService:BindAction("CharacterRotation", InputReceived, false, Enum.KeyCode.R)
I Hope this helps!
Thanks! It worked really well like I expected!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.