Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerController : MonoBehaviour
- {
- public AudioClip walkClip; // Walking sfx
- public AudioClip attackClip; // Attacking sfx
- public float yukariSpeed; // Yukari's speed
- private Animator anim; // ref to animator
- private HashIDs hash; // ref to hashIDs for animator states¶meters
- void Awake()
- {
- // references
- anim = GetComponent<Animator>();
- hash = GameObject.FindGameObjectWithTag("GameController").GetComponent<HashIDs>();
- }
- void FixedUpdate()
- {
- // movement input caching
- float h = Input.GetAxis("Horizontal");
- float v = Input.GetAxis("Vertical");
- MovementManagement(h, v);
- }
- void Update()
- {
- // attack input caching & audio ref
- bool attack = Input.GetButtonDown("Attack");
- anim.SetBool(hash.attackBool, attack);
- AudioManagement(attack);
- }
- void MovementManagement(float horizontal, float vertical)
- {
- // Check for axis inputs
- if (horizontal != 0f || vertical != 0f)
- {
- // to set the speed parameter to a positive value, and create a vector to move the player
- anim.SetFloat(hash.speedFloat, 0.45f); // This line is probably missing important things
- Vector2 movement = new Vector2(horizontal, vertical);
- rigidbody2D.AddForce(movement * yukariSpeed * Time.deltaTime);
- }
- else
- {
- // stop moving if no input
- anim.SetFloat(hash.speedFloat, 0f);
- }
- }
- // manage audio assets for player states
- void AudioManagement (bool attack) // create attack bool
- // if footsteps are not playing, play them when walk state is activated. the nested if prevents the audio file from overlapping.
- {
- if (anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.walkState)
- {
- if (!audio.isPlaying)
- {
- audio.Play();
- }
- }
- else
- {
- AudioSource.PlayClipAtPoint(walkClip, transform.position);
- }
- // if attack is true, play attackClip
- if (attack)
- {
- AudioSource.PlayClipAtPoint(attackClip, transform.position);
- }
- }
- }
- // Old Stuff Below Here
- // void FixedUpdate ()
- // {
- // float moveHorizontal = Input.GetAxis("Horizontal");
- // float moveVertical = Input.GetAxis("Vertical");
- // bool attack = Input.GetButton("Attack");
- // bool block = Input.GetButton("Block");
- //
- // Vector2 movement = new Vector2(moveHorizontal, moveVertical);
- //
- // rigidbody2D.AddForce(movement * speed * Time.deltaTime);
- // }
- //}
Advertisement
Add Comment
Please, Sign In to add comment