【问题标题】:How could i fix my Quaternion.LookRotation in 2D我怎样才能修复我的 Quaternion.LookRotation 二维
【发布时间】:2020-06-08 11:41:47
【问题描述】:

我试图让玩家在我试图制作的 2D 自上而下的射击游戏中看着我的鼠标。我对四元数还没有任何经验,但我设法解决了对象旋转的常见问题。我还有其他问题,玩家根本没有指向鼠标,它只是指向某个随机方向。

Public Transform playerPos;
Private Vector3 mousePos;

// Define and look at mouse

 void MouseL()
{
    mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1);
    Vector3 relativePos = mousePos - playerPos.position;

// rotate relativePos so the player will not rotate 90 deg in y axis

    Vector3 rotatedRelativePos = Quaternion.Euler(0, 0, 90) * relativePos;
    Quaternion rotation = Quaternion.LookRotation(Vector3.forward, rotatedRelativePos);
    playerPos.rotation = rotation;


}

// Update is called once per frame
void Update()
{

    MouseL();
}

即使 relativePos 确实发生了变化,但由于某种原因,玩家的旋转不会,无论我与玩家一起移动到哪里,似乎只有当 mousePos 确实发生变化时它才会旋转,但我将 relativePos 放入旋转中,所以我没有'不知道当 rotateRelativePos 发生变化时它是如何不旋转的。

编辑:如果 relativePos 发生变化,则旋转会发生变化,但非常轻微,例如整个屏幕上的 2 度。

编辑 2:设法用这段代码做到这一点:

Vector3 pz = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    pz.z = 0;
    var b = (pz.y - playerPos.position.y);
     var a = (pz.x - playerPos.position.x);
     var tang = a / b;
     var radianRotation = Mathf.Atan(tang);
     var degreeRotation = radianRotation * (180 / Mathf.PI);

    if (pz.y < playerPos.position.y)
    {
        playerPos.rotation = Quaternion.Euler(0, 0, -degreeRotation + 180);
    }

    else { playerPos.rotation = Quaternion.Euler(0, 0, -degreeRotation); }

【问题讨论】:

  • 为什么不直接使用反正切,因为它是二维的?然后你有一个旋转角度,就是这样
  • 谢谢,我做到了,但我有同样的问题,var a = Mathf.Abs(Input.mousePosition.y - playerPos.position.y); var b = Mathf.Abs(Input.mousePosition.x - playerPos.position.x); var tang = a / b; var radianRotation = Mathf.Atan(tang); var degreeRotation = radianRotation * (180 / Mathf.PI); playerPos.rotation = Quaternion.Euler(0, 0, degreeRotation); 让我知道这是否有问题

标签: c# unity3d rotation


【解决方案1】:
  1. 您确定您的角色在 XY 平面中移动,而不是在 XZ 中移动,这是 unity 3D 的默认设置吗?
  2. 您直接使用鼠标屏幕位置作为世界位置,只有当您的角色位于屏幕空间中的画布上时,这是可能的 - 覆盖模式(或非常不寻常的手动设置),在其他情况下您需要将屏幕转换为世界点。你上次更新的句子点在这个问题上,所以,我想,是这样的。
  3. 这对我有什么帮助:

Vector3 worldUp = Vector3.up; // 你可以在那里设置你的世界上轴

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane plane = new Plane(worldUp, playerPos.position);
float enter;
if (plane.Raycast(ray, out enter))
{
    // next two lines does the same, so select one at your own:
    //playerPos.LookAt(ray.GetPoint(enter), worldUp);
    playerPos.forward = ray.GetPoint(enter) - playerPos.position;
}

一般来说,它对向量的操作比四元数更方便,你只需要设置transform的局部前向,等于moustPointerPosition - playerPosition。

【讨论】:

  • 当我将 worldUp 定义为 Vector3.forward 或后退字符时,它会在 y 轴上旋转 90 度,使其成为一条非常细的线。当我将它定义为 Vector3.up 时,根本不旋转。尝试了我可以选择的两条线,结果相同
  • 如果你试试这个怎么办:Vector3 playerScreenPos = Camera.main.WorldToScreenPoint(playerPos.position); Vector3 screenDirection = Input.mousePosition - playerScreenPos; playerPos.forward = screenDirection;
  • 那个更奇怪。它在 x 和 y 轴上旋转以将其“上侧”指向鼠标
  • 好的。你能告诉我,哪个世界轴是你的“向上”,你想让你的角色围绕哪个轴旋转?
  • 那么,请再次尝试此代码 - 添加日志,并告诉我您在控制台中有什么输出:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多