当相机移动时,如何使内部操纵杆保持在屏幕上?
我是第一次开发手机游戏,及其2D自上而下的射手。我有一个问题,当相机移动跟随播放器时,操纵杆的中部被抛在后面。当相机静止时,一切正常,但是我真的不知道如何在相机移动时使中央操纵杆保持在正确的位置。我真的很感谢我如何做到这一点,但我宁愿自己写代码以进行自我完善。
(我知道代码是一团糟,对不起)
void Update()
{
touchNum = Input.touchCount;
if (touchNum > 0)
{
for (int i = 0; i <= (touchNum - 1); i++)
{
touch = Input.GetTouch(i);
touchPosition1 = Camera.main.ScreenToWorldPoint(touch.position);
if (area.OverlapPoint(touchPosition1))
{
switch (touch.phase)
{
case TouchPhase.Began:
startPos = touchPosition1;
joyBack.transform.position = startPos;
joyFront.transform.position = startPos;
joyBack.SetActive(true);
joyFront.SetActive(true);
joyFront.transform.position = startPos;
joyBack.transform.position = startPos;
Debug.Log("begun");
break;
case TouchPhase.Moved:
direction = touchPosition1 - startPos;
Vector2 touchPosition2 = Vector2.ClampMagnitude(direction, 1.75f);
horizontal = touchPosition2.x;
vertical = touchPosition2.y;
//Debug.Log(horizontal);
distance = horizontal * horizontal + vertical * vertical;
distance = Mathf.Sqrt(distance);
direction = direction / distance;
direction = Limiters(direction, limit);
joyFront.transform.position =new Vector2(startPos.x + touchPosition2.x, startPos.y + touchPosition2.y);
Debug.DrawLine(startPos, startPos + touchPosition2);
Debug.Log(direction);
break;
case TouchPhase.Ended:
distance = 0;
Debug.Log("Ended");
joyBack.SetActive(false);
joyFront.SetActive(false);
break;
}
}
else
{
joyBack.SetActive(false);
joyFront.SetActive(false);
distance = 0;
}
}
}
}
private static Vector2 Limiters(Vector2 direction, float limit)
{
if (direction.x > limit)
{
direction.x = limit;
}
else if (direction.x < -limit)
{
direction.x = -limit;
}
if (direction.y > limit)
{
direction.y = limit;
}
else if (direction.y < -limit)
{
direction.y = -limit;
}
return direction;
}
private void FixedUpdate()
{
move.Translate((direction) * movespeed * distance * Time.fixedDeltaTime);
}
}
I'm developing a mobile game for the first time, and its 2D top-down shooter. I'm having an issue where the middle part of the joystick gets left behind when the camera moves to follow the player. Everything works fine when the camera is stationary, but I really have no idea how to make the central joystick stay in the correct position when the camera moves. I would really appreciate a suggestion on how I could do this, but I'd rather write the code myself for self-improvement.
(I know the code is a mess, I'm sorry)
void Update()
{
touchNum = Input.touchCount;
if (touchNum > 0)
{
for (int i = 0; i <= (touchNum - 1); i++)
{
touch = Input.GetTouch(i);
touchPosition1 = Camera.main.ScreenToWorldPoint(touch.position);
if (area.OverlapPoint(touchPosition1))
{
switch (touch.phase)
{
case TouchPhase.Began:
startPos = touchPosition1;
joyBack.transform.position = startPos;
joyFront.transform.position = startPos;
joyBack.SetActive(true);
joyFront.SetActive(true);
joyFront.transform.position = startPos;
joyBack.transform.position = startPos;
Debug.Log("begun");
break;
case TouchPhase.Moved:
direction = touchPosition1 - startPos;
Vector2 touchPosition2 = Vector2.ClampMagnitude(direction, 1.75f);
horizontal = touchPosition2.x;
vertical = touchPosition2.y;
//Debug.Log(horizontal);
distance = horizontal * horizontal + vertical * vertical;
distance = Mathf.Sqrt(distance);
direction = direction / distance;
direction = Limiters(direction, limit);
joyFront.transform.position =new Vector2(startPos.x + touchPosition2.x, startPos.y + touchPosition2.y);
Debug.DrawLine(startPos, startPos + touchPosition2);
Debug.Log(direction);
break;
case TouchPhase.Ended:
distance = 0;
Debug.Log("Ended");
joyBack.SetActive(false);
joyFront.SetActive(false);
break;
}
}
else
{
joyBack.SetActive(false);
joyFront.SetActive(false);
distance = 0;
}
}
}
}
private static Vector2 Limiters(Vector2 direction, float limit)
{
if (direction.x > limit)
{
direction.x = limit;
}
else if (direction.x < -limit)
{
direction.x = -limit;
}
if (direction.y > limit)
{
direction.y = limit;
}
else if (direction.y < -limit)
{
direction.y = -limit;
}
return direction;
}
private void FixedUpdate()
{
move.Translate((direction) * movespeed * distance * Time.fixedDeltaTime);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论