- Home /
Is there a way to get the actual joystick.Vertical and joystick.Horizontal Value?,
First, sorry for my bad english. I'm 15, and 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));
3. 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;
}
}
}
Answer by Llama_w_2Ls · Aug 27, 2020 at 11:39 AM
Well the joystick.Horizontal and joystick.Vertical are able to be represented as floats, going from 0 (stationary) to 1(max length). If you want to see the values then create a public vector2 and assign the two parameters as .Horizontal and .Vertical. For example:
public Vector2 JoystickValues;
public VariableJoystick joystick
void Update()
{
JoystickValues = new Vector2(joystick.Horizontal, joystick.Vertical);
}