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.EventSystems;
- public class PlayerController : MonoBehaviour
- {
- public float moveSpeed;
- private float moveSpeedStore;
- public float speedMultiplier;
- public float speedIncreaseMilestone;
- private float speedIncreaseMilestoneStore;
- private float speedMilestoneCount;
- private float speedMilestoneCountStore;
- public float jumpForce;
- public float jumpTime;
- private float jumpTimeCounter;
- private bool stoppedJumping;
- private bool canDoubleJump;
- private Rigidbody2D myRigidbody;
- public bool grounded;
- public LayerMask whatIsGround;
- public Transform groundCheck;
- public float groundCheckRadius;
- private Animator myAnimator;
- public GameManager theGameManager;
- public AudioSource jumpSound;
- public AudioSource deathSound;
- // Start is called before the first frame update
- void Start()
- {
- myRigidbody = GetComponent<Rigidbody2D>();
- myAnimator = GetComponent<Animator>();
- jumpTimeCounter = jumpTime;
- speedMilestoneCount = speedIncreaseMilestone;
- moveSpeedStore = moveSpeed;
- speedMilestoneCountStore = speedMilestoneCount;
- speedIncreaseMilestoneStore = speedIncreaseMilestone;
- stoppedJumping = true;
- }
- // Update is called once per frame
- void Update()
- {
- grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
- myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);
- if (transform.position.x > speedMilestoneCount)
- {
- speedMilestoneCount += speedIncreaseMilestone;
- speedIncreaseMilestone = speedIncreaseMilestone * speedMultiplier;
- moveSpeed = moveSpeed * speedMultiplier;
- }
- // Prevent jumping when clicking UI elements
- if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
- {
- if (EventSystem.current.IsPointerOverGameObject() && EventSystem.current.currentSelectedGameObject == null)
- return;
- if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
- {
- if (EventSystem.current.IsPointerOverGameObject(Input.touches[0].fingerId))
- return;
- }
- // Jumping code
- if (grounded)
- {
- myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
- stoppedJumping = false;
- jumpSound.Play();
- }
- if (!grounded && canDoubleJump)
- {
- myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
- jumpTimeCounter = jumpTime;
- canDoubleJump = false;
- stoppedJumping = false;
- jumpSound.Play();
- }
- }
- if ((Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0)) && !stoppedJumping)
- {
- if (jumpTimeCounter > 0)
- {
- myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
- jumpTimeCounter -= Time.deltaTime;
- }
- }
- if (Input.GetKeyUp(KeyCode.Space) || Input.GetMouseButtonUp(0))
- {
- jumpTimeCounter = 0;
- stoppedJumping = true;
- }
- if (grounded)
- {
- jumpTimeCounter = jumpTime;
- canDoubleJump = true;
- }
- myAnimator.SetFloat("Speed", myRigidbody.velocity.x);
- myAnimator.SetBool("Grounded", grounded);
- }
- void OnCollisionEnter2D(Collision2D other)
- {
- if (other.gameObject.tag == "killbox")
- {
- deathSound.Play();
- theGameManager.RestartGame();
- moveSpeed = moveSpeedStore;
- speedMilestoneCount = speedMilestoneCountStore;
- speedIncreaseMilestone = speedIncreaseMilestoneStore;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement