- Home /
Find left joystick Vector2 away from sprite's up direction.
Hi! Basically, I'm making a 2D game and my right joystick controls my character rotation, and my left joystick controls the movement. I'm trying to find how to get which direction my left joystick is moving in relation to where the character is facing, so if he were facing right on the screen and I were holding up on the left joystick, I would get an X value of 1 rather than Y. Sorry if this is simple maths! I'm terrible at that. Thanks in advance.
I suppose one way would be to get the dot product between the left joystick and the character transform's forward/right and use these, but I just feel like there is probably a simpler way
Answer by ItsAaron · May 28, 2020 at 06:26 AM
Ended up doing it like this but I'm sure there must be a better way
float xDot = Mathf.Clamp(Vector2.Dot(movementInput, spriteTransform.right), -1f, 1f);
float yDot = Mathf.Clamp(Vector2.Dot(movementInput, spriteTransform.up), -1f, 1f);
Your answer
Follow this Question
Related Questions
[Help] Object flips around when using transform.Rotate 3 Answers
Rotating a 2D GameObject on Z regardless of the distance. 2 Answers
How can I rotate my player to look at the direction my Joystick is pointing to? (Top-Down 2D) 3 Answers
How do I make one GameObject Point Towards another GameObject in 2D? 1 Answer