我想自己做一个moba游戏。
我想做的是
)
Camera.main.ScreenToWorldPoint(new Vector3((joystick.Horizontal + 1) * Screen.width / 2, (joystick.Vertical + 1) * Screen.height / 2, Camera.main.nearClipPlane));这看起来很简单,但是在我的代码中,即使我移动操纵杆,joystick.Horizontal和joystick.Vertical值也保持为0。
基本上,该方法获得一个名为joystick_tag的字符串输入。然后,通过标记,每次调用该方法时,它都会发现一个由标记标记的操纵杆。然后调用Update()方法,以便获得当前的joystick.Horizontal和joystick.Vertical。
这是完整代码。操纵杆类来自Fenerax工作室的“欢乐棒包”:
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;
}
}
}发布于 2020-08-27 10:55:48
抱歉,但你的代码看上去一团糟。我强烈建议你在做一个MOBA之前先上一门课程或者使用团结学习。你不应该亲自打电话给Update()。统一叫它自己。要回答您的问题,joystick.Horizontal和joystick.Vertical应该返回实际值。
https://stackoverflow.com/questions/63613027
复制相似问题