123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
-
- // =================================
- // Namespaces.
- // =================================
- using UnityEngine;
- // =================================
- // Define namespace.
- // =================================
- namespace MirzaBeig
- {
- namespace Demos
- {
- namespace TheLastParticle
- {
- // =================================
- // Classes.
- // =================================
- //[ExecuteInEditMode]
- [System.Serializable]
- public class Player : MonoBehaviour
- {
- // =================================
- // Nested classes and structures.
- // =================================
- // ...
- // =================================
- // Variables.
- // =================================
- // ...
- public Rigidbody rb { get; set; }
- // ...
- public float thrust = 0.125f;
- public float boostThrustMult = 2.0f;
- // ...
- new Camera camera;
- public float rotateToCameraForwardSpeed = Mathf.Infinity;
- // ...
- public float cameraFieldOfViewMaxSpeed = 20.0f;
- public Vector2 cameraFieldOfViewAngleRange = new Vector2(50.0f, 75.0f);
- // ...
- Vector3 startPosition;
- Quaternion startRotation;
- // =================================
- // Functions.
- // =================================
- // ...
- void Awake()
- {
- }
- // ...
- void Start()
- {
- rb = GetComponent<Rigidbody>();
- startPosition = transform.position;
- startRotation = transform.rotation;
- lockMouse();
- camera = Camera.main;
- }
- // ...
- void lockMouse()
- {
- Cursor.visible = false;
- Cursor.lockState = CursorLockMode.Locked;
- }
- void unlockMouse()
- {
- Cursor.visible = true;
- Cursor.lockState = CursorLockMode.None;
- }
- // ...
- void FixedUpdate()
- {
- // Input.
- float vertical = Input.GetAxis("Vertical");
- float horizontal = Input.GetAxis("Horizontal");
- //float forward = Input.GetKey(KeyCode.LeftShift) ? 1.0f : 0.0f;
- Vector3 force = Vector3.zero;
- force += Vector3.forward * vertical;
- force += Vector3.right * horizontal;
- force *= thrust;
- bool boost = Input.GetKey(KeyCode.LeftShift);
- if (boost)
- {
- force *= boostThrustMult;
- }
- rb.AddRelativeForce(force, ForceMode.VelocityChange);
- // ...
- float currentSpeed = rb.velocity.magnitude;
- float cameraFieldOfViewSpeedTime = currentSpeed / cameraFieldOfViewMaxSpeed;
- cameraFieldOfViewSpeedTime = Mathf.Clamp01(cameraFieldOfViewSpeedTime);
- camera.fieldOfView = Mathf.Lerp(
- cameraFieldOfViewAngleRange.x, cameraFieldOfViewAngleRange.y, cameraFieldOfViewSpeedTime);
- }
- // ...
- void Update()
- {
- if (Input.GetMouseButtonDown(0))
- {
- lockMouse();
- }
- else if (Input.GetKeyDown(KeyCode.Escape))
- {
- unlockMouse();
- }
- if (Input.GetKeyDown(KeyCode.R))
- {
- transform.position = startPosition;
- transform.rotation = startRotation;
- }
- }
- // ...
- void LateUpdate()
- {
- transform.rotation = Damping.dampQuaternion(transform.rotation,
- Quaternion.LookRotation(camera.transform.forward), rotateToCameraForwardSpeed, Time.deltaTime);
- }
- // =================================
- // End functions.
- // =================================
- }
- // =================================
- // End namespace.
- // =================================
- }
- }
- }
- // =================================
- // --END-- //
- // =================================
|