Guest User

charactercontroller

a guest
Jul 5th, 2016
41
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityStandardAssets.CrossPlatformInput;
  4.  
  5. public class CharacterController : MonoBehaviour {
  6.  
  7.     //PAC-16-8895266-2043887
  8.     public float speed;
  9.     public AudioClip hitSwoosh;
  10.    
  11.     private Rigidbody2D rb;
  12.     private AudioSource playerSounds;
  13.     private Animator anim;
  14.     private bool facingRight;
  15.     private string[] whichAttack = {"isAttacking", "isAttacking2", "isAttacking3"};
  16.    
  17.     protected bool isAttacking = false;
  18.     protected bool isMoving = false;
  19.     //protected bool comboOneDone = false;
  20.     //protected bool comboTwoDone = false;
  21.  
  22.     // Use this for initialization
  23.     void Start () {
  24.         rb = GetComponent<Rigidbody2D>();
  25.         anim = transform.GetChild (0).GetComponent<Animator>();
  26.         playerSounds = GetComponent<AudioSource>();
  27.        
  28.         facingRight = true;
  29.     }
  30.    
  31.     void Update(){
  32.         if(CrossPlatformInputManager.GetButtonDown ("Attack")){
  33.             isAttacking = true;
  34.         }
  35.     }
  36.    
  37.     // Update is called once per frame
  38.     void FixedUpdate () {
  39.         float horizontal = CrossPlatformInputManager.GetAxis ("Horizontal");
  40.        
  41.         if(isAttacking == false){
  42.             MoveControl (horizontal);
  43.         }
  44.         Flip (horizontal);
  45.         HandleAttack ();
  46.         Reset ();
  47.     }
  48.    
  49.     void MoveControl(float horizontal){
  50.         if(!anim.GetCurrentAnimatorStateInfo(0).IsTag("Attack")){
  51.             rb.velocity = new Vector2(horizontal * speed, 0);
  52.             anim.SetFloat ("speed", Mathf.Abs (horizontal));
  53.             print (horizontal);
  54.            
  55.         }
  56.     }
  57.    
  58.     void Flip(float horizontal){
  59.         if(horizontal > 0 && !facingRight || horizontal < 0 && facingRight){
  60.             facingRight = !facingRight;
  61.            
  62.             Vector3 theScale = transform.localScale;
  63.             theScale.x *= -1;
  64.             transform.localScale = theScale;
  65.         }
  66.     }
  67.    
  68.     void HandleAttack(){
  69.         if(isAttacking && !anim.GetCurrentAnimatorStateInfo(0).IsTag("Attack")){
  70.             rb.velocity = Vector2.zero;
  71.             anim.SetTrigger (whichAttack[Random.Range (0, 3)]);
  72.             /*playerSounds.clip = hitSwoosh;
  73.             playerSounds.Play();
  74.            
  75.             if(!comboOneDone){
  76.                 comboOneDone = true;
  77.                 anim.SetTrigger (whichAttack[0]);
  78.                 comboTwoDone = false;
  79.             }else if(!comboTwoDone && comboOneDone){
  80.                 comboTwoDone = true;
  81.                 anim.SetTrigger (whichAttack[1]);
  82.             }else if(comboOneDone && comboTwoDone){
  83.                 anim.SetTrigger (whichAttack[2]);
  84.                 comboOneDone = false;
  85.             }
  86.             Debug.Log (comboOneDone);
  87.             Debug.Log (comboTwoDone);*/
  88.         }
  89.        
  90.     }
  91.    
  92.     void Reset(){
  93.         isAttacking = false;
  94.     }
  95.    
  96. }
RAW Paste Data Copied