ren811

ControlesFox.cs

Dec 13th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.78 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ControlesFox : MonoBehaviour {
  5.    
  6.     #region Properties
  7.    
  8.     private float runSpeed = 0.3f;
  9.     private int contador = 0;
  10.     private bool isControllable = true;
  11.     private bool isJumping = false;
  12.    
  13.     public GUIText texto;
  14.    
  15.     public enum States{
  16.         TURNING_LEFT,
  17.         TURNING_RIGHT,
  18.         NOT_TURNING
  19.     }
  20.    
  21.     public States currentState = States.NOT_TURNING;
  22.    
  23.     private Vector3 CamPos = new Vector3();
  24.    
  25.     #endregion
  26.    
  27.     #region Functions
  28.    
  29.     void Start ()
  30.     {
  31.         animation["CorridaRaposa"].speed = 3f;
  32.         animation["CorridaRaposa"].wrapMode = WrapMode.Loop;
  33.         animation["CorridaRaposa"].blendMode = AnimationBlendMode.Blend;
  34.         animation["PuloRaposa"].speed = 2.5f;
  35.         animation["PuloRaposa"].blendMode = AnimationBlendMode.Blend;
  36.         animation.Play("CorridaRaposa");
  37.        
  38.         texto.text = "Tigelas coletadas: 0";
  39.     }
  40.  
  41.     void Update ()
  42.     {
  43.         GetPlayerInput ();
  44.        
  45.         if(currentState == States.TURNING_LEFT)
  46.         {
  47.             gameObject.transform.Rotate (0f,-2.5f,0f);
  48.         }
  49.         if (currentState == States.TURNING_RIGHT)
  50.         {
  51.             gameObject.transform.Rotate (0f, 2.5f,0f);
  52.         }
  53.         if (currentState == States.NOT_TURNING)
  54.         {
  55.             if (gameObject.transform.rotation.y < 0)
  56.             {
  57.                 gameObject.transform.Rotate (0f, 1.5f, 0f);
  58.             }
  59.             if (gameObject.transform.rotation.y > 0)
  60.             {
  61.                 gameObject.transform.Rotate (0f, -1.5f, 0f);
  62.             }
  63.         }
  64.        
  65.         MoveFox ();
  66.         MoveCamera ();
  67.        
  68.         if (animation["PuloRaposa"].time >= 2.4)
  69.         {
  70.             isJumping = false;
  71.             animation.Play ("CorridaRaposa");
  72.         }
  73.  
  74.         texto.text = "Tigelas coletadas: " + contador.ToString();
  75.     }
  76.    
  77.     void MoveCamera()
  78.     {
  79.         CamPos = gameObject.transform.position;
  80.         Camera.main.transform.position = new Vector3(CamPos.x, CamPos.y + 1.9f, CamPos.z - 2.4f);
  81.     }
  82.    
  83.     void MoveFox()
  84.     {
  85.         this.transform.Translate(0f,0f,runSpeed);
  86.         if (this.transform.position.x > 48)
  87.         {
  88.             this.transform.Translate(-0.3f,0,0);   
  89.         }
  90.         if (this.transform.position.x < 40)
  91.         {
  92.             this.transform.Translate(0.3f,0,0);
  93.         }
  94.     }
  95.    
  96.     void GetPlayerInput()
  97.     {
  98.         if (!isControllable)
  99.         {
  100.             Input.ResetInputAxes();
  101.         }
  102.         if (Input.GetKeyDown(KeyCode.LeftArrow))
  103.         {  
  104.             currentState  = States.TURNING_LEFT;
  105.         }
  106.         if (Input.GetKeyDown(KeyCode.RightArrow))
  107.         {
  108.             currentState = States.TURNING_RIGHT;
  109.         }
  110.        
  111.         if(Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow))
  112.         {
  113.             currentState = States.NOT_TURNING;
  114.         }
  115.        
  116.         if (Input.GetKeyDown (KeyCode.Space))
  117.         {
  118.             if (!isJumping)
  119.             {
  120.                 isJumping = true;
  121.                 animation.Play("PuloRaposa");
  122.                 Input.ResetInputAxes();
  123.             }
  124.             else
  125.                 Input.ResetInputAxes();
  126.         }
  127.     }
  128.    
  129.     void OnCollisionEnter(Collision colisao)
  130.     {
  131.         if (colisao.gameObject.tag == "Tigela")
  132.         {
  133.             contador += 1;
  134.             Debug.Log("Colidiu com tigela!");
  135.         }
  136.     }
  137.    
  138.     #endregion
  139. }
Advertisement
Add Comment
Please, Sign In to add comment