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

Unity Cross Platform Input Standard Asset (Joystick doesn't work)

Discussion in 'Scripting' started by Mohelm97, Jul 14, 2015.

  1. Mohelm97

    Mohelm97

    Joined:
    Feb 6, 2013
    Posts:
    11
    Hi all :)
    it's my first post here and at first i'm sorry for my bad English :D
    i'm trying to use this Joystick in Unity 5.1.1f1 (the default one in Standard Assets)

    but i see it always goes to the right-bottom corner and you can't go left and bottom with it
    "Standard Assets/CrossPlatformInput/Scripts/Joystick.cs" in this file i found this

    Code (CSharp):
    1. void OnEnable()
    2. {
    3.      m_StartPos = transform.position;
    4.      CreateVirtualAxes();
    5. }
    this set the start position but "transform.position" always return (0,0,0) so i think OnEnable called before the position for "RectTransform" set so i edit it to
    Code (CSharp):
    1. void OnEnable()
    2. {
    3.      CreateVirtualAxes();
    4. }
    5. void Start()
    6. {
    7.      m_StartPos = transform.position;
    8. }
    and it's work like charm

    NOW, i post this to ask if i did any mistake or to ask why it's not working ? (heey i'm a little noob)

    Best wishes ;)
     
  2. SirBoboHobo

    SirBoboHobo

    Joined:
    Feb 26, 2015
    Posts:
    41
    I have set this up already and it works correctly for me, in the joystick script change the transform.position line to this
    Code (CSharp):
    1. transform.position = Vector3.ClampMagnitude(new Vector3(newPos.x, newPos.y, newPos.z), MovementRange) + m_StartPos;
    this will make it go in a circle and stay in the middle.

    as for setting it up in your player script use the standartassets. put a
    Code (CSharp):
    1. using UnityStandardAssets.CrossPlatformInput;
    in the top of your script and then do this
    Code (CSharp):
    1.  
    2. Vector3 movement
    3. void FixedUpdate(){
    4. float h = CrossPlatformInputManager.GetAxisRaw ("Horizontal");
    5.         float v = CrossPlatformInputManager.GetAxisRaw ("Vertical");
    6.  
    7.         Move (h, v);
    8. }
    9. void Move(float h, float v)
    10.     {
    11.         movement.Set (h, v, 0f);
    12.  
    13.         movement = movement.normalized * speed * Time.deltaTime;
    14.  
    15.         playerRigidbody2d.MovePosition (transform.position + movement);
    16.     }
    17.  
    of course in the awake get the rigidbody, and change the values as you like, this is a 2d game, for 3d change the Z.
     
    Mohelm97 likes this.
  3. Mohelm97

    Mohelm97

    Joined:
    Feb 6, 2013
    Posts:
    11
    Thanks a lot SirBoboHobo :)
    it's great to make it work in circle not rectangle :D, i use it and it's work will but i'm asking about OnEnable and setting m_position ?
     
  4. SirBoboHobo

    SirBoboHobo

    Joined:
    Feb 26, 2015
    Posts:
    41
    I haven't modified a thing in the standard assets and I recommend you don't either, it has properties from different scripts, it is programmed really good for using, here's a tutorial on this subject, should answer your questions



    Also, the scripts above should do the job getting it back to our starting position worked for me at least :)