Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.PostProcessing;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- public class PlayerControl : MonoBehaviour {
- public Vector3 targetPosition;
- public float speed = 1000f;
- public float maxSpeed = 1000f;
- public float minSpeed = 300f;
- public float accel = 50f;
- public float deccel = 50f;
- private float currAccel = 0f;
- private float currDeccel = 0f;
- public float turnSpeed = 150f;
- public float lerpSpeed = 0.3f;
- public float minThrusterScale = 1f;
- public float maxThrusterScale = 3f;
- public GameObject thrusters;
- public float scaleSpeed = 5f;
- public static float playerMaxSize = 8f;
- public static float playerMinSize = 1f;
- public static float currentScale = playerMinSize;
- public AnimationCurve accellerationCurve;
- public AnimationCurve deccellerationCurve;
- private Rigidbody rb;
- Vector3 velocity;
- public AudioClip stretchSound;
- public AudioSource audioSource;
- public static float GetPlayerScaleAsPercent()
- {
- return (currentScale - playerMinSize) / (playerMaxSize - playerMinSize);
- }
- public static float GetCurrentScale()
- {
- return currentScale;
- }
- // Use this for initialization
- void Start () {
- targetPosition = transform.position;
- if (audioSource != null)
- audioSource.clip = stretchSound;
- rb = GetComponent<Rigidbody>();
- currentScale = playerMinSize;
- MasterGameControl.SetPlayerAlive(true);
- }
- // Update is called once per frame
- void Update ()
- {
- if (!Input.GetMouseButton(0) && !Input.GetMouseButton(1))
- {
- if (audioSource != null)
- {
- if (audioSource.isPlaying)
- audioSource.Stop();
- }
- }
- //SetSpeed();
- //SetThrusterScale();
- }
- private void SetSpeed()
- {
- if (Input.GetKey(KeyCode.D) && speed < maxSpeed)
- {
- if(currAccel < 1.0f)
- {
- currAccel += 1f * Time.deltaTime;
- }
- float localAccel = accel * accellerationCurve.Evaluate(currAccel);
- speed += localAccel;
- }
- else
- {
- currAccel = 0f;
- }
- if (Input.GetKey(KeyCode.A) && speed > minSpeed)
- {
- if (currDeccel < 1.0f)
- {
- currDeccel += 1f * Time.deltaTime;
- }
- float localDeccel = deccel * deccellerationCurve.Evaluate(currDeccel);
- speed -= localDeccel;
- }
- else
- {
- currDeccel = 0f;
- }
- }
- private void SetThrusterScale()
- {
- //0f - 1f
- float currentThrusterScalePercent = (speed - minSpeed) / (maxSpeed - minSpeed);
- thrusters.transform.localScale = new Vector3(1f, 1f, (maxThrusterScale - minThrusterScale) * currentThrusterScalePercent + minThrusterScale);
- }
- void FixedUpdate()
- {
- //UpdateVelocity();
- ApplyForce();
- UpdateRotation();
- }
- private void ApplyForce()
- {
- rb.AddForce(new Vector3(speed, 0, 0), ForceMode.Acceleration);
- }
- private void UpdateRotation()
- {
- Quaternion rot = Quaternion.Euler(rb.velocity.y * 30f * Time.deltaTime, -90, -(rb.velocity.z * 30f * Time.fixedDeltaTime));
- rb.MoveRotation(rot);
- }
- private void UpdateVelocity()
- {
- //Calculate new velocity
- velocity = new Vector3(speed, (targetPosition.y - transform.position.y) * turnSpeed, (targetPosition.z - transform.position.z) * turnSpeed);
- rb.velocity = velocity * Time.fixedDeltaTime;
- }
- private void playStretchSound()
- {
- if (audioSource == null) return;
- audioSource.Play();
- }
- public AudioSource GetAudioSource()
- {
- return audioSource;
- }
- public Vector3 GetWorldPositionOnPlane(Vector3 screenpos, float z)
- {
- Ray ray = Camera.main.ScreenPointToRay(screenpos);
- Plane xy = new Plane(Vector3.right, new Vector3(0, 0, z));
- float distance;
- xy.Raycast(ray, out distance);
- return ray.GetPoint(distance);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement