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 acceleration = 0.1f;
- public float adjustRate = 0.3f;
- public float maxSpeed = 10.0f;
- public float jumpSpeed = 10.0f;
- public float strafeSpeed = 0.15f;
- public float accelDeadZone = 0.004f;
- public float hitDistance = 1.5f;
- public float jumpHitDistance = 0.15f;
- bool isFalling = false;
- Rigidbody rb;
- BoxCollider boxCol;
- float targetSpeed;
- float accelTilt;
- Vector3 vertSpeed;
- public float currentSpeed;
- Slider throttle;
- Slider speedometer;
- public bool onGround;
- //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);
- Debug.DrawRay(alignRay.origin, alignRay.direction, Color.magenta);
- 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;
- }
- else onGround = false;
- rb.MoveRotation(Quaternion.FromToRotation(Vector3.up, hit.normal));
- }
- //Update relative velocity
- //relativeVelocity = transform.InverseTransformDirection(rigidbody.velocity);
- targetSpeed = throttle.value * maxSpeed;
- currentSpeed = transform.InverseTransformDirection(rb.velocity).z;
- speedometer.value = currentSpeed / maxSpeed;
- vertSpeed = Vector3.up * rb.velocity.y;
- if (currentSpeed != (throttle.value * maxSpeed))
- {
- currentSpeed = Mathf.Lerp(currentSpeed, targetSpeed, adjustRate * Time.deltaTime);
- }
- rb.velocity = (transform.forward * currentSpeed) + vertSpeed;
- //Move
- //rb.velocity += Vector3.forward * acceleration * Input.GetAxis("Vertical");
- #if UNITY_EDITOR
- rb.AddRelativeForce(Vector3.right * strafeSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, ForceMode.Impulse);
- #endif
- if (Mathf.Abs(Input.acceleration.x) >= accelDeadZone)
- {
- accelTilt = Input.acceleration.x;
- }
- else accelTilt = 0;
- rb.AddRelativeForce(Vector3.right * strafeSpeed * Time.deltaTime * accelTilt, ForceMode.Impulse);
- //isFalling = Mathf.Abs((int)rigidbody.velocity.y)(Physics.Linecast(transform.position, transform.position));
- if (Input.GetButtonDown("Jump"))
- {
- Jump();
- }
- }
- public void Jump()
- {
- if (onGround)
- {
- rb.velocity = new Vector3(rb.velocity.x, jumpSpeed, rb.velocity.z);
- }
- }
- 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