C# 我应该用什么来代替dirX,让我的子弹左右射击

C# 我应该用什么来代替dirX,让我的子弹左右射击,c#,unity3d,C#,Unity3d,我正在做一个2d游戏,我想知道如何让子弹左右射击。在这一刻,即使我的玩家向右移动,子弹也只会向左移动。我怎样才能让他们两面射击,或者在他们发现标记为“敌人”的物体时射击 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 使用UnityStandardAssets.CrossPlatformInput; 公共阶级特征:单一行为 { 刚体2d rb; 浮动dirX; [序列化字段] 浮动速度=5f,跳跃力=400f,子弹速度=500f; 矢量3

我正在做一个2d游戏,我想知道如何让子弹左右射击。在这一刻,即使我的玩家向右移动,子弹也只会向左移动。我怎样才能让他们两面射击,或者在他们发现标记为“敌人”的物体时射击

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityStandardAssets.CrossPlatformInput;
公共阶级特征:单一行为
{
刚体2d rb;
浮动dirX;
[序列化字段]
浮动速度=5f,跳跃力=400f,子弹速度=500f;
矢量3局部尺度;
公共交通;
公共刚体2D子弹;
//用于初始化
void Start()
{
localScale=transform.localScale;
rb=GetComponent();
}
//每帧调用一次更新
无效更新()
{
dirX=CrossPlatformInputManager.GetAxis(“水平”);
if(CrossPlatformInputManager.GetButtonDown(“跳转”))
跳跃();
if(CrossPlatformInputManager.GetButtonDown(“Fire1”))
火();
}
void FixedUpdate()
{
rb.velocity=新矢量2(dirX*moveSpeed,rb.velocity.y);
}
无效跳转()
{
if(rb.velocity.y==0)
rb.附加力(矢量2.up*跳跃力);
}
虚火
{
var firedBullet=实例化(bullet、barrel.position、barrel.rotation);
firedBullet.AddForce(枪管上升速度*子弹速度);
}
}

您已经考虑了桶的方向。移动时只需更改桶的方向

一种方法是直接设置:

    // Update is called once per frame
    void Update()
    {
        dirX = CrossPlatformInputManager.GetAxis("Horizontal");
        if (dirX !=0)
        {
            barrel.up = Vector3.right * Mathf.Sign(dirX);
        }

        if (CrossPlatformInputManager.GetButtonDown("Jump"))
            Jump();

        if (CrossPlatformInputManager.GetButtonDown("Fire1"))
            Fire();
    }
如果木桶是玩家对象的子对象,则更改角色的旋转以使木桶的方向指向正确的方向也会起作用。问题中没有足够的信息可以确定,但使用设置角色的旋转可能会起作用:

    // Update is called once per frame
    void Update()
    {
        dirX = CrossPlatformInputManager.GetAxis("Horizontal");
        if (dirX !=0)
        {
            Vector3 newPlayerForward = Vector3.forward * Mathf.Sign(dirX);

            transform.rotation = Quaternion.LookRotation(newPlayerForward, Vector3.up);
        }

        if (CrossPlatformInputManager.GetButtonDown("Jump"))
            Jump();

        if (CrossPlatformInputManager.GetButtonDown("Fire1"))
            Fire();
    }

firedBullet.AddForce
中传递的参数决定了方向。不清楚枪管是什么,但试着把你的
dirX
float考虑进去,例如,枪管在枪的末端,应该在那里射击
    // Update is called once per frame
    void Update()
    {
        dirX = CrossPlatformInputManager.GetAxis("Horizontal");
        if (dirX !=0)
        {
            Vector3 newPlayerForward = Vector3.forward * Mathf.Sign(dirX);

            transform.rotation = Quaternion.LookRotation(newPlayerForward, Vector3.up);
        }

        if (CrossPlatformInputManager.GetButtonDown("Jump"))
            Jump();

        if (CrossPlatformInputManager.GetButtonDown("Fire1"))
            Fire();
    }