Production of Unity joystick and minimap

Need to import plug-in

 Joystick control code: MoveController

public class MoveController : MonoBehaviour {

    private Animator an;
    private void Start()
    {
        an = GetComponent<Animator>();
    }
    void OnEnable()
    {
        EasyJoystick.On_JoystickMove += OnJoystickMove;
        EasyJoystick.On_JoystickMoveEnd += OnJoystickMoveEnd;
    }

    void OnDisable()
    {
        EasyJoystick.On_JoystickMove -= OnJoystickMove;
        EasyJoystick.On_JoystickMoveEnd -= OnJoystickMoveEnd;
    }

    void OnDestroy()
    {
        EasyJoystick.On_JoystickMove -= OnJoystickMove;
        EasyJoystick.On_JoystickMoveEnd -= OnJoystickMoveEnd;
    }


    void OnJoystickMoveEnd(MovingJoystick move)
    {
        if (move.joystickName == "MoveJoystick")
        {
            an.SetFloat("Run", 0);
        }
    }
    void OnJoystickMove(MovingJoystick move)
    {
        if (move.joystickName != "MoveJoystick")
        {
            return;
        }
        

        float joyPositionX = move.joystickAxis.x;
        float joyPositionY = move.joystickAxis.y;

        if (joyPositionY != 0 || joyPositionX != 0)
        {
            //设置角色的朝向(朝向当前坐标+摇杆偏移量)
           transform.LookAt(new Vector3(transform.position.x + joyPositionX, transform.position.y, transform.position.z + joyPositionY));
            //移动玩家的位置(按朝向位置移动)
            transform.Translate(Vector3.forward * Time.deltaTime * 5);
            //播放奔跑动画
            an.SetFloat("Run", 1);
        }
    }
}

After importing the joystick, please note:

 

Modify value based on screen size

The name of the joystick here needs to be the same as the joystick code in the MoveController code.

 if (move.joystickName == "MoveJoystick")
        {
            an.SetFloat("Run", 0);
        }

 

 Things to note when importing minimaps

1. Add layer layer

2. The mini map needs to be bound to a character

 

 3. Switch the layer and change the character's layer to mapsystem. Otherwise, the mini map will not move according to the movement of the character, but will move according to the movement of the joystick.

 

Guess you like

Origin blog.csdn.net/m0_71624363/article/details/129541987