Advertisement
Guest User

Untitled

a guest
May 29th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.47 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Movement : MonoBehaviour
  5. {
  6.     public float rotationPeriod = 0.3f;    
  7.     public float sideLength = 1f;
  8.  
  9.     public float jumpTimer = 1.5f;
  10.  
  11.     public float InAirMoveSpeed = 5.5f;
  12.  
  13.     bool isRotate = false;
  14.  
  15.     public bool ThirdPerson = true;
  16.                  
  17.     float directionX = 0;                  
  18.     float directionZ = 0;
  19.  
  20.     public float x;
  21.     public float y;
  22.  
  23.     public string platform = "";            
  24.  
  25.     Vector3 startPos;                      
  26.     float rotationTime = 0;                
  27.     float radius;                          
  28.     Quaternion fromRotation;                
  29.     Quaternion toRotation;
  30.  
  31.     Transform playerFollower;
  32.  
  33.     float distToGround;
  34.  
  35.     public bool canJump = true;
  36.  
  37.  
  38.     void Start()
  39.     {
  40.         playerFollower = GameObject.FindWithTag("PlayerFollower").transform;
  41.         radius = sideLength * Mathf.Sqrt(2f) / 2f;
  42.  
  43.         distToGround = GetComponent<Collider>().bounds.extents.y;
  44.     }
  45.  
  46.     void Update()
  47.     {
  48.         if (Input.GetKeyDown(KeyCode.V))
  49.         {
  50.             ThirdPerson = !ThirdPerson;
  51.         }
  52.         if(Input.GetKey(KeyCode.LeftShift))
  53.         {
  54.             rotationPeriod = 0.3f;
  55.         }
  56.  
  57.         else if(!Input.GetKey(KeyCode.LeftShift))
  58.         {
  59.             rotationPeriod = 0.4f;
  60.         }
  61.  
  62.  
  63.         if(Input.GetKeyDown(KeyCode.Space))
  64.         {
  65.             //Jump
  66.             if(isGrounded() && canJump == true)
  67.             {
  68.                 canJump = false;
  69.  
  70.                 Invoke("resetIsJumping", jumpTimer);
  71.  
  72.                 Jump();
  73.             }
  74.         }
  75.        
  76.         y = -Input.GetAxisRaw("Horizontal");
  77.        
  78.         x = Input.GetAxisRaw("Vertical");
  79.  
  80.  
  81.        
  82.         if ((x != 0 || y != 0) && !isRotate)
  83.         {
  84.             directionX = y;
  85.             directionZ = x;
  86.             startPos = transform.position;
  87.             fromRotation = transform.rotation;
  88.             transform.Rotate(directionZ * 90, 0, directionX * 90, Space.World);
  89.             toRotation = transform.rotation;
  90.             transform.rotation = fromRotation;
  91.             rotationTime = 0;
  92.            
  93.             if(isGrounded() == true)
  94.             {
  95.                 isRotate = true;
  96.             }
  97.         }
  98.     }
  99.  
  100.     void FixedUpdate()
  101.     {
  102.         if (ThirdPerson && isRotate )
  103.         {
  104.             rotationTime += Time.fixedDeltaTime;                                    
  105.             float ratio = Mathf.Lerp(0, 1, rotationTime / rotationPeriod);          
  106.  
  107.            
  108.             float thetaRad = Mathf.Lerp(0, Mathf.PI / 2f, ratio);                  
  109.             float distanceX = -directionX * radius * (Mathf.Cos(45f * Mathf.Deg2Rad) - Mathf.Cos(45f * Mathf.Deg2Rad + thetaRad));      
  110.             float distanceY = radius * (Mathf.Sin(45f * Mathf.Deg2Rad + thetaRad) - Mathf.Sin(45f * Mathf.Deg2Rad));                    
  111.             float distanceZ = directionZ * radius * (Mathf.Cos(45f * Mathf.Deg2Rad) - Mathf.Cos(45f * Mathf.Deg2Rad + thetaRad));      
  112.             transform.position = new Vector3(startPos.x + distanceX, startPos.y + distanceY, startPos.z + distanceZ);                  
  113.  
  114.            
  115.             transform.rotation = Quaternion.Lerp(fromRotation, toRotation, ratio);      
  116.  
  117.            
  118.             if (ratio == 1)
  119.             {
  120.                 isRotate = false;
  121.                 directionX = 0;
  122.                 directionZ = 0;
  123.                 rotationTime = 0;
  124.             }
  125.         }
  126.  
  127.         if(!isGrounded())
  128.         {
  129.             transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * InAirMoveSpeed, 0, Input.GetAxis("Vertical") * Time.deltaTime * InAirMoveSpeed, Space.World);
  130.         }
  131.     }
  132.  
  133.     public bool isGrounded()
  134.     {
  135.         return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
  136.     }
  137.  
  138.     private void resetIsJumping()
  139.     {
  140.         canJump = true;
  141.     }
  142.  
  143.     private void Jump()
  144.     {
  145.         GetComponent<Rigidbody>().AddForce(new Vector3(0, 500, 0));
  146.     }
  147.  
  148.     private void OnCollisionStay(Collision collision)
  149.     {
  150.         if(collision.transform.tag == "Platform")
  151.         {
  152.             //On platform
  153.             platform = collision.gameObject.name;
  154.         }
  155.     }
  156.  
  157.     private void OnCollisionExit(Collision collision)
  158.     {
  159.         if(collision.transform.tag == "Platform")
  160.         {
  161.             //No longer on platform
  162.             platform = "";
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement