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

Using Input System - I want to rotate a 2D on the Z axis (only) using the right thumbstick

Discussion in 'Scripting' started by caddi, Apr 2, 2020.

  1. caddi

    caddi

    Joined:
    Mar 23, 2020
    Posts:
    13
    I'm using the following script that I got from a Brackey's tutorial. It' working perfectly, except the object is rotating on all three axis, I'd like it to only rotate on the Z axis when I using the right thumbstick. How can I modify the code to do this? Thanks!

    Code (CSharp):
    1.  
    2. public class PlayerController : MonoBehaviour
    3. {
    4.     PlayerControls controls;
    5.     public float playerSpeed;
    6.     public float playerRotateSpeed;
    7.     Vector2 move;
    8.     Vector2 rotate;
    9.    
    10.     private void Awake() {
    11.         controls = new PlayerControls();
    12.  
    13.         controls.Gameplay.Move.performed += ctx => move = ctx.ReadValue<Vector2>();
    14.         controls.Gameplay.Move.canceled += ctx => move = Vector2.zero;
    15.         controls.Gameplay.Rotate.performed += ctx => rotate = ctx.ReadValue<Vector2>();
    16.         controls.Gameplay.Rotate.canceled += ctx => rotate = Vector2.zero;
    17.     }
    18.  
    19.     private void Update() {
    20.         Vector2 m = new Vector2(move.x, move.y) * playerSpeed * Time.deltaTime;
    21.         transform.Translate(m, Space.World);
    22.  
    23.         Vector2 r = new Vector2(-rotate.y, -rotate.x) * playerRotateSpeed * Time.deltaTime;
    24.         transform.Rotate(r, Space.World);
    25.     }
    26.  
    27.     private void OnEnable() {
    28.         controls.Gameplay.Enable();
    29.     }
    30.     private void OnDisable() {
    31.         controls.Gameplay.Disable();
    32.     }
    33. }
    34.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    I think you would replace line 23 above with:

    <code deleted>

    Correction to above... try this instead:

    Code (csharp):
    1. Vector3 r = Vector3.forward * rotate.magnitude * playerRotateSpeed * Time.deltaTime;
    If you want bidirectional movement, you could capture only one axis like this:

    Code (csharp):
    1. Vector3 r = Vector3.forward * rotate.x * playerRotateSpeed * Time.deltaTime;
     
    caddi likes this.
  3. caddi

    caddi

    Joined:
    Mar 23, 2020
    Posts:
    13
    Hi @Kurt-Dekker

    Thanks for your reply, but that would negate the rotate input variables from the controller that are created in lines 15 and 16. You are on to something I think though, because when I use your line of code my player is rotating on the z axis the way I want but it's just continuously rotating, even without any input.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Agreed... I think my first post was that way. I amended it above.

    As far as negation, just use
    -rotate.x
    if you want it the other way.
     
    caddi likes this.
  5. caddi

    caddi

    Joined:
    Mar 23, 2020
    Posts:
    13
  6. caddi

    caddi

    Joined:
    Mar 23, 2020
    Posts:
    13
    @Kurt-Dekker can I pick your brain with one more thing? It's working as I want; however it's responding similar to using Input.GetAxisRaw() in that is response to changes immediately. Do you know if there's an easy change similar to using Input.GetAxis() that will create that bit of momentum lag when changing directions? Hope what I'm saying makes sense.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Probably what you want is to track the motion amount each frame when the finger is down, storing it as a float.

    After the input returns to zero, instead start using that stored value as input, but slowly reduce that stored value toward zero each frame. That way if the user slows it almost to a stop before they let go, you'll have it coast at that really-reduced value.

    You can even say on finger lift, if it is below a certain amount, just set it to zero.