Advertisement
Purianite

Untitled

Feb 25th, 2016
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.57 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7.     public float throttleForce = 50.0f;
  8.     public float maxSpeed = 200.0f;
  9.     public float jumpForce = 10.0f;
  10.     public float strafeSpeed = 0.15f;
  11.     public float accelDeadZone = 0.004f;
  12.     public float hitDistance = 0.5f;
  13.     public float jumpHitDistance = 0.15f;
  14.     public float rotationTimeframe = 0.4f;
  15.     bool isFalling = false;
  16.     Rigidbody rb;
  17.     BoxCollider boxCol;
  18.     float targetForce;
  19.     float accelTilt;
  20.     Vector3 vertSpeed;
  21.     public float currentSpeed;
  22.     Slider throttle;
  23.     Slider speedometer;
  24.     public bool onGround;
  25.     Quaternion targetRotation;
  26.     float rotationTimePassed = 0.0f;
  27.     Quaternion currentRotation;
  28.     Vector3 localVel; //local velocity for zeroing out unwanted forces
  29.     float trackGravity = 9.81f;
  30.  
  31.     //Vector3 relativeVelocity;
  32.  
  33.     // Use this for initialization
  34.     void Start()
  35.     {
  36.         rb = GetComponent<Rigidbody>();
  37.         boxCol = GetComponent<BoxCollider>();
  38.         throttle = GameObject.Find("ThrottleControl").GetComponent<Slider>();
  39.         speedometer = GameObject.Find("Speedometer").GetComponent<Slider>();
  40.  
  41.     }
  42.  
  43.     // Update is called once per frame
  44.     void FixedUpdate()
  45.     {
  46.         RaycastHit hit;
  47.         Ray alignRay = new Ray(transform.position, -transform.up);
  48.         Ray downRay = new Ray(transform.position, Vector3.down);
  49.  
  50.         Debug.DrawRay(alignRay.origin, alignRay.direction, Color.magenta);
  51.         Debug.DrawRay(downRay.origin, downRay.direction, Color.green);
  52.  
  53.         if (Input.GetKeyDown(KeyCode.Escape))
  54.         {
  55.             Application.Quit();
  56.         }
  57.  
  58.         if (Input.GetKey(KeyCode.R))
  59.         {
  60.             Application.LoadLevel(Application.loadedLevelName);
  61.         }
  62.  
  63.         if (Physics.Raycast(alignRay, out hit, hitDistance))
  64.         {
  65.             if (hit.distance <= jumpHitDistance)
  66.             {
  67.                 onGround = true;
  68.                 //rb.useGravity = false;
  69.             }
  70.             else
  71.             {
  72.                 onGround = false;
  73.                 //rb.useGravity = true;
  74.             }
  75.             targetRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
  76.  
  77.             if (rotationTimePassed < rotationTimeframe)
  78.             {
  79.                 rotationTimePassed += Time.deltaTime;
  80.                 Mathf.Clamp(rotationTimePassed, 0, rotationTimeframe);
  81.             }
  82.  
  83.             currentRotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationTimePassed / rotationTimeframe);
  84.             rb.MoveRotation(currentRotation);
  85.  
  86.             if (Quaternion.Angle(currentRotation, rb.rotation) == 0)
  87.             {
  88.                 rotationTimePassed = 0;
  89.                 //rb.AddForce(-transform.up * trackGravity);
  90.                 //rb.useGravity = false;
  91.             }
  92.  
  93.             //rb.MoveRotation(Quaternion.FromToRotation(Vector3.up, hit.normal));
  94.         }
  95.  
  96.         //else rb.useGravity = true;
  97.  
  98.        
  99.         //Debug.Log(rotationTimePassed);
  100.  
  101.         //Update relative velocity
  102.         //relativeVelocity = transform.InverseTransformDirection(rigidbody.velocity);
  103.         /*
  104.         targetForce = throttle.value * maxForce;
  105.         currentSpeed = transform.InverseTransformDirection(rb.velocity).z;
  106.         speedometer.value = currentSpeed / maxForce;
  107.         vertSpeed = Vector3.up * rb.velocity.y;
  108.         */
  109.  
  110.         /*
  111.         if (currentSpeed != (throttle.value * maxForce))
  112.         {
  113.             currentSpeed = Mathf.Lerp(currentSpeed, targetForce, adjustRate * Time.deltaTime);
  114.         }
  115.         */
  116.  
  117.         localVel = transform.InverseTransformDirection(rb.velocity);
  118.  
  119.         //Add throttle force
  120.         rb.AddRelativeForce(Vector3.forward * throttleForce * Input.GetAxis("Throttle"));
  121.  
  122.         //Negate sideways velocity since we're only moving with MovePosition()
  123.         rb.AddRelativeForce(transform.right * -transform.InverseTransformDirection(localVel).x, ForceMode.Impulse);
  124.        
  125.         //Move
  126.         //rb.velocity += Vector3.forward * acceleration * Input.GetAxis("Vertical");
  127. #if UNITY_EDITOR
  128.         rb.MovePosition(rb.position + (rb.rotation * Vector3.right) * strafeSpeed * Time.deltaTime * Input.GetAxis("Horizontal"));
  129.         //rb.MovePosition(Vector3.right * strafeSpeed * Input.GetAxis("Horizontal") * Time.deltaTime);
  130. #endif
  131.         if (Mathf.Abs(Input.acceleration.x) >= accelDeadZone)
  132.         {
  133.             accelTilt = Input.acceleration.x;
  134.         }
  135.         else accelTilt = 0;
  136.  
  137.         //rb.AddRelativeForce(Vector3.right * strafeSpeed * Time.deltaTime * accelTilt, ForceMode.VelocityChange);
  138.         rb.MovePosition(rb.position + transform.right * strafeSpeed * Time.deltaTime * accelTilt);
  139.         //isFalling = Mathf.Abs((int)rigidbody.velocity.y)(Physics.Linecast(transform.position, transform.position));
  140.         if (Input.GetButtonDown("Jump"))
  141.         {
  142.             Jump();
  143.         }
  144.  
  145.         if  (rb.velocity.magnitude > maxSpeed)
  146.         {
  147.             rb.velocity = rb.velocity.normalized * maxSpeed;
  148.         }
  149.     }
  150.  
  151.     public void Jump()
  152.     {
  153.         if (onGround)
  154.         {
  155.             rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
  156.         }
  157.     }
  158.  
  159.     void move(Vector3 motion, float dt)
  160.     {
  161.         //float vertSpeed = m_verticalSpeed;
  162.  
  163.  
  164.         //Vector3 vec = motion + new Vector3(0, vertSpeed, 0);
  165.         //vec += new Vector3(m_horz, 0, 0);
  166.         //vec *= dt;
  167.  
  168.  
  169.         if (GetComponent<Rigidbody>())
  170.         {
  171.             //GetComponent<Rigidbody>().velocity = vec;
  172.         }
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement