Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2007
    Location
    Seattle
    Posts
    214

    X-Box 360 Controller Setup

    I wanted to take the third person controller system that 3DBuzz has given us and make my X-Box 360 controller work with it. During this process I was asked by a fellow MMO student how I accomplished this task. After working out a few bugs I have come up with a solution that is pretty basic but fairly easy to follow along. I thought I would share it with all of you. My hope is that some of you will find it useful.

    What I added will allow a player to rotate the camera's position around the character using the right thumbstick of a X-Box 360 Controller. Pressing down on the thumbstick will turn the camera control on and off. Pressing up and down on the D-pad arrows will move the camera towards and away from the character. I will not be going into moving the character with the left thumbstick because the third person camera system is already setup to do that.

    First off, I opened up Unity and navigated to the Input Manager. Here I added a total of 5 new axes to the size field.


    1. Open the Input Manager.
    2. Click on the arrow next to Axes to open up the options.
    3. Change the Size to 22, default is 17.
    4. Rename the newly created inputs.
    I used Horizontal_Alt, Vertical_Alt, ThumbStickButton_Right, DpadHorizontal, and DpadVertical. (Note: DpadHorizontal is not used in this tutorial but I am setting it up here for future use.)
    5. Change the settings of each of the new inputs.


    ThumbStick_Right was easy, I set the Positive Button to joystick button 9 and made sure Type was set to key or mouse button.The rest of them all have similar settings.
    -no button settings
    -Dead: 0.19
    -Sensitivity: 3
    -Type: Joystick Axis

    The one thing that is different for all of them is the Axis setting.
    Horizontal_Alt- 4th axis (joysticks)
    Vertical_Alt- 5th axis (joysticks)
    DpadHorizontal- 6th axis (joysticks)
    DpadVertical- 7th axis (joysticks)

    That is all that needs to be done in Unity. From here I jumped into Visual Studio and opened up TP_Camera.cs from the 3DBuzz VTMS.

    The first thing I needed to do here was add 3 more fields.
    Code:
    public float ThumbStickSensitivity = 1.25f; //ADDED: thumbstick controls
    public float DpadSensitivity = 0.5f;   //ADDED: d-pad controls
    private bool cameraMode = true; //ADDED: should we move the camera or a pointer?
    cameraMode is there because I wanted to add the ability to turn the camera control on and off with the click of a button. I might want to use the thumbstick to control the mouse pointer when the camera control is off.

    After that I wanted to change 2 more fields for readability. I used inputX instead of mouseX because it's going to be used by the 360 controller now too.
    Code:
    private float inputX = 0f; //CHANGED: mouseX
    private float inputY = 0f; //CHANGED: mouseY
    Next I wanted to be able to toggle cameraMode when the right thumbstick button is pressed so I created this method.
    Code:
    //Has the right thumbstick button been pressed?
        bool CheckCameraMode()
        {
            if(Input.GetButtonDown("ThumbStickButton_Right"))
            {
                if (cameraMode)
                    cameraMode = false; //TODO: Screen.showCursor = false;
                else
                    cameraMode = true; //TODO: Screen.showCursor = true;
            }
            return cameraMode;
        }
    After that I needed to go into HandlePlayerInput() and add a second dead zone variable. It is set to 0.3f because I have a very old controller and it needs a large dead zone.
    Code:
    var mouseDeadZone = 0.01f;
    var thumbstickDeadZone = 0.3f; //ADDED; old, old controller
    I have all of the information I need, now it's time to put it all together and make it work! If cameraMode is true then read the right thumbstick axes. If they are greater than the dead zone update the x and y inputs.
    Code:
    //move camera with right thumbstick when in camera mode        
            if (CheckCameraMode()) //check if in camera mode
            {
                if ((Input.GetAxis("Horizontal_Alt") < -thumbstickDeadZone || Input.GetAxis("Horizontal_Alt") > thumbstickDeadZone))
                    inputX += Input.GetAxis("Horizontal_Alt") * ThumbStickSensitivity;
    
                if (Input.GetAxis("Vertical_Alt") > thumbstickDeadZone || Input.GetAxis("Vertical_Alt") < -thumbstickDeadZone)
                    inputY -= Input.GetAxis("Vertical_Alt") * ThumbStickSensitivity;
    
                Debug.Log(new Vector2(Input.GetAxis("Horizontal_Alt"), Input.GetAxis("Vertical_Alt"))); //report value of right thumbstick
            }
            else {} //TODO: move mouse cursor with right thumbstick
    I used the Debug.Log call to make sure the thumbstick was being read by Unity.

    And finally after clamping inputY and checking to see if the D-pad up or down arrow is being pressed, change the camera distance accordingly.
    Code:
    //adjust the camera distance from the d-pad
            if (CheckCameraMode())
            {
                if (Input.GetAxis("DpadVertical") != 0)
                {
                    desiredDistance = Mathf.Clamp(Distance + Input.GetAxis("DpadVertical") * DpadSensitivity, DistanceMin, DistanceMax);
                    preOccludedDistance = desiredDistance;
                    distanceSmooth = DistanceSmooth;
                }
            }
    Comments, questions and suggestions for improvement are always welcome.

  2. #2
    Join Date
    Dec 2009
    Location
    United Kingdom
    Posts
    60
    Thanks Lerich, I'll try this out once I'm done watching the rest of the unity camera system videos.

  3. #3
    Join Date
    Apr 2007
    Location
    Seattle
    Posts
    214
    Quote Originally Posted by zOrigin View Post
    Thanks Lerich, I'll try this out once I'm done watching the rest of the unity camera system videos.
    Let me know if you have any problems when you try it out. I'll be glad to try and help clear up anything confusing or if it's not working for you.

  4. #4
    Join Date
    Mar 2011
    Posts
    1
    this is working fine thank you,now im trying to implement rotation instead of strafe, on an xbox controller you need your right thumb free for buttons most of the time.
    Last edited by Rik333; 03-15-2011 at 07:57 PM.

  5. #5
    Join Date
    Jan 2011
    Posts
    6
    Thank you very much for sharing this with us!
    @Rik333: Have you tried to implement rotation instead of strafe?

  6. #6
    Join Date
    Aug 2007
    Posts
    7
    Quote Originally Posted by Rik333 View Post
    this is working fine thank you,now im trying to implement rotation instead of strafe, on an xbox controller you need your right thumb free for buttons most of the time.
    I'd be interested in learning rotation as well.

    I'm also having issues getting the camera control to work on the 360 controller, but i wanna see if I can get it working myself before i ask for help.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •