added footsteps!
need to move the call from OnMove to Run, still some bugfixing to do
This commit is contained in:
		@@ -29,12 +29,22 @@ public class PlayerBehavior : MonoBehaviour
 | 
			
		||||
 | 
			
		||||
    Animator animator;
 | 
			
		||||
    [HideInInspector] public bool playerIsAlive = true;
 | 
			
		||||
 | 
			
		||||
    [Header("Sound")]
 | 
			
		||||
    [SerializeField] public AudioClip footstepSound;
 | 
			
		||||
    [SerializeField] public AudioClip deathSound;
 | 
			
		||||
    AudioSource audioSource;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    void Start()
 | 
			
		||||
    {
 | 
			
		||||
    {  // initialize
 | 
			
		||||
        _rb = GetComponent<Rigidbody2D>();
 | 
			
		||||
        stateController = GameObject.Find("StateController").GetComponent<StateController>();
 | 
			
		||||
 | 
			
		||||
        audioSource = this.gameObject.GetComponent<AudioSource>();
 | 
			
		||||
        audioSource.clip = footstepSound;
 | 
			
		||||
        audioSource.loop = true;
 | 
			
		||||
 | 
			
		||||
        animator = GetComponent<Animator>();
 | 
			
		||||
        GameObject.Find("Main Camera").GetComponent<CameraMovement>().player = this.gameObject;
 | 
			
		||||
        playerIsAlive = true;
 | 
			
		||||
@@ -204,13 +214,14 @@ public class PlayerBehavior : MonoBehaviour
 | 
			
		||||
        if (playerIsAlive) {
 | 
			
		||||
            print("destroyPlayer called");
 | 
			
		||||
            playerIsAlive = false;
 | 
			
		||||
            AudioSource audio = this.gameObject.GetComponent<AudioSource>();
 | 
			
		||||
            audio.Play();
 | 
			
		||||
            audioSource.clip = deathSound;
 | 
			
		||||
            audioSource.loop = false;
 | 
			
		||||
            audioSource.Play();
 | 
			
		||||
 | 
			
		||||
            // animate
 | 
			
		||||
            animator.Play("Die");
 | 
			
		||||
            // yield return new WaitForSeconds(animator.GetCurrentAnimatorStateInfo(0).length);
 | 
			
		||||
            yield return new WaitForSeconds(audio.clip.length);
 | 
			
		||||
            yield return new WaitForSeconds(audioSource.clip.length);
 | 
			
		||||
 | 
			
		||||
            // this.stateController.SetDeathCanvasActive(true);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -63,6 +63,9 @@ public class PlayerMovement : MonoBehaviour
 | 
			
		||||
 | 
			
		||||
    [HideInInspector] private PlayerBehavior playerBehavior;
 | 
			
		||||
    [HideInInspector] private StateController stateController;
 | 
			
		||||
 | 
			
		||||
    [HideInInspector] private AudioSource audioSource;
 | 
			
		||||
    [HideInInspector] private bool soundPlaying = false;
 | 
			
		||||
    #endregion
 | 
			
		||||
 | 
			
		||||
    private void Awake()
 | 
			
		||||
@@ -71,6 +74,7 @@ public class PlayerMovement : MonoBehaviour
 | 
			
		||||
        playerBehavior = this.gameObject.GetComponent<PlayerBehavior>();
 | 
			
		||||
        grapplingRope = playerBehavior.grapplingRope;
 | 
			
		||||
        stateController = GameObject.FindGameObjectWithTag("StateController").GetComponent<StateController>();
 | 
			
		||||
        audioSource = GetComponent<AudioSource>();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void Start()
 | 
			
		||||
@@ -86,6 +90,18 @@ public class PlayerMovement : MonoBehaviour
 | 
			
		||||
        } else {
 | 
			
		||||
            this._moveInput = Vector2.zero;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (!IsJumping && !_isJumpFalling && !isRegFalling && value.Get<Vector2>().x != 0) {
 | 
			
		||||
            if (!soundPlaying) {
 | 
			
		||||
                print("footsteps PLAY");
 | 
			
		||||
                audioSource.Play();
 | 
			
		||||
                soundPlaying = true;
 | 
			
		||||
            }
 | 
			
		||||
        } else if (audioSource.isPlaying && audioSource.clip.name == "footsteps") {
 | 
			
		||||
            print("footsteps stop");
 | 
			
		||||
            audioSource.Stop();
 | 
			
		||||
            soundPlaying = false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void OnJump()
 | 
			
		||||
@@ -364,6 +380,15 @@ public class PlayerMovement : MonoBehaviour
 | 
			
		||||
        //Convert this to a vector and apply to rigidbody
 | 
			
		||||
        RB.AddForce(movement * Vector2.right, ForceMode2D.Force);
 | 
			
		||||
 | 
			
		||||
        // play sound
 | 
			
		||||
        // if (!IsJumping && movement != 0) {
 | 
			
		||||
        //     if (!audioSource.isPlaying) {
 | 
			
		||||
        //         audioSource.Play();
 | 
			
		||||
        //     }
 | 
			
		||||
        // } else if (audioSource.isPlaying && audioSource.clip.name == "footsteps") {
 | 
			
		||||
        //     audioSource.Stop();
 | 
			
		||||
        // }
 | 
			
		||||
 | 
			
		||||
        /*
 | 
			
		||||
		 * For those interested here is what AddForce() will do
 | 
			
		||||
		 * RB.velocity = new Vector2(RB.velocity.x + (Time.fixedDeltaTime  * speedDif * accelRate) / RB.mass, RB.velocity.y);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user