I want to choose 1 out of 4 dash animations based on the dash direction, but I can’t come up with the math needed for it. I have looked on the developer forum but all the solutions I found basically boiled down to getting which key (from W, S, A and D) is pressed which is not ideal.
I have 4 animations:
Dash Forward
Dash Back
Dash Left
Dash Right
I want to pick one of them based on the desired_direction I have in the script below:
-- ...
-- Get camera
local camera = workspace.CurrentCamera
-- Calculating direction
local desired_direction = camera.CFrame.LookVector * -1
if UserInputService:IsKeyDown(Enum.KeyCode.W) or UserInputService:IsKeyDown(Enum.KeyCode.S) or UserInputService:IsKeyDown(Enum.KeyCode.A) or UserInputService:IsKeyDown(Enum.KeyCode.D) then
desired_direction = humanoid.MoveDirection
end
-- 0 out the Y axis to make it impossible to dash up
desired_direction = Vector3.new(desired_direction.X, 0, desired_direction.Z).Unit
-- ...
This is how I want to determine the animations (the black dot is the player, the red lines show the “ranges” in which the animation will be played):
I basically need to determine in which range the direction falls into (get the angle between the player position and the direction?)
Hopefully I explained it good enough, if not please ask follow up questions
I dont really know how to do this myself but I think it is possible by getting the direction they are gonna dash to and their camera angle/ direction they are looking at. Again not sure how but hope it gives you a starting point
Intersting. I figured out one of the ways. I required ControlModule and called GetMoveVector, which returns the unit vector regardless of the device (at least applies to keyboard and mobile devices).
On a PC the situation is very simple: [Z] -1 → forward, 1 → backward; [X] -1 → left, 1 → right.
On mobile devices, the thumbstick can point in any direction, which is why I get the longer axis of the move vector and then see if it’s less or more than 0.
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local playerScripts = player:WaitForChild("PlayerScripts")
local Controls = require(playerScripts:WaitForChild("PlayerModule"):WaitForChild("ControlModule"))
while true do
task.wait()
local moveVector = Controls:GetMoveVector()
if moveVector:Dot(moveVector) == 0 then continue; end
if math.abs(moveVector.X) > math.abs(moveVector.Z) then
local direction = moveVector.X > 0 and "Right" or "Left"
print(direction)
else
local direction = moveVector.Z > 0 and "Backward" or "Forward"
print(direction)
end
end
That’s basically what I have right now in desired_direction
I think I need to use :vectorToWorldSpace on the direction but not sure how to get the angle after that.
Hey thanks for the reply, unfortunately this won’t work for me as I’m trying to use the desired_direction variable and it is not relative to the HumanoidRootPart (I didn’t test your code but if it works then I assume Controls:GetMoveVector() is)
I modified the code a bit based on this image:
and this is what came out (this is right after desired_direction is calculated):
-- Convert desired direction to world space based on root position
local temp_dir = root.CFrame:VectorToWorldSpace(desired_direction)
local direction = "Couldn't determine"
if temp_dir.X > root.CFrame.Position.X and temp_dir.Z > root.CFrame.Position.Z then
direction = "Forward"
elseif temp_dir.X > root.CFrame.Position.X and temp_dir.Z < root.CFrame.Position.Z then
direction = "Back"
elseif temp_dir.X < root.CFrame.Position.X and temp_dir.Z > root.CFrame.Position.Z then
direction = "Left"
elseif temp_dir.X < root.CFrame.Position.X and temp_dir.Z < root.CFrame.Position.Z then
direction = "Right"
end
print("Direction:", direction)
The issue with this is that this seems to give the result based on the root’s position relative to the (0,0,0) position in the world. And I might be stupid but I don’t understand why
edit: VectorToWorldSpace doesn’t seem to be doing what I thought it’s supposed to do
Can you please elaborate again on the expected result. I appreciate the image, but I’m not quite sure how the player and camera are positioned at the time a particular animation should be applied. So you are trying to get the direction relative to the humanoid root part?
:GetMoveVectors() method isn’t affected by character’s orientation. Depends on WASD/thumbstick/gamepad(?).
These values apply regardless of the relative space (world, object). The world’s -1 z is also root part’s -1 z.