Get two Joysticks working for same Movement ?

Hi, I have two joystick buttons on canvas and i want them both to move character. I defined joystick in script and it works well for the first one but second. If i disable first one then second will work. How can i get both work for same movement ?

So noone knows ? :frowning:

What exactly do you mean by “joystick buttons on canvas”?

Hi @Antistone I’ve two joysticks and i want each of them to work for same movement function. But only one of them works. So if i remove first one then second will work. Same goes for other option. So i can’t get both work. I’ve shared screenshot of scene and code that i used. Can you help me?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CoolJoystick;

public class Movement : MonoBehaviour
{
    public static float playerThrust = 360f;

    public Rigidbody2D rb_Player;

    protected Joystick _joyStick;

    void Awake()
    {
        rb_Player = GetComponent<Rigidbody2D>();

        _joyStick = FindObjectOfType<Joystick>();
    }

    void FixedUpdate()
    {
        MobileMovement();
    }

    void MobileMovement()
    {
        Vector3 mobileDirection = Vector3.Normalize(new Vector3(_joyStick.Horizontal, _joyStick.Vertical, 0));
        rb_Player.velocity = mobileDirection * palyerThrust;
    }
}

It looks like your code is just searching for the first Joystick it can find (via FindObjectOfType). If you want to search for ALL the Joysticks, you can use FindObjectsOfType instead (note the plural).

Obviously, you would also need to store those joysticks in some form of collection rather than a single Joystick variable, and you’d need to write some logic for combining their inputs into a single result (the simplest option that occurs to me would be to sum them).

It’s possible that some sort of wrappers already exist for combining input from multiple Joysticks, but I’m not familiar with the Joystick class and I can’t seem to find it in the Unity Scripting API docs.

1 Like