奇怪的行为Instance bullet预制件

时间:2017-04-20 08:03:49

标签: c# unity5

如果玩家没有移动并且只是站立预制子弹是正常运动(图片A)。 我的预制件在玩家跑步或移动时表现出不良行为(图B)。

Please Check the Picture

这是我的firePos脚本:

public class FirePos : MonoBehaviour {
    public bool _facingRight;
    public GameObject bulletRight, bulletLeft;
    bool canShootFire;
    // Use this for initialization
    void Start () {
        canShootFire = true;
        _facingRight = GameObject.Find ("Player").GetComponent<PlayerManager>();
    }

    // Update is called once per frame
    void Update () {
        if(Input.GetKeyDown(KeyCode.K) && canShootFire && GameObject.Find("Player").GetComponent<PlayerManager>().facingRight){
            canShootFire = false;
            Instantiate (bulletRight, transform.position, transform.rotation);
            StartCoroutine ("Firing");
        }

        if(Input.GetKeyDown(KeyCode.K) && canShootFire && !GameObject.Find("Player").GetComponent<PlayerManager>().facingRight){
            canShootFire = false;
            Instantiate (bulletLeft, transform.position, transform.rotation);
            StartCoroutine ("Firing");
        }

    }

    IEnumerator Firing(){
        yield return new WaitForSeconds (.5f);
        canShootFire = true;
    }

}

这是子弹运动

public class bulletRight : MonoBehaviour {
public float speed;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
    transform.Translate (Vector2.right*speed*Time.deltaTime,0);
    Destroy (gameObject,1.5f);
}
}

1 个答案:

答案 0 :(得分:0)

我认为这只是物理学。您假设子弹的初始速度为零,但事实并非如此。这是射手目前的速度。尝试在项目符号对象中设置初始速度并将其添加到speed