// Update is called once per frame void Update() { // 如果按下了攻击键(默认是鼠标左键) if (InputEx.GetButtonDown("Fire1")) { // 播放动画,播放音效 anim.SetTrigger("Shoot"); GetComponent <AudioSource>().Play(); if (playerCtrl.facingRight) { // 角色向右时,向右边发射子弹 // 实例化一个子弹 Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0, 0, 0))) as Rigidbody2D; // 直接为刚体设置速度 bulletInstance.velocity = new Vector2(speed, 0); } else { // 角色向左时,向右边发射子弹 // 实例化一个子弹 Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0, 0, 180f))) as Rigidbody2D; // 直接为刚体设置速度 bulletInstance.velocity = new Vector2(-speed, 0); } } }
void Update() { // 如果按下炸弹的按钮,且没有已经安置的炸弹,且还有未被安置的炸弹 if (InputEx.GetButtonDown("Fire2") && !bombLaid && bombCount > 0) { // 减少炸弹数量 bombCount--; // 设置已经安放炸弹 bombLaid = true; // 播放音效 AudioSource.PlayClipAtPoint(bombsAway, transform.position); // 实例化一个炸弹到玩家所在位置 Instantiate(bomb, transform.position, transform.rotation); } // 如果炸弹数量大于0,则显示炸弹标志 if (bombCount > 0) { bombHUD.enabled = true; bombNum.enabled = true; bombNum.text = "x" + bombCount; } else { bombHUD.enabled = false; bombNum.enabled = false; } }
// Update is called once per frame void Update() { // 通过2D射线检测角色与辅助检测对象之间是否存在Ground层中的物体 grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground")); // Debug.DrawLine (transform.position, groundCheck.position, Color.red, 1f); // 如果玩家按下了跳跃键,并且检测到地面,则进入跳跃状态 if (InputEx.GetButtonDown("Jump") && grounded) { jump = true; } }