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. Dismiss Notice

Movement With Joystick

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

  1. Mr_AgentFox

    Mr_AgentFox

    Joined:
    Jun 23, 2019
    Posts:
    59
    Hey! I am using the new Input System in unity and I am trying to rotate but I don't know if there is a button that is already set up to replace my "Mouse X" and "Mouse Y"? Not sure if there is a button already set up but if anyone could help me out with this that would be fantastic!

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class Controler : MonoBehaviour
    5. {
    6.  
    7.     public float runSpeed = 5f;
    8.     public float shiftSpeed = 10f;
    9.  
    10.     public float speedH = 2f;
    11.     public float speedV = 2f;
    12.  
    13.     private float yaw = 0.0f;
    14.     private float pitch = 0.0f;
    15.  
    16.     PlayerControls controls;
    17.  
    18.     Vector2 move;
    19.     Vector2 rotate;
    20.  
    21.     void Awake()
    22.     {
    23.         controls = new PlayerControls();
    24.  
    25.         controls.Gameplay.Movement.performed += ctx => move = ctx.ReadValue<Vector2>();
    26.         controls.Gameplay.Movement.canceled += ctx => move = Vector2.zero;
    27.  
    28.         controls.Gameplay.Rotation.performed += ctx => rotate = ctx.ReadValue<Vector2>();
    29.         controls.Gameplay.Rotation.canceled += ctx => rotate = Vector2.zero;
    30.     }
    31.  
    32.     void OnEnable()
    33.     {
    34.         controls.Gameplay.Enable();
    35.     }
    36.  
    37.     void Update()
    38.     {
    39.         transform.Translate(new Vector3(runSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, runSpeed * Input.GetAxis("Vertical") * Time.deltaTime), Space.Self);
    40.  
    41.         yaw += speedH * Input.GetAxis("Mouse X"); // What do I put here? Joystick X?
    42.         pitch -= speedV * Input.GetAxis("Mouse Y"); // What do I put here? Joystick Y?
    43.  
    44.         transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
    45.     }
    46.  
    47.     void OnDisable()
    48.     {
    49.         controls.Gameplay.Disable();
    50.     }
    51. }
    Thanks!
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,914
    ?

    I really don't understand what is your question. Do you have the Rotation entry in the GamePlay map in the Input system?
    If yes, you should have the rotation in the rotate Vector2.

     
  3. Mr_AgentFox

    Mr_AgentFox

    Joined:
    Jun 23, 2019
    Posts:
    59

    I followed Brackeys tutorial and the vector 2 wouldn’t rotate the player... I checked it twice
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,914
    ?
    Then debug it. Debug log the values from the vector2 right where you would calculate. If they have values then you're doing something wrong elsewhere if they are not changing then your input system setup is wrong somewhere.
     
  5. Mr_AgentFox

    Mr_AgentFox

    Joined:
    Jun 23, 2019
    Posts:
    59
    The movement code in the update method didn’t work either. Where do you imply I should put the Debug.Log(“”)?
     
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,914
    Well, you movement code does contain Input.GetAxis. See Brackey's tutorial again how he is calculating the movement and rotation. Also watch the beginning as well to check if you set up the input actions on the right way and you didn't forget to generate the C# class PlayerControls( for example, and you didn't change the class name or something).
     
  7. Mr_AgentFox

    Mr_AgentFox

    Joined:
    Jun 23, 2019
    Posts:
    59
    I did generate the class, I tried his movement 2 times and it didn’t work. Why would I need to change the class name?
     
  8. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,914
    You don't, that's the point if you gave something else when you generated the c# code (see the tutorial around 6:30).

    For movement, first step is to replace the Input.GetAxis calls with move.x and move.y as Brackey's doing in his tutorial. So you're doing it wrong.
     
  9. Mr_AgentFox

    Mr_AgentFox

    Joined:
    Jun 23, 2019
    Posts:
    59

    I followed the tutorial and then reverted to my code that actually worked.
     
  10. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,914
    What? Okay. If your code was working then what we're talking about here? I don't understand.
     
  11. Mr_AgentFox

    Mr_AgentFox

    Joined:
    Jun 23, 2019
    Posts:
    59

    I need to rotate my player with the right stick and I cant. is there something I can put to replace my "Mouse X" & "Mouse Y" so I can rotate with my right stick?
     
  12. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,914
    I told you: rotate.x and rotate.y and watch that frickin' tutorial again and carefully. You're doing things wrong.
     
  13. Mr_AgentFox

    Mr_AgentFox

    Joined:
    Jun 23, 2019
    Posts:
    59

    Thank you, for your consideration.
     
  14. barge9

    barge9

    Joined:
    May 13, 2015
    Posts:
    12
    Hello, I followed the Brackeys tutorial too, and was able to get everything working after some fussing with my own imput system config.... Instead of using each thumb stick direction (up, down, left, right) separately, Try setting up the Input System Action to just be one binding: Left Stick [Gamepad], and make sure the Action Type is "Value", and the Control Type is "Vector 2", hope that helps Screen Shot 2020-11-14 at 11.10.14 AM.png