I am making a sword. At the moment, while the player is swinging, their walkspeed is set to 0 and a tween moves them forward. Meaning the player can’t face the direction they want.
I am trying to rotate the player towards their MoveVector while taking the camera into account.
I cannot seem to find how to rotate the player in the direction of a Vector3.
For example, I have a function that gives me the players input direction and returns something similar to this:
W = (0,0,-1),
A = (-1,0,0),
etc
I found the function in another post since I am not great with rotations and positions.
function GetInputDirection()
local MovementVector = Controller:GetMoveVector()
if MovementVector == Vector3.zero then
return Vector3.zero
end
local CameraLook = workspace.CurrentCamera.CFrame.LookVector
local Offset = CFrame.lookAt(Vector3.zero,Vector3.new(CameraLook.x,0,CameraLook.z))*MovementVector
return Offset.Unit
end
This returns what I want, a vector3 with the input direction. But I cannot for the life of me rotate the player.
I’ve tried tweens and ultimately would prefer it to be tweened. I have also tried multiple ways of setting the CFrame and tried looking into the character’s core scripts, but found that the movement doesn’t use the camera at all and Humanoid.AutoRotate is probably handling it
It should rotate the player in the direction of their input.
I am probably missing something, or this is just a lack of my understanding with Vector3.
local Controller = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule")):GetControls()
function GetInputVector2() --returns a Vector2 with an input direction based on the player's camera
local movementVector = Controller:GetMoveVector()
local cameraLook = workspace.CurrentCamera.CFrame.LookVector
local offset = CFrame.lookAt(Vector3.zero, Vector3.new(cameraLook.x,0,cameraLook.z)) * movementVector
local inputDirection = Vector2.new(offset.X, offset.Z)
if inputDirection == Vector2.zero then
return inputDirection
else
return inputDirection.Unit
end
end
while wait() do --test
local inputVector = GetInputVector2()
if inputVector ~= Vector2.zero then
inputVector = inputVector * -1 --because forward is -1 and backwards is 1, multiplying them by -1 will reverse it
local angle = math.deg(math.atan2(inputVector.X, inputVector.Y)) --converts the vector2 into an angle
workspace.Test2.Orientation = Vector3.new(0, angle, 0)
end
end
This is as far as I got. I don’t understand the math as this is just a bunch of research from other threads pulled together.
Unfortunately when I tried to rotate the HumanoidRootPart it would not work. Instead I ended up using @Downrest 's solution after switching from tweening to body velocities. I’m going to keep this as the solution though as it some what answers my original question and I put a lot of time getting to where I did.