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

Cross-Platfrom-Input Animator scripting

Discussion in 'Scripting' started by ApteryxK, Apr 5, 2015.

  1. ApteryxK

    ApteryxK

    Joined:
    Feb 8, 2015
    Posts:
    40
    Hello,
    :p :)
    I am trying to make a script to run my animations when the player is moving. But I am using a Joystick with Crossplatfrom stuff.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerScript : MonoBehaviour {
    5.  
    6.  
    7.     Animator anim;
    8.     float speed;
    9.     void Start () {
    10.         anim = GetComponent<Animator>();
    11.         }
    12.  
    13.  
    14.     void Update () {
    15.         //Movement
    16.         speed = Input.GetAxis ("Vertical"); //[B]Here I actually would like to get the CrossPlatfrom Axis,
    17. //I thought it was realted input Axises first, but I am no longer sure.[/B]
    18.         anim.SetFloat ("Speed", speed);
    19.  
    20.         //Jump
    21.         if( Input.GetAxis ("Jump") > 0)
    22.         {
    23.             anim.SetBool("Jump", true);
    24.         }
    25.         Debug.Log(speed);
    26.         //Crouch
    27.         }
    28. }
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.EventSystems;
    4.  
    5. namespace UnityStandardAssets.CrossPlatformInput
    6. {
    7.     public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
    8.     {
    9.         public enum AxisOption
    10.         {
    11.             // Options for which axes to use
    12.             Both, // Use both
    13.             OnlyHorizontal, // Only horizontal
    14.             OnlyVertical // Only vertical
    15.         }
    16.  
    17.         public int MovementRange = 100;
    18.         public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
    19.         public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
    20.         public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
    21.  
    22.         Vector3 m_StartPos;
    23.         bool m_UseX; // Toggle for using the x axis
    24.         bool m_UseY; // Toggle for using the Y axis
    25.         CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
    26.         CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input
    27.  
    28.         void OnEnable()
    29.         {
    30.             m_StartPos = transform.position;
    31.             CreateVirtualAxes();
    32.         }
    33.  
    34.         void UpdateVirtualAxes(Vector3 value)
    35.         {
    36.             var delta = m_StartPos - value;
    37.             delta.y = -delta.y;
    38.             delta /= MovementRange;
    39.             if (m_UseX)
    40.             {
    41.                 m_HorizontalVirtualAxis.Update(-delta.x);
    42.             }
    43.  
    44.             if (m_UseY)
    45.             {
    46.                 m_VerticalVirtualAxis.Update(delta.y);
    47.             }
    48.         }
    49.  
    50.         void CreateVirtualAxes()
    51.         {
    52.             // set axes to use
    53.             m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
    54.             m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
    55.  
    56.             // create new axes based on axes to use
    57.             if (m_UseX)
    58.             {
    59.                 m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
    60.                 CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
    61.             }
    62.             if (m_UseY)
    63.             {
    64.                 m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
    65.                 CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
    66.             }
    67.         }
    68.  
    69.  
    70.         public void OnDrag(PointerEventData data)
    71.         {
    72.             Vector3 newPos = Vector3.zero;
    73.  
    74.             if (m_UseX)
    75.             {
    76.                 int delta = (int)(data.position.x - m_StartPos.x);
    77.                 delta = Mathf.Clamp(delta, - MovementRange, MovementRange);
    78.                 newPos.x = delta;
    79.             }
    80.  
    81.             if (m_UseY)
    82.             {
    83.                 int delta = (int)(data.position.y - m_StartPos.y);
    84.                 delta = Mathf.Clamp(delta, -MovementRange, MovementRange);
    85.                 newPos.y = delta;
    86.             }
    87.             transform.position = new Vector3(m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z + newPos.z);
    88.             UpdateVirtualAxes(transform.position);
    89.         }
    90.  
    91.  
    92.         public void OnPointerUp(PointerEventData data)
    93.         {
    94.             transform.position = m_StartPos;
    95.             UpdateVirtualAxes(m_StartPos);
    96.         }
    97.  
    98.  
    99.         public void OnPointerDown(PointerEventData data) { }
    100.  
    101.         void OnDisable()
    102.         {
    103.             // remove the joysticks from the cross platform input
    104.             if (m_UseX)
    105.             {
    106.                 m_HorizontalVirtualAxis.Remove();
    107.             }
    108.             if (m_UseY)
    109.             {
    110.                 m_VerticalVirtualAxis.Remove();
    111.             }
    112.         }
    113.     }
    114. }
    I am trying to make speed the value of Vertical Axis of my Crossplatfrom input, but I think that GetAxis and CrossPlatfrom are different stuff.

    How do I refer to CrossPlatfromAxis Vertical.

    I am using it with a joystick for my mobile game.

    I couldn't find any documentation about CrossPlatformAxis :/
    Thanks :D !
    Help would be appreciated. :)
     
    Last edited: Apr 5, 2015
  2. ApteryxK

    ApteryxK

    Joined:
    Feb 8, 2015
    Posts:
    40
    Is CrossPlatformInputManager an actual class ? I am really confused.