Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Jump : MonoBehaviour {
- private Rigidbody rb;
- public float jumpForce;
- private bool isGrounded = false;
- private bool canJump = true;
- public float rotateSpeed;
- public float moveSpeed;
- [Tooltip("Is the super jump power up active?")]
- public bool superJump = false;
- private GameObject hud;
- [Tooltip("Can you jump?")]
- public bool ableToJump = true;
- [Tooltip("Are you using the jetpack?")]
- public bool jetpackMode = false;
- [Tooltip("Jetpack")]
- public GameObject jetpack;
- [Tooltip("Jetpack particles")]
- public ParticleSystem fire;
- [Tooltip("Jetpack particles")]
- public ParticleSystem fire2;
- private float startingPoint;
- private float newPoint;
- [Tooltip("The times the speed changes")]
- public float[] times;
- [Tooltip("The speed when the speed changes")]
- public float[] speedForTimes;
- private float totalTime = 0;
- private float totalDistance = 0;
- [Tooltip("Should music start based on position?")]
- public bool isTesting;
- [Tooltip("Use this to make sure you can't fly very high")]
- public GameObject[] topGround;
- public Animator jetpackAnim;
- public GameObject jetpackTimer;
- private Vector3 rayCenter;
- public Animator finishMenu;
- // Use this for initialization
- void Start () {
- if (isTesting == true) {
- foreach (float i in times)
- {
- totalTime += i;
- }
- for (int i = 0; i < times.Length; i ++) {
- float e = times[i] * speedForTimes[i];
- totalDistance += e;
- }
- float r = transform.position.x - (times[times.Length - 1] * speedForTimes[speedForTimes.Length - 2]) ;
- r /= speedForTimes[speedForTimes.Length - 1];
- startingPoint = totalTime + r;
- ChangePlayerSpeed[] speeds = GetComponents<ChangePlayerSpeed>();
- foreach (ChangePlayerSpeed i in speeds)
- {
- if (i.delayTime <= startingPoint) {
- Destroy(i);
- }
- }
- ChangePlayerY[] cpy = GetComponents<ChangePlayerY>();
- foreach (ChangePlayerY i in cpy)
- {
- if (i.delayTime <= startingPoint)
- {
- Destroy(i);
- }
- }
- //newPoint = transform.position.x;
- moveSpeed = speedForTimes[speedForTimes.Length - 1];
- AudioSource music = GameObject.Find("Music").GetComponent<AudioSource>();
- music.time = startingPoint;
- music.Play();
- foreach (GameObject e in topGround) {
- e.SetActive(false);
- }
- }
- else {
- moveSpeed = 5;
- GameObject.Find("Music").GetComponent<AudioSource>().Play();
- GameObject.Find("Music").GetComponent<AudioSource>().volume = PlayerPrefs.GetFloat("Volume");
- }
- rb = GetComponent<Rigidbody>();
- hud = GameObject.Find("HUD");
- //fire = jetpack.transform.GetChild(0).GetComponent<ParticleSystem>();
- fire.Stop();
- fire2.Stop();
- }
- // Update is called once per frame
- void Update() {
- rayCenter = transform.position;
- rayCenter.x = rayCenter.x + 0.3f;
- if (Physics.Raycast(rayCenter, Vector3.down, 1 / 2 + 0.75f))
- {
- if (jetpackMode == false)
- {
- Quaternion rot = transform.rotation;
- rot.z /= 90;
- rot.z = Mathf.Round(rot.z);
- rot.z *= 90;
- rot.x = 0;
- rot.y = 0;
- transform.rotation = rot;
- rb.angularVelocity = Vector3.zero;
- isGrounded = true;
- }
- }
- else
- {
- isGrounded = false;
- }
- Debug.DrawRay(rayCenter, Vector3.down * (1 / 2 + 0.65f), Color.green);
- if (Input.GetMouseButton(0) && canJump == true && isGrounded == true && ableToJump && jetpackMode == false)
- {
- Hop();
- }
- if (Input.GetKey(KeyCode.UpArrow) && ableToJump && jetpackMode == false && canJump == true && isGrounded == true)
- {
- Hop();
- }
- if (Input.GetKey(KeyCode.Space) && ableToJump && jetpackMode == false && canJump == true && isGrounded == true)
- {
- Hop();
- }
- if (Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0) || Input.GetKey(KeyCode.UpArrow)) {
- if (jetpackMode == true) {
- Fly();
- }
- }
- if (Input.GetKeyUp(KeyCode.Space) || Input.GetMouseButtonUp(0) || Input.GetKeyUp(KeyCode.UpArrow))
- {
- if (jetpackMode == true)
- {
- StopFly();
- }
- }
- if (jetpack.activeSelf == false && jetpackMode == true)
- {
- jetpack.SetActive(true);
- }
- }
- void FixedUpdate()
- {
- Vector3 vel = rb.velocity;
- vel.x = moveSpeed;
- rb.velocity = vel;
- if (isGrounded == false && jetpackMode == false)
- {
- transform.Rotate(Vector3.back, Time.fixedDeltaTime * rotateSpeed);
- }
- }
- void OnTriggerEnter(Collider coll) {
- if (coll.gameObject.tag == "Spring") {
- SuperJump();
- }
- if (coll.gameObject.tag == "Flag") {
- Finish();
- }
- if (coll.gameObject.tag == "Jetpack")
- {
- jetpackMode = true;
- transform.eulerAngles = new Vector3(0, 0, -5);
- rb.constraints = RigidbodyConstraints.FreezeRotation;
- Destroy(coll.gameObject);
- Camera.main.GetComponent<CameraFollow>().FollowY(false);
- foreach (GameObject e in topGround) {
- e.SetActive(true);
- }
- rb.mass = rb.mass / 3;
- Invoke("EndFly", 13f);
- jetpackTimer.SetActive(true);
- jetpackAnim.SetBool("JetPack", true);
- }
- if (coll.gameObject.tag == "DeathZone") {
- }
- }
- void Reset() {
- canJump = true;
- }
- void Hop() {
- if (superJump == true)
- {
- Vector3 zero = rb.velocity;
- zero.y = 0;
- rb.velocity = zero;
- rb.AddForce(Vector2.up * (jumpForce * 1.3f));
- canJump = false;
- Invoke("Reset", 0.2f);
- }
- if(superJump == false && canJump == true && isGrounded == true) {
- Vector3 zero = rb.velocity;
- zero.y = 0;
- rb.velocity = zero;
- rb.AddForce(Vector2.up * jumpForce);
- canJump = false;
- Invoke("Reset", 0.1f);
- }
- }
- void SuperJump() {
- Vector3 zero = rb.velocity;
- zero.y = 0;
- rb.velocity = zero;
- rb.AddForce(Vector2.up * (jumpForce * 1.5f));
- canJump = false;
- Invoke("Reset", 0.1f);
- }
- void StopSJ () {
- superJump = false;
- }
- public void StartSJ () {
- superJump = true;
- hud.GetComponent<PowerUpManager>().SuperJump();
- Invoke("StopSJ", 5);
- }
- void Finish () {
- GetComponent<Death>().finished = true;
- moveSpeed = 0;
- rb.velocity = Vector3.zero;
- finishMenu.SetBool("LevelComplete", true);
- }
- void Fly () {
- if (fire.isStopped == true) {
- fire.Play();
- fire2.Play();
- }
- rb.useGravity = false;
- Vector3 vel = rb.velocity;
- vel.x = moveSpeed;
- vel.y = moveSpeed;
- vel.z = 0;
- rb.velocity = vel;
- }
- void StopFly () {
- fire.Stop();
- fire2.Stop();
- Vector3 stop = rb.velocity;
- stop.y = 0;
- rb.velocity = stop;
- rb.useGravity = true;
- }
- void EndFly () {
- jetpackMode = false;
- rb.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezePositionZ;
- transform.position = new Vector3(transform.position.x, transform.position.y, 0);
- Camera.main.GetComponent<CameraFollow>().FollowY(true);
- foreach (GameObject e in topGround)
- {
- e.SetActive(false);
- }
- rb.mass = rb.mass * 3;
- jetpackAnim.SetBool("JetPack", false);
- jetpackTimer.SetActive(false);
- jetpack.SetActive(false);
- rb.useGravity = true;
- rb.velocity = Vector3.zero;
- rb.angularVelocity = Vector3.zero;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement