如何在leftside上统一实例化玩家子弹

时间:2016-09-01 10:21:47

标签: c# unity3d instantiation

在我的团结2D游戏中,我用玩家方向实例化了子弹,但子弹子弹在玩家右侧实例化我试图在玩家左侧弹出实例化。这是我的玩家子弹

public Rigidbody2D playerWeapon;


void Start(){

    InvokeRepeating ("PlayerWeapon",1.0f,1.0f);
}

void PlayerWeapon()
{
        Rigidbody2D bPrefab = Instantiate (playerWeapon, new Vector3 (transform.position.x, transform.position.y, transform.position.z), Quaternion.identity) as Rigidbody2D;
}

1 个答案:

答案 0 :(得分:0)

使用transform.forward应该这样做。您还可以使用偏移来修改子弹应该从玩家的距离产生的距离。请尝试以下代码:

public Rigidbody2D playerWeapon;

void Start()
{
    InvokeRepeating("PlayerWeapon", 1.0f, 1.0f);
}

void PlayerWeapon()
{
    float offset = 5;
    Vector3 plyrPos = transform.position;
    Quaternion plyrRot = transform.rotation;
    Vector3 plyrDir = transform.forward;
    Vector3 spawnPos = plyrPos + plyrDir * offset;
    Rigidbody2D bPrefab = Instantiate(playerWeapon, spawnPos, plyrRot) as Rigidbody2D;
}

修改

您也可以这样做:

void PlayerWeapon()
{
    float shootSpeed = 10f;
    float offset = 5;
    Rigidbody2D bPrefab = Instantiate(playerWeapon, transform.position + (offset * transform.forward), transform.rotation) as Rigidbody2D;
    bPrefab.velocity = transform.forward * shootSpeed;
}

如果仍然向错误的方向移动,请将shootSpeed值翻转为-10,再试一次,然后将offset翻转为-5。这可能会解决您的问题。