2015-04-05 651 views
0

我在Unity製作2D遊戲。如何讓子彈從玩家的方向發起

在這個我想添加一個有限的鏡頭數量的子彈。

子彈向玩家的方向發射,但總是從右側發起,即使玩家面對左側。我已將子彈數限制爲3.

如何在發生子彈之間放置延遲?

1腳本(子彈)

public class Bullet : MonoBehaviour { 

    private Player player; 
    public float speed = 1f; 
    public int abc = 2; 

    // Use this for initialization 
    void Start() { 
     player = GameObject.Find ("Player").GetComponent<Player>(); 
     if (player.aa.x == transform.localScale.x) 
      abc = 1; 
    } 

    // Update is called once per frame 
    public void Update() { 
     if (abc == 1) 
      rigidbody2D.velocity = new Vector3 (transform.localScale.x, 0, 1) * speed; 
     else 
      rigidbody2D.velocity = new Vector3 (transform.localScale.x, 0, 1) * speed; 
    } 
} 

第二腳本(播放器)

public class Player : MonoBehaviour { 

    public float speed = 10f; 
    public Vector2 maxVelocity = new Vector2(3, 5); 
    public bool standing; 
    public float jetSpeed = 15f; 
    public float airSpeedMultiplier = .3f; 
    public AudioClip leftFootSound; 
    public AudioClip rightFootSound; 
    public AudioClip thudSound; 
    public AudioClip rocketSound; 
    public Vector3 aa = new Vector3(1,1,1); 

    private Animator animator; 
    private PlayerController controller; 

    void Start(){ 
     controller = GetComponent<PlayerController>(); 
     animator = GetComponent<Animator>(); 
    } 

    void PlayLeftFootSound(){ 
     if (leftFootSound) 
      AudioSource.PlayClipAtPoint (leftFootSound, transform.position); 
    } 

    void PlayRightFootSound(){ 
     if (rightFootSound) 
      AudioSource.PlayClipAtPoint (rightFootSound, transform.position); 
    } 

    void PlayRocketSound(){ 
     if (!rocketSound || GameObject.Find ("RocketSound")) 
      return; 

     GameObject go = new GameObject ("RocketSound"); 
     AudioSource aSrc = go.AddComponent<AudioSource>(); 
     aSrc.clip = rocketSound; 
     aSrc.volume = 0.7f; 
     aSrc.Play(); 

     Destroy (go, rocketSound.length); 
    } 

    void OnCollisionEnter2D(Collision2D target){ 
     if (!standing) { 
      var absVelX = Mathf.Abs(rigidbody2D.velocity.x); 
      var absVelY = Mathf.Abs(rigidbody2D.velocity.y); 

      if(absVelX <= .1f || absVelY <= .1f){ 
       if(thudSound) 
        AudioSource.PlayClipAtPoint(thudSound, transform.position); 
      } 
     } 
    } 

    // Update is called once per frame 
    void Update() { 
     var forceX = 0f; 
     var forceY = 0f; 

     var absVelX = Mathf.Abs (rigidbody2D.velocity.x); 
     var absVelY = Mathf.Abs (rigidbody2D.velocity.y); 

     if (absVelY < .2f) 
      standing = true; 
     else 
      standing = false; 

     if (controller.moving.x != 0) { 
      if (absVelX < maxVelocity.x) { 

       forceX = standing ? speed * controller.moving.x : (speed * controller.moving.x * airSpeedMultiplier); 

       aa = transform.localScale = new Vector3 (forceX > 0 ? 1 : -1, 1, 1); 

      } 

      animator.SetInteger ("AnimState", 1); 

     } else { 
      animator.SetInteger ("AnimState", 0); 
     } 

     if (controller.moving.y > 0) { 
      PlayRocketSound(); 
         if (absVelY < maxVelocity.y) 
           forceY = jetSpeed * controller.moving.y; 

         animator.SetInteger ("AnimState", 2); 
       } else if (absVelY > 0) { 
      animator.SetInteger("AnimState", 3); 
       } 

     rigidbody2D.AddForce (new Vector2 (forceX, forceY)); 
    } 
} 

第三腳本(的PlayerController)

public class PlayerController : MonoBehaviour { 

    public Vector2 moving = new Vector2(); 
    public int Bulletlimit = 0; 
    public int MaxBulletlimit = 3; 
    public float bulletDelay = 3f; 
    public bool Gun; 

    public Bullet bullet; 
    // Use this for initialization 
    void Start() {} 

    // Update is called once per frame 
    void Update() { 

     moving.x = moving.y = 0; 

     if (Input.GetKey ("right")) { 
      moving.x = 1; 
     } else if (Input.GetKey ("left")) { 
      moving.x = -1; 
     } 

     if (Input.GetKey ("up")) { 
      moving.y = 1; 
     } else if (Input.GetKey ("down")) { 
      moving.y = -1; 
     } 

     if (Input.GetKey ("s")) { 
      if(Gun){ 
       if(Bulletlimit < MaxBulletlimit) 
       { 
        Bullet clone = Instantiate (bullet, transform.position, Quaternion.identity) as Bullet; 
        Bulletlimit = Bulletlimit + 1; 
       } 
      } 
     } 
    } 

    public void BulletCount() { 
     Bulletlimit = Bulletlimit - 1; 
    } 
} 

回答

0

1)還有就是要知道一個簡單的方法定位你的子彈和射擊方向。在角色下方添加一個兒童虛擬遊戲物體,作爲子彈的初始位置。把它放在你想要的地方。現在這個gameobject相對於你的角色移動和旋轉。當你實例化子彈時,使用它transform.positiontransform.rotation.forward

2)保持用戶在像private float lastShotTime;這樣的變量中觸發子彈的當前時間。當您發射子彈lastShotTime = Time.time時更新它的值。然後,當用戶想要拍攝另一顆子彈時,檢查自上次拍攝以來足夠的時間if (Time.time > lastShotTime + fireDelay) { Shoot(); }

+0

我得到了正確的子彈延遲的東西,但得到一個參數異常,get_time只能從主線程調用,並在位置之一,我無法找到玩家正面臨的當前面,並得到 – 2015-04-06 03:10:30

+0

得到通過將Time.time放置在函數中,但不知道爲什麼需要完成該例外。 – 2015-04-06 03:28:42

相關問題