I am creating a 3D Game in unity where you are a Space ship shooting projectiles, I want to make it so when I pick up an powerUp to change the style of shooting, and for that I created another 2 bullets with different scripts for the bullet that should go oblique on the left side and the one that should go oblique right.
I created a prefab of those two and when I shoot they are not moving oblique but only on the x axis.

如果我只有一个子弹,它是工作的,但当我添加第二个子弹时,它只在x轴上移动,而不是在x和y轴上移动.

这是正在工作的子弹和右侧倾斜的子弹以及这两个不起作用的子弹的代码:

public class laser_Right : MonoBehaviour
{
    public int speed = 8;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        Movelaser();
    }

    void Movelaser()
    {
        Vector3 movement = (Vector3.up + Vector3.right).normalized * speed * Time.deltaTime;


        transform.Translate(movement);

        if (transform.position.y >= 9)
        {
            Destroy(gameObject);
        }
    }

}

因为左边的子弹是一样的,但是方向不同.

我很确定代码是正确的,但问题出在其他地方,可能是因为我用两颗子弹制作了一个预制件? 子弹设计了不同的 playbook ,所以我不明白为什么它不起作用.

推荐答案

您的脚本在Vector3.up + Vector3.right的绝对全局方向上移动投射物.目前还不清楚你是否已经将你的抛射物朝向它们预期的运动方向,但你可能已经做到了.然后,您可以使用transform.forward来获取相对于脚本应用到的对象的局部"向前"方向上的向量.

类似于:

Vector3 movement = transform.forward * speed * Time.deltaTime;

只需确保创建您的3个对象,以便每个对象都朝向您希望其移动的方向.

或者...

If you don't want to reorient the objects,你可以在它的运动中加上transform.right的a lateral分量.这似乎与您最初的做法一致.

Vector3 movement = (transform.forward + transform.right).normalized * speed * Time.deltaTime;

这将使其沿其局部向前45°的方向对角移动.

但最好是在创建对象时将其旋转到适当的方向,并使用我的第一个示例中的代码.

Csharp相关问答推荐

发布.NET框架项目将.NET核心元素注入到web. connect中

`Task`只有在C#中等待时才会运行吗?

Unity中的刚体2D运动

. net依赖注入如何避免服务类中的新

Mongo作为.NET中Testcontainers的副本集

UWP中的任务和界面

在调整大小的控件上绘制

为什么Regex.IsMatch(";\\t";,";\\t";)返回FALSE而不是TRUE?

异步实体框架核心查询引发InvalidOperation异常

在swagger示例中添加默认数组列表

什么类型的对象存储在大对象堆(LOH)中

BFF到具有AAD/Entra ID B2C的内部API(.NET/ASP.NET核心/标识.Web)

如何在GRPC代码First服务的返回类型上使用多态性?

MSI无法将快捷方式添加到启动文件夹

Visual Studio,Docker容器-容器调用:连接被拒绝

如何在C#中从MongoDB IPipelineStageDefinition中获取聚合命令的字段/选项?

工厂类是如何在.NET 8中注册的?

C#System.Commandline:如何向命令添加参数以便向其传递值?

如何在C# WinForm控件中使用Windows 10/11的黑暗主题?

如何根据分割文本的块数来计算文本的大小?