如何通过点击改变操纵杆位置?
How to change joystick position by tap?
我是 Unity 的新手,最近我正在为操纵杆的问题而苦恼。我想通过手指点击来改变操纵杆的位置。例如,如果我触摸屏幕中央,操纵杆也会显示在中央。
这是我的代码:
private void Update()
{
Touch firstTap = Input.touches[0];
if (firstTap.phase == TouchPhase.Began)
{
m_StartPos = firstTap.position;
transform.position = m_StartPos;
UpdateVirtualAxes(m_StartPos);
}
else if (firstTap.phase == TouchPhase.Ended)
{
m_StartPos = new Vector3(500f, 500f, 0);
transform.position = m_StartPos;
UpdateVirtualAxes(m_StartPos);
}
}
我在操纵杆脚本中添加了Update()
函数。当我点击屏幕时,位置确实发生了变化,但是当我移动手指时,onGrag()
永远不会被调用。
您可以创建一个 class,例如 JoystricManager,您将在其中拥有一个 Update() 方法。在 Update() 中,您将检查 Input.GetMouseButtonDown(0) - which will be true when user taps the screen. Add a collider to you joystick GameObject and after getting click check if this click is inside the collider. If click is inside - ignore it and just check in your joystick script Input.MousePosition 以检查操纵杆本身的移动并执行您的自定义操作(移动播放器等)。如果点击将在操纵杆碰撞器之外 - 只需移动操纵杆
到新职位。将点击位置保存在你的摇杆中,以获得点击的起始坐标。当用户移动手指时,您将获得新的鼠标位置,使用这两个点,您可以计算出操纵杆指向的位置。
我是 Unity 的新手,最近我正在为操纵杆的问题而苦恼。我想通过手指点击来改变操纵杆的位置。例如,如果我触摸屏幕中央,操纵杆也会显示在中央。
这是我的代码:
private void Update()
{
Touch firstTap = Input.touches[0];
if (firstTap.phase == TouchPhase.Began)
{
m_StartPos = firstTap.position;
transform.position = m_StartPos;
UpdateVirtualAxes(m_StartPos);
}
else if (firstTap.phase == TouchPhase.Ended)
{
m_StartPos = new Vector3(500f, 500f, 0);
transform.position = m_StartPos;
UpdateVirtualAxes(m_StartPos);
}
}
我在操纵杆脚本中添加了Update()
函数。当我点击屏幕时,位置确实发生了变化,但是当我移动手指时,onGrag()
永远不会被调用。
您可以创建一个 class,例如 JoystricManager,您将在其中拥有一个 Update() 方法。在 Update() 中,您将检查 Input.GetMouseButtonDown(0) - which will be true when user taps the screen. Add a collider to you joystick GameObject and after getting click check if this click is inside the collider. If click is inside - ignore it and just check in your joystick script Input.MousePosition 以检查操纵杆本身的移动并执行您的自定义操作(移动播放器等)。如果点击将在操纵杆碰撞器之外 - 只需移动操纵杆 到新职位。将点击位置保存在你的摇杆中,以获得点击的起始坐标。当用户移动手指时,您将获得新的鼠标位置,使用这两个点,您可以计算出操纵杆指向的位置。