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
- Pass a parameter called joystick_tag. (like this: Get3DJoystickPosition(joystick_tag))
- 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));
- 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;
}
}
}