Advertisement
Guest User

Untitled

a guest
May 25th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.35 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.PostProcessing;
  5. using UnityEngine.UI;
  6. using UnityEngine.SceneManagement;
  7.  
  8. public class PlayerControl : MonoBehaviour {
  9.  
  10.     public Vector3 targetPosition;
  11.     public float speed = 1000f;
  12.     public float maxSpeed = 1000f;
  13.     public float minSpeed = 300f;
  14.  
  15.     public float accel = 50f;
  16.     public float deccel = 50f;
  17.     private float currAccel = 0f;
  18.     private float currDeccel = 0f;
  19.  
  20.     public float turnSpeed = 150f;
  21.     public float lerpSpeed = 0.3f;
  22.  
  23.     public float minThrusterScale = 1f;
  24.     public float maxThrusterScale = 3f;
  25.     public GameObject thrusters;
  26.  
  27.     public float scaleSpeed = 5f;
  28.  
  29.     public static float playerMaxSize = 8f;
  30.     public static float playerMinSize = 1f;
  31.     public static float currentScale = playerMinSize;
  32.  
  33.     public AnimationCurve accellerationCurve;
  34.     public AnimationCurve deccellerationCurve;
  35.  
  36.     private Rigidbody rb;
  37.     Vector3 velocity;
  38.  
  39.     public AudioClip stretchSound;
  40.  
  41.     public AudioSource audioSource;
  42.  
  43.     public static float GetPlayerScaleAsPercent()
  44.     {
  45.         return (currentScale - playerMinSize) / (playerMaxSize - playerMinSize);
  46.     }
  47.  
  48.     public static float GetCurrentScale()
  49.     {
  50.         return currentScale;
  51.     }
  52.  
  53.     // Use this for initialization
  54.     void Start () {
  55.         targetPosition = transform.position;
  56.         if (audioSource != null)
  57.             audioSource.clip = stretchSound;
  58.  
  59.         rb = GetComponent<Rigidbody>();
  60.         currentScale = playerMinSize;
  61.         MasterGameControl.SetPlayerAlive(true);
  62.     }
  63.    
  64.     // Update is called once per frame
  65.     void Update ()
  66.     {
  67.         if (!Input.GetMouseButton(0) && !Input.GetMouseButton(1))
  68.         {
  69.             if (audioSource != null)
  70.             {
  71.                 if (audioSource.isPlaying)
  72.                     audioSource.Stop();
  73.             }
  74.         }
  75.  
  76.         //SetSpeed();
  77.         //SetThrusterScale();
  78.     }
  79.  
  80.     private void SetSpeed()
  81.     {
  82.         if (Input.GetKey(KeyCode.D) && speed < maxSpeed)
  83.         {
  84.             if(currAccel < 1.0f)
  85.             {
  86.                 currAccel += 1f * Time.deltaTime;
  87.             }
  88.             float localAccel = accel * accellerationCurve.Evaluate(currAccel);
  89.             speed += localAccel;
  90.         }
  91.         else
  92.         {
  93.             currAccel = 0f;
  94.         }
  95.  
  96.  
  97.         if (Input.GetKey(KeyCode.A) && speed > minSpeed)
  98.         {
  99.             if (currDeccel < 1.0f)
  100.             {
  101.                 currDeccel += 1f * Time.deltaTime;
  102.             }
  103.             float localDeccel = deccel * deccellerationCurve.Evaluate(currDeccel);
  104.             speed -= localDeccel;
  105.          }
  106.         else
  107.         {
  108.             currDeccel = 0f;
  109.         }
  110.  
  111.     }
  112.  
  113.     private void SetThrusterScale()
  114.     {
  115.         //0f - 1f
  116.         float currentThrusterScalePercent = (speed - minSpeed) / (maxSpeed - minSpeed);
  117.         thrusters.transform.localScale = new Vector3(1f, 1f, (maxThrusterScale - minThrusterScale) * currentThrusterScalePercent + minThrusterScale);
  118.     }
  119.  
  120.     void FixedUpdate()
  121.     {
  122.         //UpdateVelocity();
  123.  
  124.         ApplyForce();
  125.  
  126.         UpdateRotation();
  127.     }
  128.  
  129.     private void ApplyForce()
  130.     {
  131.         rb.AddForce(new Vector3(speed, 0, 0), ForceMode.Acceleration);
  132.     }
  133.  
  134.     private void UpdateRotation()
  135.     {
  136.         Quaternion rot = Quaternion.Euler(rb.velocity.y * 30f * Time.deltaTime, -90, -(rb.velocity.z * 30f * Time.fixedDeltaTime));
  137.  
  138.         rb.MoveRotation(rot);
  139.     }
  140.  
  141.     private void UpdateVelocity()
  142.     {
  143.  
  144.         //Calculate new velocity
  145.         velocity = new Vector3(speed, (targetPosition.y - transform.position.y) * turnSpeed, (targetPosition.z - transform.position.z) * turnSpeed);
  146.  
  147.         rb.velocity = velocity * Time.fixedDeltaTime;
  148.     }
  149.  
  150.     private void playStretchSound()
  151.     {
  152.         if (audioSource == null) return;
  153.        
  154.         audioSource.Play();
  155.  
  156.     }
  157.  
  158.     public AudioSource GetAudioSource()
  159.     {
  160.         return audioSource;
  161.     }
  162.  
  163.     public Vector3 GetWorldPositionOnPlane(Vector3 screenpos, float z)
  164.     {
  165.         Ray ray = Camera.main.ScreenPointToRay(screenpos);
  166.         Plane xy = new Plane(Vector3.right, new Vector3(0, 0, z));
  167.         float distance;
  168.         xy.Raycast(ray, out distance);
  169.  
  170.         return ray.GetPoint(distance);
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement