【问题标题】:How can I rotate my plane as per joystick rotation?如何根据操纵杆旋转来旋转我的飞机?
【发布时间】:2019-04-30 06:16:44
【问题描述】:

目前我正在开发一款射击游戏,它是一款内置 3d 的 2d 游戏。我想使用操纵杆来控制我的玩家平面旋转。我已经在画布上添加了我的操纵杆,并使用 PointersEventData 我正在处理我的操纵杆旋转。 这是它的代码:(ControllerBG 是操纵杆的外圈,控制器是操纵杆的内圈)

public virtual void OnDrag(PointerEventData eventData)
{
    Vector2 pos;
    if (RectTransformUtility.ScreenPointToLocalPointInRectangle(controllerBG.rectTransform, eventData.position, eventData.pressEventCamera, out pos))
    {
        pos.x = (pos.x / controllerBG.rectTransform.sizeDelta.x);
        pos.y = (pos.y / controllerBG.rectTransform.sizeDelta.y);

        inputVector = new Vector3(pos.x * 2, 0, pos.y * 2);
        inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;

        //move the joystick inner circle
        controller.rectTransform.anchoredPosition =
            new Vector3(inputVector.x * (controllerBG.rectTransform.sizeDelta.x / 2),
                        inputVector.z * (controllerBG.rectTransform.sizeDelta.y / 2));
    }
}

现在我想要的是当操纵杆旋转时,我想旋转我的玩家平面,但我不知道如何做到这一点,请帮助我。

到目前为止,我已经尝试过了,但它对我不起作用: 1.

Quaternion targetRotation = Quaternion.LookRotation(controller.rectTransform.anchoredPosition);
plane.transform.rotation = Quaternion.RotateTowards(
    plane.transform.rotation,
    targetRotation,
    PlaneController.instance.steeringPower * Time.deltaTime);
  1. 还创建了一个函数
private void Rotate(float dir)
{
    if (plane != null)
        plane.transform.Rotate(Vector3.up * PlaneController.instance.steeringPower * dir * Time.deltaTime * 0.8f);
}

我已经创建了这个 Rotate() 但我不知道如何在操纵杆运动中使用它,即如果控制器(操纵杆)顺时针移动 Rotate(1f) 否则如果逆时针移动 Rotate(-1f)。

请帮我解决我的问题。提前谢谢你。

【问题讨论】:

  • 看看这个如果可以帮助。 answers.unity.com/questions/1433049/…。互联网上有大量的链接和教程。你只需要谷歌它。
  • 嘿@SaadAnees,实际上我确实在网上寻找了很多教程,但我没有得到我想要的解决方案。你能帮我这个代码吗,我会非常感谢你
  • 当然。你能告诉我你想添加哪个操纵杆控件吗?是手机触控摇杆吗?
  • 是的,它的移动触摸操纵杆。​​span>

标签: c# unity3d canvas rotation joystick


【解决方案1】:

所以这是你的OnDrag 函数:

public virtual void OnDrag(PointerEventData eventData)
    {
        Vector2 pos = Vector2.zero;
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle
        (controllerBG.rectTransform,
        eventData.position,
        eventData.pressEventCamera,
        out pos))
        {
        pos.x = (pos.x / controllerBG.rectTransform.sizeDelta.x);
        pos.y = (pos.y / controllerBG.rectTransform.sizeDelta.y);

        float x = (controllerBG.rectTransform.pivot.x == 1) ? pos.x * 2 + 1 : pos.x * 2 - 1;
        float y = (controllerBG.rectTransform.pivot.x == 1) ? pos.y * 2 + 1 : pos.y * 2 - 1;
        inputVector = new Vector3(x, 0, y);
        inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
        controller.rectTransform.anchoredPosition =
            new Vector3(inputVector.x * (controllerBG.rectTransform.sizeDelta.x / 2),
                        inputVector.z * (controllerBG.rectTransform.sizeDelta.y / 2));
        }
    }

我假设您已经实现了 OnPointerDownOnPointerUp 来处理与操纵杆相关的其他事件。我还假设inputVector 是一个公共变量,可以在外部使用它,或者您可以在同一个脚本中使用它。这完全取决于你。

public Transform rot;
    void Update()
    {    
          Vector3 direction = new Vector3();
          direction = inputVector;
          rot.Rotate(direction);
    }

希望它可以帮助您解决问题。

【讨论】:

  • 嘿@SaadAnees,是的,我已经在 OnPointerDown() 和 OnPointerUp() 上实现了代码。我也了解 OnDrag() 您所做的任何更改。你能解释一下我不知道你在做什么的 Update() 部分吗?
  • 我已经编辑了答案。我忘了说public Transform rot;是你的飞机。
  • 嘿@saadAnees,我在同一个脚本文件中实现这个代码,因为我不想在不同的脚本中应用它,那么那个 variableJoystick 是什么?这旋转太奇怪了。
  • 嘿@SaadAnees,非常感谢你,我终于得到了我想要的结果。你真的很棒,非常感谢你帮助我。我只是将rot.Rotate(direction) 更改为Quaternion targetRotation = Quaternion.LookRotation(direction); plane.transform.rotation = Quaternion.RotateTowards(plane.transform.rotation, targetRotation, PlaneController.instance.steeringPower * Time.deltaTime);
  • @AishaD.S.我编辑了代码。您可以移除 variableJoystick。这是我的另一个操纵杆脚本名称。如果您使用相同的脚本,则不需要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多