Advertisement
Purianite

Untitled

Feb 23rd, 2016
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.61 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7.  
  8.     public float acceleration = 0.1f;
  9.     public float adjustRate = 0.3f;
  10.     public float maxSpeed = 10.0f;
  11.     public float jumpSpeed = 10.0f;
  12.     public float strafeSpeed = 0.15f;
  13.     public float accelDeadZone = 0.004f;
  14.     public float hitDistance = 1.5f;
  15.     public float jumpHitDistance = 0.15f;
  16.     bool isFalling = false;
  17.     Rigidbody rb;
  18.     BoxCollider boxCol;
  19.     float targetSpeed;
  20.     float accelTilt;
  21.     Vector3 vertSpeed;
  22.     public float currentSpeed;
  23.     Slider throttle;
  24.     Slider speedometer;
  25.     public bool onGround;
  26.  
  27.     //Vector3 relativeVelocity;
  28.  
  29.     // Use this for initialization
  30.     void Start()
  31.     {
  32.         rb = GetComponent<Rigidbody>();
  33.         boxCol = GetComponent<BoxCollider>();
  34.         throttle = GameObject.Find("ThrottleControl").GetComponent<Slider>();
  35.         speedometer = GameObject.Find("Speedometer").GetComponent<Slider>();
  36.  
  37.     }
  38.  
  39.     // Update is called once per frame
  40.     void FixedUpdate()
  41.     {
  42.         RaycastHit hit;
  43.         Ray alignRay = new Ray(transform.position, -transform.up);
  44.  
  45.         Debug.DrawRay(alignRay.origin, alignRay.direction, Color.magenta);
  46.  
  47.         if (Input.GetKeyDown(KeyCode.Escape))
  48.         {
  49.             Application.Quit();
  50.         }
  51.  
  52.         if (Input.GetKey(KeyCode.Menu))
  53.         {
  54.             Application.LoadLevel(Application.loadedLevelName);
  55.         }
  56.  
  57.         if (Physics.Raycast(alignRay, out hit, hitDistance))
  58.         {
  59.             if (hit.distance <= jumpHitDistance)
  60.             {
  61.                 onGround = true;
  62.             }
  63.             else onGround = false;
  64.  
  65.             rb.MoveRotation(Quaternion.FromToRotation(Vector3.up, hit.normal));
  66.         }
  67.  
  68.         //Update relative velocity
  69.         //relativeVelocity = transform.InverseTransformDirection(rigidbody.velocity);
  70.         targetSpeed = throttle.value * maxSpeed;
  71.         currentSpeed = transform.InverseTransformDirection(rb.velocity).z;
  72.         speedometer.value = currentSpeed / maxSpeed;
  73.         vertSpeed = Vector3.up * rb.velocity.y;
  74.  
  75.         if (currentSpeed != (throttle.value * maxSpeed))
  76.         {
  77.             currentSpeed = Mathf.Lerp(currentSpeed, targetSpeed, adjustRate * Time.deltaTime);
  78.         }
  79.  
  80.         rb.velocity = (transform.forward * currentSpeed) + vertSpeed;
  81.  
  82.         //Move
  83.         //rb.velocity += Vector3.forward * acceleration * Input.GetAxis("Vertical");
  84. #if UNITY_EDITOR
  85.         rb.AddRelativeForce(Vector3.right * strafeSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, ForceMode.Impulse);
  86. #endif
  87.         if (Mathf.Abs(Input.acceleration.x) >= accelDeadZone)
  88.         {
  89.             accelTilt = Input.acceleration.x;
  90.         }
  91.         else accelTilt = 0;
  92.  
  93.         rb.AddRelativeForce(Vector3.right * strafeSpeed * Time.deltaTime * accelTilt, ForceMode.Impulse);
  94.         //isFalling = Mathf.Abs((int)rigidbody.velocity.y)(Physics.Linecast(transform.position, transform.position));
  95.         if (Input.GetButtonDown("Jump"))
  96.         {
  97.             Jump();
  98.         }
  99.     }
  100.  
  101.     public void Jump()
  102.     {
  103.         if (onGround)
  104.         {
  105.             rb.velocity = new Vector3(rb.velocity.x, jumpSpeed, rb.velocity.z);
  106.         }
  107.     }
  108.  
  109.     void move(Vector3 motion, float dt)
  110.     {
  111.         //float vertSpeed = m_verticalSpeed;
  112.  
  113.  
  114.         //Vector3 vec = motion + new Vector3(0, vertSpeed, 0);
  115.         //vec += new Vector3(m_horz, 0, 0);
  116.         //vec *= dt;
  117.  
  118.  
  119.         if (GetComponent<Rigidbody>())
  120.         {
  121.             //GetComponent<Rigidbody>().velocity = vec;
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement