Search Unity

Controller Rotation Is Messing With Player Perspective.

Discussion in 'Editor & General Support' started by Mr_AgentFox, Jul 21, 2019.

  1. Mr_AgentFox

    Mr_AgentFox

    Joined:
    Jun 23, 2019
    Posts:
    59
    Using the right stick, I can rotate fine, but the player is eventually rotated the wrong way. I don't want it to be rotated like in the image, I want it upright. Any answers?

    WrongRotation.PNG


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     public float runSpeed = 5f;
    9.     public float shiftSpeed = 10f;
    10.  
    11.     PlayerControls controls;
    12.  
    13.     public GunScript shoot;
    14.  
    15.     Vector2 move;
    16.     Vector2 rotate;
    17.  
    18.     void Awake()
    19.     {
    20.         controls = new PlayerControls();
    21.  
    22.         controls.Gameplay.Movement.performed += ctx => move = ctx.ReadValue<Vector2>();
    23.         controls.Gameplay.Movement.canceled += ctx => move = Vector2.zero;
    24.  
    25.         controls.Gameplay.Rotation.performed += ctx => rotate = ctx.ReadValue<Vector2>();
    26.         controls.Gameplay.Rotation.canceled += ctx => rotate = Vector2.zero;
    27.  
    28.         controls.Gameplay.Shoot.performed += ctx => Shoot();
    29.     }
    30.  
    31.     void Update()
    32.     {
    33.         transform.Translate(new Vector3(runSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, runSpeed * Input.GetAxis("Vertical") * Time.deltaTime), Space.Self);
    34.  
    35.         Vector2 r = new Vector2(-rotate.y, rotate.x) * 100f * Time.deltaTime;
    36.         transform.Rotate(r, Space.Self);
    37.     }
    38.  
    39.     void OnEnable()
    40.     {
    41.         controls.Gameplay.Enable();
    42.     }
    43.  
    44.     void OnDisable()
    45.     {
    46.         controls.Gameplay.Disable();
    47.     }
    48.  
    49.     void Shoot()
    50.     {
    51.         shoot.Shoot();
    52.     }
    53. }
    54.