[英]my bullet wont move after first shot why?
我的子彈射擊邏輯存在問題。
第一次一切正常,但是當我在重生后射擊子彈時它不動。 它只適用於第一次拍攝。
子彈是預制件。
這是我的代碼:
public Rigidbody2D bulletrb;
private float dirX=1;
public float speed=.001f;
public Transform playerPos;
private Vector3 bulletPos;
public GameObject bulletObj;
void Update () {
bulletPos.x = playerPos.transform.position.x + 2;
bulletPos.y = playerPos.transform.position.y + 1;
if (Input.GetKeyDown("1"))
{
bulletrb.velocity = new Vector2(dirX * speed, bulletrb.velocity.y);
Debug.Log("Shoot!");
}
}
private void OnTriggerEnter2D(Collider2D target)
{
if(target.gameObject.tag=="Zombie")
{
Destroy(bulletObj);
Debug.Log("Hited!");
Instantiate(bulletObj,bulletPos,Quaternion.identity,playerPos);
}
}
當您調用Destroy
,您正在丟失活動子彈GameObject
,但是當您調用Instantiate
,它實際上並未分配回您的bulletObj
引用。
快速簡單的修復方法是:
bulletObj = Instantiate(bulletObj, bulletPos, Quaternion.identity, playerPos);
可能會有更多改進,但這是問題的要點。
我要做的一個建議是使用bulletObj.SetActive(false)
和bulletObj.SetActive(true)
而不是Destroy
和Instantiate
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.