【问题标题】:Rotating Quaternions [closed]旋转四元数
【发布时间】:2021-04-14 01:52:34
【问题描述】:

这是一个向玩家发射激光的炮塔的代码,一个胶囊。激光目前以错误的旋转方式出来,它是直立的,而不是激光通常的样子。

到目前为止,我的代码运行良好,没有错误。我该如何解决这个问题?

void SpawnLaser(int iter)
{
    for (var j = 0; j < 5; j++)
    {
        Vector3 pos = transform.position;
        var rotation = transform.rotation;
        rotation *= Quaternion.Euler(0, 90, 0);
        Instantiate(projectile, pos, rotation);
    }
}

【问题讨论】:

  • 这是2D游戏吗?这些是精灵还是网格? transform 的哪个局部轴是炮塔的“前部”? projectile.transform 的哪个局部轴是激光的“前部”?如果它询问旋转激光器和询问碰撞,则该帖子目前过于宽泛。关于碰撞的部分应该是一个单独的问题。
  • 不要这样做。,只需使用 Rotate
  • 另外,projectile 是什么类型以及它的游戏对象附加了哪些组件(假设它有一个)?基本上,这篇文章需要minimal reproducible example。请参阅How to Ask 了解更多信息。
  • Uhhhh,对于关闭它的人来说,信息令人困惑。我应该编辑什么?
  • Projectile 只是一个游戏对象。

标签: c# unity3d quaternions


【解决方案1】:

我不完全确定您的代码的目标,但我可以提供一些指导,并且当我对您的问题有更多了解时可以编辑我的答案。

您的生成没有问题,问题很可能与您的移动有关。另一个问题是你的旋转被乘以一个局部变量,所以每个激光都指向同一个方向。

    // my laser object
    [SerializeField] private GameObject projectile = null;

    private void Update()
    {
        // when I hit space, spawn new lasers
        if (Input.GetKeyDown(KeyCode.Space))
            SpawnLaser();
    }

    void SpawnLaser()
    {
        // keep the rotation outside of the loop so the *= 90 will change the angle the object is positioned to look at
        var rotation = transform.rotation;

        // as the angle is *= 90, there are only 4 possible rotations, so I would adjust your loop or rotation product
        for (int j = 0; j < 4; j++)
        {
            Vector3 pos = transform.position;
            rotation *= Quaternion.Euler(0, 90, 0);

            // I attached a rigidbody to move the objects, if your objects or your player do not have rigidbodies, then they will not collide
            Rigidbody laserObj = Instantiate(projectile, pos, rotation).GetComponent<Rigidbody>();

            // I am adding a force of 10 in the forward direction of my newly spawned laser
            laserObj.AddForce(laserObj.transform.forward * 10f);
        }
    }

确保将刚体组件和碰撞器组件添加到正在生成的预制件中。您需要做的其他事情是在激光器上添加一层,这样它们就不会相互碰撞。查看Layer-based collisions。确保为您的激光添加一个图层并禁用与自身的碰撞,以免与其他激光发生碰撞。

至于你的碰撞问题,原因有很多,这里不再一一列举,而是链接到an answer that goes over the possible reason

编辑:在给出更多信息后,这可能更接近您想要实现的目标。

    // this script is on the object that will be firing the laser, a turret, gun, etc.

    [SerializeField] private GameObject projectile = null;      // laser prefab 
    [SerializeField] private Transform playerObjectPos = null;        // reference to what we are trying to aim at (player, enemy, etc.)

    private float laserFiringForce = 10f;

    private void Update()
    {
        // when I hit space, spawn new laser spawns
        if (Input.GetKeyDown(KeyCode.Space))
            SpawnLaser(playerObjectPos);
    }

    /// <summary>
    /// Spawns a laser aiming at a specified target
    /// </summary>
    /// <param name="target"></param>
    void SpawnLaser(in Transform target)
    {
        // lock our rotation to only Y-axis
        Vector3 targetPosition = new Vector3(target.position.x, transform.position.y, target.position.z);

        // have our turret / player / etc. look at the target position
        transform.LookAt(targetPosition);

        // instantiate a laser at the position of our turret / player and have its rotation aim in the same direction as our turret / player
        // while grabbing the reference to the rigidbody component on the laser so we can add a force to it (Note: You can have the movement logic on the laser itself instead of doing it here)
        Rigidbody laserObj = Instantiate(projectile, transform.position, Quaternion.LookRotation(transform.forward, Vector3.up)).GetComponent<Rigidbody>();

        // add a force in the direction the laser is facing so it moves
        laserObj.AddForce(laserObj.transform.forward * laserFiringForce);
    }

我会研究一个您可以学习的 FPS 或 3D 炮塔游戏教程。鉴于这些问题,我认为您对编程、Unity 或两者都比较陌生。如果这是您的第一个项目,请查看教程,创建它并将其更改为更多您的项目。完成一些教程后,您应该对 Unity 有更好的了解。如果您是一起编程的新手,我也会考虑研究 c# 和一般编程的类、文档等。

要开始使用 Unity,请尝试 Brackey's。我链接的教程是他的 3D 塔防游戏的第 4 集,他在那里制作了炮塔。

【讨论】:

  • 呃,它最终向四个方向发射激光。我想要的是一个根据其位置发射激光的炮塔。抱歉,如果有点混乱。目前,玩家不会与激光发生碰撞,也不会移动。
  • 你想要一个激光瞄准一个物体并向那个物体发射激光吗?即使目标移动,激光也会跟踪并击中目标?您之前的代码跨越了 4 个激光。我之所以以我认为的方式拆分它不是您想要做的,因为它对我来说没有太大意义。
  • 是的,但我不想让激光跟踪。我没有意识到它会在彼此之上产生 4 个激光!
  • 明白了。所以你想要一个可以瞄准的激光射击,然后在一个位置射击?
  • 是的,在播放器上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
  • 2020-04-11
  • 1970-01-01
  • 1970-01-01
  • 2013-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多