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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Xbox controller input bug

Discussion in 'Scripting' started by timoshaddix, May 28, 2016.

  1. timoshaddix

    timoshaddix

    Joined:
    Jan 7, 2014
    Posts:
    64
    Hello,
    I've been trying to make my game work with an xbox controller, i kinda got it working, everything works execept for some axis which i need to invert.
    But it's moving by itself without any input.
    i have displayed the input on the screen and as you can see if acts weird.
    Once i stop giving input unity just goes back to a default input as it seems.



    I really hope someone can help me out!

    This is the code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour {
    5.  
    6.     public static float mouseSensitivityX = 200f;
    7.     public static float mouseSensitivityY = 200f;
    8.     public static float walkSpeed = 8f;
    9.     public GameObject cameraT;
    10.     Rigidbody rb;
    11.  
    12.     Vector3 moveAmount;
    13.     Vector3 smoothMoveVelocity;
    14.  
    15.     float verticalLookRotation;
    16.     // Use this for initialization
    17.     void Start () {
    18.         rb = GetComponent<Rigidbody>();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update () {
    23.         transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * Time.deltaTime * mouseSensitivityX);
    24.         verticalLookRotation += Input.GetAxis("Mouse Y") * Time.deltaTime * mouseSensitivityY;
    25.         verticalLookRotation = Mathf.Clamp(verticalLookRotation, -60, 60);
    26.         cameraT.transform.localEulerAngles = Vector3.left * verticalLookRotation;
    27.  
    28.         Vector3 moveDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized;
    29.         Vector3 targetMoveAmount = moveDir * walkSpeed;
    30.         moveAmount = Vector3.SmoothDamp(moveAmount, targetMoveAmount, ref smoothMoveVelocity, .15f);
    31.     }
    32.  
    33.     void FixedUpdate()
    34.     {
    35.         rb.MovePosition(rb.position + transform.TransformDirection(moveAmount) * Time.fixedDeltaTime);
    36.     }
    37. }
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Its not a 'bug' it has to do with the hardware registering values from the analog sticks as they may not be pristine. To solve this, you need to add a dead zone. If you are using the InputManager (which I think you are) increase the deadZone value. I usually use 0.2F.
     
  3. skalev

    skalev

    Joined:
    Feb 16, 2012
    Posts:
    264
  4. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    Try putting these values in for Gravity, Dead Zone, Sensitivity:
    Input.png
     
  5. timoshaddix

    timoshaddix

    Joined:
    Jan 7, 2014
    Posts:
    64
    Alright, thanks for the replies!
    It was indeed the dead zone,
    i managed to get it working expect for the Y axis of the left joystick, i am 100% sure that i have set the deadzone for it, but it is still keeping a constant value above 0, the rest works properly
     
    RavenOfCode likes this.
  6. timoshaddix

    timoshaddix

    Joined:
    Jan 7, 2014
    Posts:
    64
    EDIT: It only keeps a constant value above 0 when i slightly push the joystick, so when i push it all the way it doesn't do it.
     
    RavenOfCode likes this.
  7. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    Does it work in here? It shows deadzones and such.