user12243518
user12243518

Reputation:

Is there a way to get the actual joystick.Vertical and joystick.Horizontal Value?

I'm trying to make a moba game by my own.

What I want to do is

  1. Pass a parameter called joystick_tag. (like this: Get3DJoystickPosition(joystick_tag))
  2. Get current joystick.Horizontal, joystick.Vertical, Camera.main so that I can calculate the value:
Camera.main.ScreenToWorldPoint(new Vector3((joystick.Horizontal + 1) * Screen.width / 2, (joystick.Vertical + 1) * Screen.height / 2, Camera.main.nearClipPlane));
  1. return that value.

It seems quite simple, but in my code the joystick.Horizontal and the joystick.Vertical value keeps 0 even if I move the joystick.

Basically, the method gets a string input, called joystick_tag. then by the tag, every time the method gets called, it finds a joystick tagged by the tag. Then it calls the Update() method so that I can get the current joystick.Horizontal and joystick.Vertical.

This is the full code. Joystick class is from the Joystick Pack by Fenerax Studios:

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

namespace Korselo.JoyPos
{
    public class JoyPos : MonoBehaviour
    {
        public Joystick joystick;
        public Vector3 world_v;
        public string joystick_tag_public;
        // Start is called before the first frame update
        void Start()
        {
          
        }

        // Update is called once per frame
        void Update()
        {
            world_v = Camera.main.ScreenToWorldPoint(new Vector3((joystick.Horizontal + 1) * Screen.width / 2, (joystick.Vertical + 1) * Screen.height / 2, Camera.main.nearClipPlane));

            Debug.Log(world_v);
        }

        public Vector3 Get3DJoystickPosition(string joystick_tag)
        {
            GameObject joystickGameObject = GameObject.FindGameObjectWithTag(joystick_tag);
            if (joystickGameObject != null)
            {
                joystick = joystickGameObject.GetComponent<Joystick>();
            }
            Update();
            return world_v;
        }
    }
}

Upvotes: 0

Views: 391

Answers (1)

Yern
Yern

Reputation: 331

Sorry, but your code looks like a mess. I highly suggest you take a course or use Unity Learn to begin with before making a MOBA. And you shouldn't call Update() yourself. Unity calls it himself. To answer your question, joystick.Horizontal and joystick.Vertical should return you the actual values.

Upvotes: 0

Related Questions