Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Bug Controller Movement Now Working In Update.

Discussion in 'Input System' started by Xayias, Jul 28, 2020.

  1. Xayias

    Xayias

    Joined:
    Mar 22, 2019
    Posts:
    7
    Hello, I am working on a top down 3D Shooter and in the previous build I am able to plug in a controller with the new Input System and move around with the left analog stick fine. I start updating a few things for a update I have planned in a few months and once I test the controller the movement just doesn't want to work. I have a Vector2 = moveAxis and when I use the stick the moveAxis updated but it seems the transform.position that is being set with the moveAxis isn't updating. I haven't touched the code for the controller and the only thing that I might have messed with was accidentally deleting some animation states within my animator but that shouldn't mess this up. I have rotation and shooting with the right stick and a trigger button and both work fine in this new build so the controller is being detected and some actions are happening.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.InputSystem;
    6.  
    7. [RequireComponent(typeof(CharacterController))]
    8. public class PlayerController : MonoBehaviour
    9. {
    10.     // Handling Variables
    11.     public float rotationSpeed = 450f;
    12.     public float walkSpeed = 3f;
    13.     public float runSpeed = 5f;
    14.     private float acceleration = 5f;
    15.     // System Variables
    16.     private Quaternion targetRotation;
    17.     private Vector3 currentVelocityMod;
    18.     Ray cameraRay; // The ray that is cast from the camera to the mouse position
    19.     RaycastHit cameraRayHit; // The object that the ray hits
    20.     /*public Transform spawn;*/
    21.  
    22.     // Components
    23.     public Transform handHold;
    24.     private CharacterController controller;
    25.     private Animator animator;
    26.     private new Rigidbody rigidbody;
    27.     private Camera cam;
    28.     public GameObject bulletSpawnPoint;
    29.     public float waitTime;
    30.     public GameObject projectile;
    31.     public float fireRate = 3f;
    32.     private float lastShot = 0.0f;
    33.     private GameObject player;
    34.  
    35.     /*private Vector3 moveInput;
    36.     private Vector3 moveVelocity;
    37.     public float moveSpeed;*/
    38.  
    39.     public PlayerControls controls;
    40.     private float JoystickSpeed = 6f;
    41.     private Vector2 moveAxis;
    42.     private Vector2 rotateAxis;
    43.     public float horizontalRotationSpeed;
    44.  
    45.     private int Xbox_One_Controller = 0;
    46.     private int PS4_Controller = 0;
    47.  
    48.     Vector3 lastPos;
    49.     Vector3 playerPosition;
    50.     float threshold = 0.0f;
    51.     public string sceneName;
    52.  
    53.     // Start is called before the first frame update
    54.     void Start()
    55.     {
    56.         controller = GetComponent<CharacterController>();
    57.         animator = GetComponent<Animator>();
    58.         cam = Camera.main;
    59.         lastPos = transform.position;
    60.         player = GameObject.FindGameObjectWithTag("Player");
    61.         rigidbody = player.GetComponent<Rigidbody>();
    62.         Scene currentScene = SceneManager.GetActiveScene();
    63.         string sceneName = currentScene.name;
    64.     }
    65.    
    66.     private void Awake()
    67.     {
    68.         controls = new PlayerControls();
    69.     }
    70.  
    71.     // Update is called once per frame
    72.     void Update()
    73.     {
    74.         ControlMouse();
    75.         //ControlWASD();
    76.         //Turning();
    77.  
    78.         // Gun Input
    79.         if (Input.GetButtonDown("Shoot"))
    80.         {
    81.             Shoot();
    82.         }
    83.         /*for(int i = 0; i < guns.Length; i++)
    84.         {
    85.             if (Input.GetKeyDown((i+1) + "") || Input.GetKeyDown("[" + ((i+1)) + "]"))
    86.             {
    87.                 EquipGun(i);
    88.                 break;
    89.  
    90.             }
    91.         }*/
    92.         /*Vector2 moveAxis = new Vector2(mAxis.x, mAxis.y) * Time.deltaTime;*/
    93.         /*transform.Rotate(new Vector3(0.0f, rotateAxis.y * horizontalRotationSpeed * Time.deltaTime, 0.0f));*/
    94.        
    95.         string[] names = Input.GetJoystickNames();
    96.             for (int x = 0; x < names.Length; x++)
    97.             {
    98.                 print(names[x].Length);
    99.                 if (names[x].Length == 19)
    100.                 {
    101.                     print("PS4 CONTROLLER IS CONNECTED");
    102.                     PS4_Controller = 1;
    103.                     Xbox_One_Controller = 0;
    104.                 }
    105.                 if (names[x].Length == 33)
    106.                 {
    107.                     print("XBOX ONE CONTROLLER IS CONNECTED");
    108.                     //set a controller bool to true
    109.                     PS4_Controller = 0;
    110.                     Xbox_One_Controller = 1;
    111.                 }
    112.             }
    113.  
    114.         if (Xbox_One_Controller == 1)
    115.         {
    116.             if (sceneName == "Level1")
    117.             {
    118.                 Cursor.visible = true;
    119.                 Debug.Log("Cursor Is Not Hidden");
    120.             }
    121.             else {
    122.                 Debug.Log("Cursor Will Be Hidden");
    123.                 Cursor.visible = false;
    124.                 Debug.Log("Cursor Is Hidden");
    125.             }
    126.             Debug.Log("Cursor Should Be Hidden");
    127.             transform.position += new Vector3(moveAxis.x * Time.deltaTime * JoystickSpeed, 0 , moveAxis.y * Time.deltaTime * JoystickSpeed);
    128.             playerPosition = transform.position;
    129.             Vector3 offset = playerPosition - lastPos;
    130.             float speed;
    131.             speed = rigidbody.velocity.magnitude;
    132.             if( offset.x > threshold  || offset.x < -threshold)
    133.             {
    134.                 Debug.Log("Should Walk");
    135.                 animator.SetFloat("Speed", JoystickSpeed);
    136.             }
    137.             if( speed < 0.5 )
    138.             {
    139.                 Debug.Log("Should Idle");
    140.                 animator.SetFloat("Speed", threshold);
    141.             }
    142.  
    143.             var angle = Mathf.Atan2(rotateAxis.x, rotateAxis.y) * Mathf.Rad2Deg;
    144.             transform.localRotation = Quaternion.Euler(0, angle, 0);
    145.         }
    146.         else if (PS4_Controller == 1)
    147.         {
    148.             if (sceneName == "Level1")
    149.             {
    150.                 Cursor.visible = true;
    151.                 Debug.Log("Cursor Is Not Hidden");
    152.             }
    153.             else {
    154.                 Debug.Log("Cursor Will Be Hidden");
    155.                 Cursor.visible = false;
    156.                 Debug.Log("Cursor Is Hidden");
    157.             }
    158.             Debug.Log("Cursor Should Be Hidden");
    159.             transform.position += new Vector3(moveAxis.x * Time.deltaTime * JoystickSpeed, 0 , moveAxis.y * Time.deltaTime * JoystickSpeed);
    160.             Debug.Log(moveAxis);
    161.             Debug.Log(transform.position);
    162.             playerPosition = transform.position;
    163.             Vector3 offset = playerPosition - lastPos;
    164.             float speed;
    165.             speed = rigidbody.velocity.magnitude;
    166.             if( offset.x > threshold  || offset.x < -threshold )
    167.             {
    168.                 Debug.Log("Should Walk");
    169.                 animator.SetFloat("Speed", JoystickSpeed);
    170.             }
    171.             if( speed < 0.5 )
    172.             {
    173.                 Debug.Log("Should Idle");
    174.                 animator.SetFloat("Speed", threshold);
    175.             }
    176.  
    177.             var angle = Mathf.Atan2(rotateAxis.x, rotateAxis.y) * Mathf.Rad2Deg;
    178.             transform.localRotation = Quaternion.Euler(0, angle, 0);
    179.         }
    180.         else
    181.         {
    182.             ControlMouse();
    183.         }
    184.     }
    185.     void Shoot()
    186.     {
    187.         if (Time.time > fireRate + lastShot){
    188.             GetComponent<AudioSource>().Play();
    189.             Instantiate(projectile, bulletSpawnPoint.transform.position, bulletSpawnPoint.transform.rotation);
    190.             lastShot = Time.time;
    191.             Debug.Log("shoot");
    192.         }
    193.     }
    194.  
    195.     void Run()
    196.     {
    197.         JoystickSpeed = 3f;
    198.     }
    199.  
    200.     void NotRun()
    201.     {
    202.         JoystickSpeed = 2f;
    203.     }
    204.     void ControlMouse()
    205.     {
    206.         Vector3 mousePos = Input.mousePosition;
    207.  
    208.         mousePos = cam.ScreenToWorldPoint(new Vector3(mousePos.x,mousePos.y,cam.transform.position.y - transform.position.y));
    209.         targetRotation = Quaternion.LookRotation(mousePos - new Vector3(transform.position.x,0,transform.position.z));
    210.         transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y,targetRotation.eulerAngles.y,rotationSpeed * Time.deltaTime);
    211.  
    212.         Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical"));
    213.         /*currentVelocityMod = Vector3.MoveTowards(currentVelocityMod,input,acceleration * Time.deltaTime);*/
    214.         Vector3 motion = input;
    215.         motion *= (Mathf.Abs(input.x) == 1 && Mathf.Abs(input.z) == 1)?.7f:1;
    216.         motion *= (Input.GetButton("Run"))?runSpeed:walkSpeed;
    217.         motion += Vector3.up * -8;
    218.         controller.Move(motion * Time.deltaTime);
    219.  
    220.         animator.SetFloat("Speed",Mathf.Sqrt(motion.x * motion.x + motion.z * motion.z));
    221.  
    222.     }
    223.  
    224.     void Turning()
    225.     {
    226.         cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    227.        
    228.         if(Physics.Raycast(cameraRay, out cameraRayHit))
    229.         {
    230.             if(cameraRayHit.transform.tag=="ground")
    231.             {
    232.                 Vector3 targetPosition = new Vector3(cameraRayHit.point.x,transform.position.y,cameraRayHit.point.z);
    233.                 transform.LookAt(targetPosition);
    234.             }
    235.         }
    236.     }
    237.     void ControlWASD()
    238.     {
    239.         Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical"));
    240.         if (input != Vector3.zero)
    241.         {
    242.             targetRotation = Quaternion.LookRotation(input);
    243.             transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y,targetRotation.eulerAngles.y,rotationSpeed * Time.deltaTime);
    244.         }
    245.         currentVelocityMod = Vector3.MoveTowards(currentVelocityMod,input,acceleration * Time.deltaTime);
    246.         Vector3 motion = currentVelocityMod;
    247.         motion *= (Mathf.Abs(input.x) == 1 && Mathf.Abs(input.z) == 1)?.7f:1;
    248.         motion *= (Input.GetButton("Run"))?runSpeed:walkSpeed;
    249.         motion += Vector3.up * -8;
    250.         controller.Move(motion * Time.deltaTime);
    251.  
    252.         animator.SetFloat("Speed",Mathf.Sqrt(motion.x * motion.x + motion.z * motion.z));
    253.     }
    254.  
    255.     void OnTriggerEnter(Collider bullet)
    256.     {
    257.         if (bullet.CompareTag("Bullet"))
    258.         {
    259.             enabled = false;
    260.         }
    261.     }
    262.  
    263.     private void OnEnable()
    264.     {
    265.         controls.Gameplay.Shoot.performed += ctx => Shoot();
    266.         controls.Gameplay.Shoot.Enable();
    267.  
    268.         controls.Gameplay.Move.performed += ctx => moveAxis = ctx.ReadValue<Vector2>();
    269.         controls.Gameplay.Move.canceled += ctx => moveAxis = Vector2.zero;
    270.         controls.Gameplay.Move.Enable();
    271.        
    272.         controls.Gameplay.Rotate.performed += ctx => rotateAxis = ctx.ReadValue<Vector2>();
    273.         controls.Gameplay.Rotate.canceled += ctx => rotateAxis = Vector2.zero;
    274.         controls.Gameplay.Rotate.Enable();
    275.  
    276.         controls.Gameplay.Run.performed += ctx => Run();
    277.         controls.Gameplay.Run.Enable();
    278.  
    279.         controls.Gameplay.Run.canceled += ctx => NotRun();
    280.         controls.Gameplay.Run.Enable();
    281.  
    282.         controls.Gameplay.Submit.performed += ctx => Shoot();
    283.         controls.Gameplay.Shoot.Enable();
    284.  
    285.     }
    286.  
    287.     private void OnDisable()
    288.     {
    289.         controls.Gameplay.Shoot.performed -= ctx => Shoot();
    290.         controls.Gameplay.Shoot.Disable();
    291.  
    292.         controls.Gameplay.Run.performed -= ctx => Run();
    293.         controls.Gameplay.Run.Disable();
    294.  
    295.         controls.Gameplay.Run.canceled -= ctx => NotRun();
    296.         controls.Gameplay.Run.Disable();
    297.  
    298.     }
    299. }
    I want to note that this code is fairly similar to the old version of the script that I have in my backup. Only thing that is different is the adjusted speeds for the ControlMouse and yes ControlMouse is still using the old Input System, keyboard and mouse was working fine before I jumped into the New Input System so I didn't want to mess with it and even for the old build, both worked just fine together.