HeroMobility.cs 1.68 KB
Newer Older
Zack Rembisz's avatar
Zack Rembisz committed
1 2 3 4
using UnityEngine;
using System.Collections;

public class HeroMobility : MonoBehaviour {
5 6 7 8 9
    public enum Footsteps {
        GRASS,
        WATER
    }

Zack Rembisz's avatar
Zack Rembisz committed
10 11 12
	public float playerSpeed = 1f;
	private Rigidbody2D playerRB;
	Animator anim;
13 14
    private Vector2 lastPos;
    public Vector2 deltaPos;
15 16 17 18 19 20 21 22 23
    private float lastFootstep;
    public float footStepDelay = 0.3f;
    public Footsteps footsetps = Footsteps.GRASS;
    private AudioSource audioSource;

    public AudioClip[] grassFootsteps;
    public AudioClip[] waterFootsteps;

    void Start () {
Zack Rembisz's avatar
Zack Rembisz committed
24 25
		playerRB = GetComponent<Rigidbody2D> ();
		anim = GetComponent<Animator> ();
26
        audioSource = GetComponent<AudioSource>();
27 28
        lastPos = playerRB.position;
    }
Zack Rembisz's avatar
Zack Rembisz committed
29

30 31
    // Update is called once per frame
    void FixedUpdate () {
Zack Rembisz's avatar
Zack Rembisz committed
32 33
		Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
		playerMove (input);
34 35 36 37 38
        
        if(deltaPos.magnitude > 0.001f && Time.time > lastFootstep) {
            lastFootstep += footStepDelay;
            Footstep();
        }
Zack Rembisz's avatar
Zack Rembisz committed
39 40 41 42
	}

	void playerMove (Vector2 input){
		//Controls hero movement in all 8 directions
43 44 45
		playerRB.MovePosition((Vector2)transform.position + input.normalized*playerSpeed);
        deltaPos = playerRB.position - lastPos;
        lastPos = playerRB.position;
Zack Rembisz's avatar
Zack Rembisz committed
46

47
    }
48 49 50 51 52 53 54 55 56 57 58

    void Footstep() {
        switch (footsetps) {
            case Footsteps.GRASS:
                audioSource.PlayOneShot(grassFootsteps[Random.Range(0, grassFootsteps.Length)]);
                break;
            case Footsteps.WATER:
                audioSource.PlayOneShot(waterFootsteps[Random.Range(0, waterFootsteps.Length)]);
                break;
        }
    }
Zack Rembisz's avatar
Zack Rembisz committed
59
}