Advertisement
Purianite

Untitled

Feb 23rd, 2016
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.85 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.  
  29.     //Vector3 relativeVelocity;
  30.  
  31.     // Use this for initialization
  32.     void Start()
  33.     {
  34.         rb = GetComponent<Rigidbody>();
  35.         boxCol = GetComponent<BoxCollider>();
  36.         throttle = GameObject.Find("ThrottleControl").GetComponent<Slider>();
  37.         speedometer = GameObject.Find("Speedometer").GetComponent<Slider>();
  38.  
  39.     }
  40.  
  41.     // Update is called once per frame
  42.     void FixedUpdate()
  43.     {
  44.         RaycastHit hit;
  45.         Ray alignRay = new Ray(transform.position, -transform.up);
  46.         Ray downRay = new Ray(transform.position, Vector3.down);
  47.  
  48.         Debug.DrawRay(alignRay.origin, alignRay.direction, Color.magenta);
  49.         Debug.DrawRay(downRay.origin, downRay.direction, Color.green);
  50.  
  51.         if (Input.GetKeyDown(KeyCode.Escape))
  52.         {
  53.             Application.Quit();
  54.         }
  55.  
  56.         if (Input.GetKey(KeyCode.Menu))
  57.         {
  58.             Application.LoadLevel(Application.loadedLevelName);
  59.         }
  60.  
  61.         if (Physics.Raycast(alignRay, out hit, hitDistance))
  62.         {
  63.             if (hit.distance <= jumpHitDistance)
  64.             {
  65.                 onGround = true;
  66.                 //rb.useGravity = false;
  67.             }
  68.             else
  69.             {
  70.                 onGround = false;
  71.                 //rb.useGravity = true;
  72.             }
  73.             targetRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
  74.            
  75.             if (rotationTimePassed < rotationTimeframe)
  76.             {
  77.                 rotationTimePassed += Time.deltaTime;
  78.                 Mathf.Clamp(rotationTimePassed, 0, rotationTimeframe);
  79.             }
  80.             currentRotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationTimePassed/rotationTimeframe);
  81.             rb.MoveRotation(currentRotation);
  82.             //rb.MoveRotation(Quaternion.FromToRotation(Vector3.up, hit.normal));
  83.         }
  84.  
  85.         //Update relative velocity
  86.         //relativeVelocity = transform.InverseTransformDirection(rigidbody.velocity);
  87.         /*
  88.         targetForce = throttle.value * maxForce;
  89.         currentSpeed = transform.InverseTransformDirection(rb.velocity).z;
  90.         speedometer.value = currentSpeed / maxForce;
  91.         vertSpeed = Vector3.up * rb.velocity.y;
  92.         */
  93.  
  94.         /*
  95.         if (currentSpeed != (throttle.value * maxForce))
  96.         {
  97.             currentSpeed = Mathf.Lerp(currentSpeed, targetForce, adjustRate * Time.deltaTime);
  98.         }
  99.         */
  100.  
  101.         rb.AddRelativeForce(Vector3.forward * throttleForce * Input.GetAxis("Throttle"));
  102.        
  103.         //Move
  104.         //rb.velocity += Vector3.forward * acceleration * Input.GetAxis("Vertical");
  105. #if UNITY_EDITOR
  106.         rb.MovePosition(rb.position + (rb.rotation * Vector3.right) * strafeSpeed * Time.deltaTime * Input.GetAxis("Horizontal"));
  107.         //rb.MovePosition(Vector3.right * strafeSpeed * Input.GetAxis("Horizontal") * Time.deltaTime);
  108. #endif
  109.         if (Mathf.Abs(Input.acceleration.x) >= accelDeadZone)
  110.         {
  111.             accelTilt = Input.acceleration.x;
  112.         }
  113.         else accelTilt = 0;
  114.  
  115.         //rb.AddRelativeForce(Vector3.right * strafeSpeed * Time.deltaTime * accelTilt, ForceMode.VelocityChange);
  116.         rb.MovePosition(rb.position + transform.right * strafeSpeed * Time.deltaTime * accelTilt);
  117.         //isFalling = Mathf.Abs((int)rigidbody.velocity.y)(Physics.Linecast(transform.position, transform.position));
  118.         if (Input.GetButtonDown("Jump"))
  119.         {
  120.             Jump();
  121.         }
  122.  
  123.         if  (rb.velocity.magnitude > maxSpeed)
  124.         {
  125.             rb.velocity = rb.velocity.normalized * maxSpeed;
  126.         }
  127.     }
  128.  
  129.     public void Jump()
  130.     {
  131.         if (onGround)
  132.         {
  133.             rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
  134.         }
  135.     }
  136.  
  137.     void move(Vector3 motion, float dt)
  138.     {
  139.         //float vertSpeed = m_verticalSpeed;
  140.  
  141.  
  142.         //Vector3 vec = motion + new Vector3(0, vertSpeed, 0);
  143.         //vec += new Vector3(m_horz, 0, 0);
  144.         //vec *= dt;
  145.  
  146.  
  147.         if (GetComponent<Rigidbody>())
  148.         {
  149.             //GetComponent<Rigidbody>().velocity = vec;
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement