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

Joystick Movement

Discussion in 'PSM' started by thommoboy, Apr 8, 2014.

  1. thommoboy

    thommoboy

    Joined:
    Jul 20, 2012
    Posts:
    59
    The documentation shows the axis's but how do i apply this to my script?

    like this:

    MoveDirection = new Vector3(Input.GetAxis("left stick horizontal axis"), 0, Input.GetAxis("left stick vertical axis"));

    UPDATE:

    i got it working with this code:

    Code (csharp):
    1. if(RotXY == RotationAxis.MouseX)
    2.         {
    3.             RotationX += Input.GetAxisRaws("right stick horizontal axis") * SensitivityX * Time.deltaTime;
    4.             RotationX = ClampAngle(RotationX, MinimumX, MaximumX);
    5.             Quaternion XQuaternion  = Quaternion.AngleAxis(RotationX, Vector3.up);
    6.             _MyTransform.localRotation = OriginalRotation * XQuaternion;
    7.         }
    however my player spins around endlessly

    here is the full code:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FPSMouseLook : MonoBehaviour {
    5.  
    6.     public enum RotationAxis
    7.     {
    8.         MouseX = 1,
    9.         MouseY = 2
    10.     }
    11.  
    12.     public RotationAxis RotXY = RotationAxis.MouseX | RotationAxis.MouseY;
    13.  
    14.     public float SensitivityX = 400f;
    15.     private float MinimumX = -360f;
    16.     private float MaximumX = 360f;
    17.     private float RotationX = 0f;
    18.  
    19.     public float SensitivityY = 400f;
    20.     private float MinimumY = -75f;
    21.     private float MaximumY = 75f;
    22.     private float MinimumYRun = -50f;
    23.     private float MaximumYRun = 50f;
    24.     private float RotationY = 0f;
    25.  
    26.     public Quaternion OriginalRotation;
    27.  
    28.     private Transform _MyTransform;
    29.    
    30.     void Start()
    31.     {
    32.         Application.targetFrameRate = 60;
    33.         _MyTransform = transform;
    34.         OriginalRotation = _MyTransform.localRotation;
    35.         Screen.lockCursor = true;
    36.     }
    37.  
    38.     void Update()
    39.     {
    40.         if(RotXY == RotationAxis.MouseX)
    41.         {
    42.             RotationX += Input.GetAxis("right stick horizontal axis") * SensitivityX * Time.smoothDeltaTime;
    43.             RotationX = ClampAngle(RotationX, MinimumX, MaximumX);
    44.             Quaternion XQuaternion = Quaternion.AngleAxis(RotationX, Vector3.up);
    45.             _MyTransform.localRotation = OriginalRotation * XQuaternion;
    46.         }
    47.  
    48.         if(RotXY == RotationAxis.MouseY)
    49.         {
    50.             //RotationY += Input.GetAxis("right stick vertical axis") * SensitivityY * Time.deltaTime;
    51.             //RotationY = ClampAngle(RotationY, MinimumY, MaximumY);
    52.             //Quaternion YQuaternion = Quaternion.AngleAxis(RotationY, Vector3.left);
    53.             //_MyTransform.localRotation = OriginalRotation * YQuaternion;
    54.         }
    55.     }
    56.  
    57.     public static float ClampAngle(float Angle, float Min, float Max)
    58.     {
    59.         if(Angle < -360)
    60.         {
    61.             Angle += 360;
    62.         }
    63.  
    64.         if(Angle > 360)
    65.         {
    66.             Angle -= 360;
    67.         }
    68.  
    69.         return Mathf.Clamp(Angle, Min, Max);
    70.     }
    71.    
    72.     void OnGUI()
    73.     {
    74.         GUI.Label(new Rect(Screen.width / 2, Screen.height / 2, 200, 200), "Right H: " + Input.GetAxis("right stick horizontal axis").ToString() + " Right V: " + Input.GetAxis("right stick vertical axis").ToString() + " H Raw: " + Input.GetAxisRaw("right stick horizontal axis").ToString() + " V Raw: " + Input.GetAxisRaw("right stick vertical axis").ToString());
    75.     }
    76. }
     
    Last edited: Apr 8, 2014
    Monstalegend234 likes this.
  2. thommoboy

    thommoboy

    Joined:
    Jul 20, 2012
    Posts:
    59
    right stick horizontal axis 4th axis left: negative, right: positive
    right stick vertical axis 5th axis up: negative, down: positive
    right/left button 6th axis left: -1, right: 1, default: 0

    why is there no default for the sticks?
     
    Last edited: Apr 8, 2014
  3. PeteD

    PeteD

    Joined:
    Jul 30, 2013
    Posts:
    71
    The sticks are analog so what would they default to exactly?

    With analog sticks the best you can do is define a dead zone.
     
  4. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
    Here is an example project that maps all the input to various actions.

    Joysticks (x/y/4th/5th axis) are mapped in the InputManager, and read from scripts.
    Buttons are read directly with Input.GetKey(<string>).
    Touch uses the standard Input.GetTouch() etc.
     

    Attached Files:

    amazingjaba likes this.
  5. thommoboy

    thommoboy

    Joined:
    Jul 20, 2012
    Posts:
    59
    Thanks eriQue :) i have one more problem i'll post it here in a minute
    EDIT:

    are realtime shadows possible on the vita? whenever i run my game they dissapear
     
    Last edited: Apr 9, 2014
  6. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    377

    Wow thanks :D If i get stuck at all i'l be sure to check this out!
     
  7. PeterD

    PeterD

    Joined:
    Feb 6, 2013
    Posts:
    120
    Thanks for the example project eriQue!

    Is there a pool of these types of examples somewhere we can all access?
     
  8. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    377
    Got my touch screen game working on vita, analogue sticks, triggers and everything :) haha so awesome, beats touch screen controls anyday!
     
  9. danish115

    danish115

    Joined:
    Aug 28, 2014
    Posts:
    46
    i am making top down game like angrybot. i am using angrybots script for my player. the player moves and rotate well with both right and left joysticks but i can not handle its animation. My player has its own animation. I do not know what to do. specially firing animation is not running with angrybot script. any suggestions ??