Burnner

Untitled

Mar 30th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.42 KB | None | 0 0
  1. using System.Collections;
  2.  
  3. public class PlayerMovement : MonoBehaviour
  4. {
  5.  
  6.     public static PlayerMovement instance;
  7.     public Camera playerCamera;
  8.     public Animator myAnimator;
  9.     public GameObject particleFXDead;
  10.     public GameObject particleFX;
  11.     public GameObject body;
  12.     public Transform[] rays = new Transform[3];
  13.  
  14.     public float speed = 12.0F;
  15.     public float jumpSpeed = 8.0F;
  16.     public float gravity = 20.0F;
  17.  
  18.     private Vector3 moveDirection = Vector3.zero;
  19.     public bool tapControll = true;
  20.     private bool run = false;
  21.  
  22.     private Rigidbody rBody;
  23.     private BoxCollider collider;
  24.  
  25.  
  26.  
  27.     void Start()
  28.     {
  29.         instance = this;
  30.  
  31.         if (playerCamera == null)
  32.         {
  33.             playerCamera = Camera.main;
  34.         }
  35.  
  36.         playerCamera.transparencySortMode = TransparencySortMode.Orthographic;
  37.         myAnimator.SetTrigger("Start");
  38.         playerCamera.transform.position = new Vector3(transform.position.x + 5, playerCamera.transform.position.y, playerCamera.transform.position.z);
  39.         rBody = GetComponent<Rigidbody>();
  40.         collider = GetComponent<BoxCollider>();
  41.         rBody.freezeRotation = true;
  42.         rBody.useGravity = false;
  43.        
  44.     }
  45.  
  46.  
  47.  
  48.     bool IsGrounded()
  49.     {
  50.         if (Physics.gravity.y < 0)
  51.             return Physics.Raycast(transform.position, -Vector3.up, collider.bounds.extents.y + 0.1f);
  52.         else return
  53.             Physics.Raycast(transform.position, Vector3.up, collider.bounds.extents.y + 0.1f);
  54.  
  55.     }
  56.  
  57.  
  58.     void FixedUpdate()
  59.     {
  60.  
  61.         if (!run)
  62.             return;
  63.  
  64.         moveDirection = new Vector3(1, 0, 0);
  65.         moveDirection = transform.TransformDirection(moveDirection);
  66.         moveDirection *= speed;
  67.  
  68.         rBody.MovePosition(transform.position + moveDirection  * Time.deltaTime);
  69.  
  70.         if (Input.GetButton("Jump") && IsGrounded())
  71.         {
  72.             rBody.velocity = new Vector3(0, jumpSpeed, 0);
  73.  
  74.             if (0 < gravity)
  75.                 myAnimator.SetTrigger("JumpFloor");
  76.             else
  77.                 myAnimator.SetTrigger("JumpCeiling");
  78.  
  79.         }
  80.         rBody.AddForce(new Vector3(0, -gravity * rBody.mass, 0));
  81.    
  82.         playerCamera.transform.position = new Vector3(transform.position.x + 5, playerCamera.transform.position.y, playerCamera.transform.position.z);
  83.  
  84.         for (int i = 0; i < 3; i++)
  85.         {
  86.             if (Physics.Raycast(rays[i].position, Vector3.right, collider.bounds.extents.y + 0.1f, ~( 1 << 11))) Collided();
  87.  
  88.            
  89.         }
  90.  
  91.  
  92.     }
  93.  
  94.    
  95.  
  96.     public void Collided()
  97.     {
  98.  
  99.             body.SetActive(false);
  100.             particleFX.SetActive(false);
  101.             particleFXDead.SetActive(true);
  102.             run = false;
  103.             rBody.isKinematic = true;
  104.             SoundManager.instance.StopLevelSound();
  105.             return;
  106.        
  107.     }
  108.    
  109.     void OnTriggerEnter(Collider other)
  110.     {
  111.  
  112.  
  113.      
  114.         if (other.gameObject.tag == "GravityUP")
  115.         {
  116.             gravity *= -1;
  117.             Physics.gravity = new Vector3 (0,50f,0);
  118.             jumpSpeed *= -1;
  119.             return;
  120.          
  121.         }
  122.  
  123.         if (other.gameObject.tag == "GravityDOWN")
  124.         {
  125.             gravity *= -1;
  126.             Physics.gravity = new Vector3(0, -50f, 0);
  127.             jumpSpeed *= -1;
  128.             return;
  129.  
  130.         }
  131.  
  132.         if (other.gameObject.tag == "Controll")
  133.         {
  134.             if (tapControll)
  135.                 tapControll = false;
  136.             else
  137.                 tapControll = true;
  138.  
  139.             return;
  140.         }
  141.  
  142.         //End Animation
  143.         if (other.gameObject.tag == "LevelEnd")
  144.         {
  145.            
  146.             if (!run)
  147.                 return;
  148.             rBody.useGravity = false;
  149.  
  150.             run = false;
  151.             LeanTween.move(gameObject, VortexMiddle.instance.gameObject.transform.position, 1.5f).setEase(LeanTweenType.easeInQuad);
  152.             myAnimator.SetTrigger("LevelEnd");
  153.             return;
  154.  
  155.         }
  156.  
  157.         if (other.gameObject.tag == "EndPoint")
  158.         {
  159.             particleFX.GetComponent<ParticleSystem>().Stop();
  160.             myAnimator.SetTrigger("End");
  161.             LevelStatsManager.instance.stats.success++;
  162.             LevelStatsManager.instance.stats.beaten = true;
  163.         }
  164.  
  165.     }
  166.  
  167.  
  168.     public void PlayerRun()
  169.     {
  170.        
  171.         run = true;
  172.         if (0 < Physics.gravity.y) Physics.gravity *= -1;
  173.    
  174.     }
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment