首页 > 解决方案 > 玩家跳跃音频干扰玩家死亡音频

问题描述

所以我遇到了这个问题,当我跳跃并落在我的尖刺/敌人上时,我的玩家会播放我的死亡动画,但是当它快完成时它会中断,这是因为我告诉它在 isGuruded 为假时播放我的跳跃动画和我真的不确定如何解决它。我告诉我的游戏在它不在地面上时播放跳跃动画,这就是它干扰它的原因,但我希望我的游戏在跳跃时与尖峰碰撞时停止播放跳跃动画。当死亡动画为真时,我使我的跳跃动画为假,我也尝试在其他地方使跳跃动画为真。

我的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityStandardAssets.CrossPlatformInput;
 
public class PM : MonoBehaviour 
{
 
 
 public float moveSpeed;
 public float jumpHeight;
 Rigidbody2D rb;
 BoxCollider2D boxColliderPlayer;
 int layerMaskGround;
 float heightTestPlayer;
 public Animator animator;
 public Joystick joystick;
 float horizontalMove = 0f;
 
 
 
    void Start()
{
    rb = GetComponent<Rigidbody2D>();
 
    // Get the player's collider so we can calculate the height of the character.
    boxColliderPlayer = GetComponent<BoxCollider2D>();
    // We do the height test from the center of the player, so we should only check
    // halft the height of the player + some extra to ignore rounding off errors.
    heightTestPlayer = boxColliderPlayer.bounds.extents.y + 0.05f;
    // We are only interested to get colliders on the ground layer. If we would
    // like to jump ontop of enemies we should add their layer too (which then of
    // course can't be on the same layer as the player).
    layerMaskGround = LayerMask.GetMask("Ground");
 
    //audioSource = GetComponent<AudioSource>();
}
 
    void Update()
    {
 
        /// Player movement and playing the animtion
         if (joystick.Horizontal >= .2f)
     {
         horizontalMove = moveSpeed;
     }else if (joystick.Horizontal <= -.2f)
     {
         horizontalMove = -moveSpeed;
 
     }else
     {
         horizontalMove = 0f;
     }
 
     rb.velocity = new Vector2(horizontalMove, rb.velocity.y);
     animator.SetFloat("Speed",Mathf.Abs(horizontalMove));
 
 
     // Your jump code:
     if (CrossPlatformInputManager.GetButtonDown("Jump") && IsGrounded() )
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
            FindObjectOfType<AudioManager>().Play("PlayerJump");
 
        }
 
     if (IsGrounded() == true)
     {
        animator.SetBool("isJump", false);
 
     }
 
###### SAying if it is not colliding with the grounnd to play my jump animtion
     if (IsGrounded()== false) 
     {
        animator.SetBool("isJump", true);
 
      }
 
 
 
 
    }
    // End of update
 
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Coins"))
        {
            Destroy(other.gameObject);
 
        }
 
    }
 
#### The not touching the ground is interfering with this code.
    private void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.tag == "EnemyCollide")
        {
            animator.SetBool("Death", true);
            moveSpeed = 0.0f;
            jumpHeight = 0.0f;
            FindObjectOfType<AudioManager>().Play("PlayerDeath");
 
            //audioSource.PlayOneShot(impact, 0.7F)
        }
 
 
 
    }
 
 
    /// <summary>
    /// Simple check to see if our character is no the ground. 
    /// </summary>
    /// <returns>Returns <c>true</c> if the character is grounded.</returns>
    private bool IsGrounded()
    {
    // Note that we only check for colliders on the Ground layer (we don't want to hit ourself). 
    RaycastHit2D hit = Physics2D.Raycast(boxColliderPlayer.bounds.center, Vector2.down, heightTestPlayer, layerMaskGround);
    bool isGrounded = hit.collider != null;
    // It is soo easy to make misstakes so do a lot of Debug.DrawRay calls when working with colliders...
    Debug.DrawRay(boxColliderPlayer.bounds.center, Vector2.down * heightTestPlayer, isGrounded ? Color.green : Color.red, 0.5f);
    return isGrounded;
 }
} 

标签: c#unity3d

解决方案


推荐阅读