Advertisement
Powerhoof

SpriteAnim Runner Example

Oct 12th, 2016
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using PowerTools;
  4.  
  5. /// Example of character with run/idle animations using the SpriteAnim component
  6. public class Runner : MonoBehaviour {
  7.  
  8.     // The speed of the character, set in the inspector
  9.     public float m_speed = 0;
  10.     // Idle and run animations, drag your animations into these fields in the inspector
  11.     public AnimationClip m_idle = null;
  12.     public AnimationClip m_run = null;
  13.  
  14.     SpriteAnim m_anim = null;
  15.  
  16.     // Use this for initialization
  17.     void Start () {
  18.  
  19.         // Store the sprite animation component so we don't have to get it again every frame
  20.         m_anim = GetComponent<SpriteAnim>();
  21.  
  22.         // Start playing the idle animation
  23.         m_anim.Play(m_idle);   
  24.     }
  25.    
  26.     // Update is called once per frame
  27.     void Update () {
  28.  
  29.         float direction = 0;
  30.         if ( Input.GetKey(KeyCode.LeftArrow) )
  31.             direction = -1;
  32.  
  33.         if ( Input.GetKey(KeyCode.RightArrow) )
  34.             direction = 1;
  35.  
  36.         if ( direction == 0 )
  37.         {
  38.             if ( m_anim.GetCurrentAnimation() != m_idle )
  39.                 m_anim.Play(m_idle);
  40.         }
  41.         else
  42.         {
  43.             if ( m_anim.GetCurrentAnimation() != m_run )
  44.                 m_anim.Play(m_run);
  45.  
  46.             transform.Translate(new Vector3(direction * m_speed * Time.deltaTime, 0, 0 ) );
  47.             if ( transform.localScale.x != direction )
  48.                 transform.localScale = new Vector3( direction, 1, 1 );         
  49.         }
  50.    
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement