Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- public class Player : MonoBehaviour
- {
- public float throttleForce = 50.0f;
- public float maxSpeed = 200.0f;
- public float jumpForce = 10.0f;
- public float strafeSpeed = 0.15f;
- public float accelDeadZone = 0.004f;
- public float hitDistance = 0.5f;
- public float jumpHitDistance = 0.15f;
- public float rotationTimeframe = 0.4f;
- bool isFalling = false;
- Rigidbody rb;
- BoxCollider boxCol;
- float targetForce;
- float accelTilt;
- Vector3 vertSpeed;
- public float currentSpeed;
- Slider throttle;
- Slider speedometer;
- public bool onGround;
- Quaternion targetRotation;
- float rotationTimePassed = 0.0f;
- Quaternion currentRotation;
- //Vector3 relativeVelocity;
- // Use this for initialization
- void Start()
- {
- rb = GetComponent<Rigidbody>();
- boxCol = GetComponent<BoxCollider>();
- throttle = GameObject.Find("ThrottleControl").GetComponent<Slider>();
- speedometer = GameObject.Find("Speedometer").GetComponent<Slider>();
- }
- // Update is called once per frame
- void FixedUpdate()
- {
- RaycastHit hit;
- Ray alignRay = new Ray(transform.position, -transform.up);
- Ray downRay = new Ray(transform.position, Vector3.down);
- Debug.DrawRay(alignRay.origin, alignRay.direction, Color.magenta);
- Debug.DrawRay(downRay.origin, downRay.direction, Color.green);
- if (Input.GetKeyDown(KeyCode.Escape))
- {
- Application.Quit();
- }
- if (Input.GetKey(KeyCode.Menu))
- {
- Application.LoadLevel(Application.loadedLevelName);
- }
- if (Physics.Raycast(alignRay, out hit, hitDistance))
- {
- if (hit.distance <= jumpHitDistance)
- {
- onGround = true;
- //rb.useGravity = false;
- }
- else
- {
- onGround = false;
- //rb.useGravity = true;
- }
- targetRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
- if (rotationTimePassed < rotationTimeframe)
- {
- rotationTimePassed += Time.deltaTime;
- Mathf.Clamp(rotationTimePassed, 0, rotationTimeframe);
- }
- currentRotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationTimePassed/rotationTimeframe);
- rb.MoveRotation(currentRotation);
- //rb.MoveRotation(Quaternion.FromToRotation(Vector3.up, hit.normal));
- }
- //Update relative velocity
- //relativeVelocity = transform.InverseTransformDirection(rigidbody.velocity);
- /*
- targetForce = throttle.value * maxForce;
- currentSpeed = transform.InverseTransformDirection(rb.velocity).z;
- speedometer.value = currentSpeed / maxForce;
- vertSpeed = Vector3.up * rb.velocity.y;
- */
- /*
- if (currentSpeed != (throttle.value * maxForce))
- {
- currentSpeed = Mathf.Lerp(currentSpeed, targetForce, adjustRate * Time.deltaTime);
- }
- */
- rb.AddRelativeForce(Vector3.forward * throttleForce * Input.GetAxis("Throttle"));
- //Move
- //rb.velocity += Vector3.forward * acceleration * Input.GetAxis("Vertical");
- #if UNITY_EDITOR
- rb.MovePosition(rb.position + (rb.rotation * Vector3.right) * strafeSpeed * Time.deltaTime * Input.GetAxis("Horizontal"));
- //rb.MovePosition(Vector3.right * strafeSpeed * Input.GetAxis("Horizontal") * Time.deltaTime);
- #endif
- if (Mathf.Abs(Input.acceleration.x) >= accelDeadZone)
- {
- accelTilt = Input.acceleration.x;
- }
- else accelTilt = 0;
- //rb.AddRelativeForce(Vector3.right * strafeSpeed * Time.deltaTime * accelTilt, ForceMode.VelocityChange);
- rb.MovePosition(rb.position + transform.right * strafeSpeed * Time.deltaTime * accelTilt);
- //isFalling = Mathf.Abs((int)rigidbody.velocity.y)(Physics.Linecast(transform.position, transform.position));
- if (Input.GetButtonDown("Jump"))
- {
- Jump();
- }
- if (rb.velocity.magnitude > maxSpeed)
- {
- rb.velocity = rb.velocity.normalized * maxSpeed;
- }
- }
- public void Jump()
- {
- if (onGround)
- {
- rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
- }
- }
- void move(Vector3 motion, float dt)
- {
- //float vertSpeed = m_verticalSpeed;
- //Vector3 vec = motion + new Vector3(0, vertSpeed, 0);
- //vec += new Vector3(m_horz, 0, 0);
- //vec *= dt;
- if (GetComponent<Rigidbody>())
- {
- //GetComponent<Rigidbody>().velocity = vec;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement