- Home /
Question by
vladutstefan27 · Jul 18, 2019 at 04:41 PM ·
rotationinputmousecontrolleraxis
Cursor/Mouse movement to controller
Hello. I want to add controller input to my 2D top-down game, but I'm having some trouble. The projectile is launched from a shooting point (a game object attached to the weapon), respectively it's Transform. The weapon is being rotated when the player moves the cursor. How can I make the Left stick of the controller create similar movement of the mouse to move the cursor? or the very least, the rotation it implies.
Here is the code I used for receiving the rotation and direction of the mouse cursor. Then, if the player press Mouse Left click it launches the projectile.
// Subtraction between the position of mouse cursor and the object (in this case the Player)
Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
// Transforming the direction into an angle using "Mathf.Atan2" and converting it to Degrees from Radians.
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
// Convert 'angle' into Quaternion (Unity's Rotation)
Quaternion rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward);
transform.rotation = rotation;
I've tried changing the Vector2 direction to
Vector2 moveInput = new Vector2(Input.GetAxisRaw("Joystick_RightStickHorizontal"), Input.GetAxisRaw("Joystick_RightStickVertical"));
Vector2 direction2 = Camera.main.ScreenToWorldPoint(moveInput) - transform.position;
It didn't work. Tried a few other things. It just can't detect the triggers and sticky buttons.
Comment