Joystick in unity

0

I'm doing a little application in unity, a sphere with two joysticks, the first joystick moves to that sphere and the second one zooms, the problem is that I do not know how to call the second joysctik to write the necessary code, because what I have done and the configuration goes to the first joystick.

This is the one joystick code

public class Controller : MonoBehaviour {

    private Joystick joystick;
    private JoyButton joybutton;
    public float velocidad;

    // Use this for initialization
    void Start ()
    {
        joystick = FindObjectOfType<Joystick>();
    }

    // Update is called once per frame
    void Update ()
    {
        var rb = GetComponent<Rigidbody>();

        rb.velocity = new Vector3(joystick.Horizontal * velocidad, 0.0f, joystick.Vertical * velocidad);

    }
}
    
asked by Sergio Roger 15.06.2018 в 22:31
source

1 answer

0

a serious option:

public class Controller : MonoBehaviour {

 private Joystick joystickZoom;
 private Joystick joystickMove;

 private JoyButton joybutton;
 public float velocidad;

 // Use this for initialization
 void Start ()
 {
     joystickZoom = GameObject.FindGameObjectWithTag("zoom").GetComponent<Joystick>();
     joystickMove = GameObject.FindGameObjectWithTag("move").GetComponent<Joystick>();
 }

 // Update is called once per frame
 void Update ()
 {
     /* Lógica para cualquier caso. */

 }
}
  • You could also do it with lists, arrays so as not to do two "joystick" variables. Putting the joystick class the name if you wish.

    List<Joystick> Joysticks;
    
  • You also have the option to take the "canvas" gameobject and search for them in your children by name or index.

    GameObject.Find("canvas").transform.GetChild(0).GetComponent<Joystick>;
    GameObject.Find("canvas").transform.Find("Fixed Joystick zoom").GetComponent<Joystick>;
    

There are many ways to do it, this is the fastest, you also have the option to search by name.

joystickZoom = GameObject.Find("Fixed Joystick zoom").GetComponent<Joystick>();

Greetings.

    
answered by 17.06.2018 в 07:44