C# 旋转四元数

C# 旋转四元数,c#,unity3d,quaternions,C#,Unity3d,Quaternions,这是炮塔向玩家发射激光的代码,一个太空舱。目前激光器的旋转方向是错误的,它是垂直的,而不是激光器通常的样子 到目前为止,我的代码运行良好,没有bug。我该如何解决这个问题 void SpawnLaser(int iter) { for (var j = 0; j < 5; j++) { Vector3 pos = transform.position; var rotation = transform.rotation; ro

这是炮塔向玩家发射激光的代码,一个太空舱。目前激光器的旋转方向是错误的,它是垂直的,而不是激光器通常的样子

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

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);
    }
}
激光(国际热核实验堆) { 对于(var j=0;j<5;j++) { 向量3位置=变换位置; var rotation=transform.rotation; 旋转*=四元数欧拉(0,90,0); 实例化(投射物、位置、旋转); } }
我不完全确定您的代码的目标,但我可以提供一些指导,当我了解更多有关您的问题时,可以编辑我的答案

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

    // 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);
        }
    }
//我的激光对象
[SerializeField]私有游戏对象投射物=null;
私有void更新()
{
//当我进入太空,产生新的激光
if(Input.GetKeyDown(KeyCode.Space))
激光();
}
真空激光器()
{
//将旋转保持在循环之外,以便*=90将更改对象定位要查看的角度
var rotation=transform.rotation;
//因为角度是*=90,所以只有4个可能的旋转,所以我会调整你的循环或旋转积
对于(int j=0;j<4;j++)
{
向量3位置=变换位置;
旋转*=四元数欧拉(0,90,0);
//我附加了一个刚体来移动对象,如果您的对象或播放器没有刚体,那么它们不会碰撞
刚体laserObj=实例化(投射物、位置、旋转).GetComponent();
//我在新产生的激光的前进方向上增加了一个10的力
laserObj.AddForce(laserObj.transform.forward*10f);
}
}
确保将刚体组件和碰撞器组件添加到正在生成的预设中。你需要做的另一件事是在激光器上加一层,这样它们就不会相互碰撞。调查确保为您的激光器提供一个层,并禁用与自身的碰撞,使其不会与其他激光器碰撞

至于您关于碰撞的问题,有很多原因,因此我将链接到

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

    // 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);
    }
//此脚本位于将发射激光、炮塔、火炮等的对象上。
[SerializeField]私有游戏对象投射物=null;//激光预制
[SerializeField]专用转换PlayerObject=null;//参考我们试图瞄准的目标(玩家、敌人等)
专用浮子激光点火力=10f;
私有void更新()
{
//当我进入太空时,产生新的激光
if(Input.GetKeyDown(KeyCode.Space))
激光(playerObjectPos);
}
/// 
///产生瞄准指定目标的激光
/// 
/// 
激光(在变换目标中)
{
//仅将旋转锁定到Y轴
Vector3 targetPosition=新的Vector3(target.position.x,transform.position.y,target.position.z);
//让我们的炮塔/玩家/等查看目标位置
变换注视(targetPosition);
//在我们的炮塔/玩家的位置实例化一台激光器,并使其旋转目标与我们的炮塔/玩家的方向相同
//在抓取激光器上刚体组件的参考时,我们可以向其添加力(注意:您可以在激光器本身上拥有运动逻辑,而不是在此处进行)
刚体laserObj=实例化(射弹、transform.position、四元数、LookRotation(transform.forward、Vector3.up)).GetComponent();
//在激光器面对的方向上增加一个力,使其移动
laserObj.AddForce(laserObj.transform.forward*laserFiringForce);
}
我会看看一个教程,你可以遵循的FPS或3D炮塔游戏。考虑到这些问题,我认为你要么是编程新手,要么是Unity新手,要么两者兼而有之。如果这是您的第一个项目之一,请查看教程,创建它并将其更改为更适合您的。完成一些教程后,您应该对Unity有了更好的理解。如果你对所有的编程都是新手,我也会考虑查看C语言和编程的类、文档等。


为了让你在团结中开始,尝试一下。我链接的教程是他的3D塔防游戏的第4集,他在那里制作炮塔。

uhhh,它最终向四个方向发射激光。我想要的是一个根据其位置发射激光的炮塔。抱歉,如果有点混乱的话。目前,玩家不会与激光碰撞,也不会移动。你想要一个激光瞄准一个物体并向该物体发射激光吗?激光跟踪并击中目标,即使目标移动?你之前的代码是将4个激光相互叠加。我按我认为的方式分割它的原因不是你想做的,因为这对我来说没有太多意义。是的,但我不想让激光跟踪。我没意识到它在相互叠加的基础上产生了4个激光!明白了。那么你想要一个激光射击的目标,然后在一个位置射击?是的,在玩家身上。这是2d游戏吗?这些是精灵还是网格?变换的哪个局部轴是炮塔的“前部”?投射物的哪个局部轴。变换是激光器的“前端”?如果这篇文章问的是旋转激光器和碰撞,那么它目前就太宽泛了。关于碰撞的部分应该是一个单独的问题。不要这样做。只需使用RotateAlso,什么类型的
投射物
,以及它的gameob连接了哪些组件