lyrical

Untitled

Dec 31st, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerController : MonoBehaviour
  5. {
  6.  
  7.     public AudioClip walkClip; // Walking sfx
  8.     public AudioClip attackClip; // Attacking sfx
  9.     public float yukariSpeed; // Yukari's speed
  10.  
  11.     private Animator anim; // ref to animator
  12.     private HashIDs hash; // ref to hashIDs for animator states&parameters
  13.  
  14.     void Awake()
  15.     {
  16.         // references
  17.         anim = GetComponent<Animator>();
  18.         hash = GameObject.FindGameObjectWithTag("GameController").GetComponent<HashIDs>();
  19.     }
  20.  
  21.     void FixedUpdate()
  22.     {
  23.         // movement input caching
  24.         float h = Input.GetAxis("Horizontal");
  25.         float v = Input.GetAxis("Vertical");
  26.  
  27.         MovementManagement(h, v);
  28.     }
  29.  
  30.     void Update()
  31.     {
  32.         // attack input caching & audio ref
  33.         bool attack = Input.GetButtonDown("Attack");
  34.  
  35.         anim.SetBool(hash.attackBool, attack);
  36.         AudioManagement(attack);
  37.  
  38.     }
  39.  
  40.     void MovementManagement(float horizontal, float vertical)
  41.     {
  42.  
  43.         // Check for axis inputs
  44.         if (horizontal != 0f || vertical != 0f)
  45.         {
  46.             // to set the speed parameter to a positive value, and create a vector to move the player
  47.             anim.SetFloat(hash.speedFloat, 0.45f); // This line is probably missing important things
  48.             Vector2 movement = new Vector2(horizontal, vertical);
  49.             rigidbody2D.AddForce(movement * yukariSpeed * Time.deltaTime);
  50.         }
  51.         else
  52.         {
  53.             // stop moving if no input
  54.             anim.SetFloat(hash.speedFloat, 0f);
  55.         }
  56.     }
  57.  
  58.     // manage audio assets for player states
  59.     void AudioManagement (bool attack) // create attack bool
  60.  
  61.         // if footsteps are not playing, play them when walk state is activated. the nested if prevents the audio file from overlapping.
  62.     {
  63.         if (anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.walkState)
  64.         {
  65.             if (!audio.isPlaying)
  66.             {
  67.                 audio.Play();
  68.             }
  69.         }
  70.         else
  71.         {
  72.             AudioSource.PlayClipAtPoint(walkClip, transform.position);
  73.         }
  74.  
  75.         // if attack is true, play attackClip
  76.         if (attack)
  77.         {
  78.             AudioSource.PlayClipAtPoint(attackClip, transform.position);
  79.         }
  80.     }
  81. }
  82.  
  83.  
  84. // Old Stuff Below Here
  85. //    void FixedUpdate ()
  86. //    {
  87. //        float moveHorizontal = Input.GetAxis("Horizontal");
  88. //        float moveVertical = Input.GetAxis("Vertical");
  89. //        bool attack = Input.GetButton("Attack");
  90. //        bool block = Input.GetButton("Block");
  91. //
  92. //        Vector2 movement = new Vector2(moveHorizontal, moveVertical);
  93. //
  94. //       rigidbody2D.AddForce(movement * speed * Time.deltaTime);
  95. //    }
  96. //}
Advertisement
Add Comment
Please, Sign In to add comment