- Home /
I'm attempting to make a character face the joystick in a top down unity2d game.
I already have a working script that rotates the player towards the mouse, but I have no idea how to change that so that it rotates to the joystick. I'm looking to create a script for a second player. Here is the code making the character look at the mouse
mousePos = cam.ScreenToWorldPoint(new Vector3(Input.joystickPosition.x, Input.mousePosition.y, Input.mousePosition.z - cam.transform.position.z));
rid.transform.eulerAngles = new Vector3(0, 0, Mathf.Atan2((mousePos.y - transform.position.y), (mousePos.x - transform.position.x)) * Mathf.Rad2Deg);
Sorry for bad English. Any help will be greatly appreciated
Is this using an on-screen joystick like in a mobile game, or a joystick from a controller?
Answer by Hbgdf · May 01, 2017 at 09:06 AM
Try this:
zRot = Mathf.Atan2(joystickY, joystickX) * Mathf.Rad2Deg;
transform.rotation = Quaternion,Euler(new Vector3(0, 0, zRot);
Your answer
Follow this Question
Related Questions
How to control a 2d character in an top-down android game with the standard joystick? 1 Answer
[2D] Rotations become tighter the further left they are? 1 Answer
How do i rotate my player in moving direction in 2d? 0 Answers
How do I make a 2D object face the direction it is moving in top down space shooter 1 Answer