Advertisement
johnnygoodguy2000

PlayerController.cs

Apr 7th, 2024 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.07 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5.  
  6. public class PlayerController : MonoBehaviour
  7. {
  8.     public float moveSpeed;
  9.     private float moveSpeedStore;
  10.     public float speedMultiplier;
  11.     public float speedIncreaseMilestone;
  12.     private float speedIncreaseMilestoneStore;
  13.     private float speedMilestoneCount;
  14.     private float speedMilestoneCountStore;
  15.     public float jumpForce;
  16.     public float jumpTime;
  17.     private float jumpTimeCounter;
  18.  
  19.     private bool stoppedJumping;
  20.     private bool canDoubleJump;
  21.  
  22.  
  23.     private Rigidbody2D myRigidbody;
  24.  
  25.     public bool grounded;
  26.     public LayerMask whatIsGround;
  27.     public Transform groundCheck;
  28.     public float groundCheckRadius;
  29.  
  30.     private Animator myAnimator;
  31.  
  32.     public GameManager theGameManager;
  33.  
  34.     public AudioSource jumpSound;
  35.     public AudioSource deathSound;
  36.  
  37.  
  38.     // Start is called before the first frame update
  39.     void Start()
  40.     {
  41.         myRigidbody = GetComponent<Rigidbody2D>();
  42.         myAnimator = GetComponent<Animator>();
  43.         jumpTimeCounter = jumpTime;
  44.  
  45.         speedMilestoneCount = speedIncreaseMilestone;
  46.         moveSpeedStore = moveSpeed;
  47.         speedMilestoneCountStore = speedMilestoneCount;
  48.         speedIncreaseMilestoneStore = speedIncreaseMilestone;
  49.         stoppedJumping = true;
  50.     }
  51.  
  52.     // Update is called once per frame
  53.     void Update()
  54.     {
  55.         grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
  56.         myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);
  57.  
  58.         if (transform.position.x > speedMilestoneCount)
  59.         {
  60.  
  61.             speedMilestoneCount += speedIncreaseMilestone;
  62.             speedIncreaseMilestone = speedIncreaseMilestone * speedMultiplier;
  63.  
  64.             moveSpeed = moveSpeed * speedMultiplier;
  65.         }
  66.  
  67.         // Prevent jumping when clicking UI elements
  68.         if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
  69.         {
  70.             if (EventSystem.current.IsPointerOverGameObject() && EventSystem.current.currentSelectedGameObject == null)
  71.                 return;
  72.             if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
  73.             {
  74.                 if (EventSystem.current.IsPointerOverGameObject(Input.touches[0].fingerId))
  75.                     return;
  76.             }
  77.  
  78.             // Jumping code
  79.             if (grounded)
  80.             {
  81.                 myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
  82.                 stoppedJumping = false;
  83.                 jumpSound.Play();
  84.             }
  85.  
  86.             if (!grounded && canDoubleJump)
  87.             {
  88.                 myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
  89.                 jumpTimeCounter = jumpTime;
  90.                 canDoubleJump = false;
  91.                 stoppedJumping = false;
  92.                 jumpSound.Play();
  93.             }
  94.  
  95.         }
  96.  
  97.         if ((Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0)) && !stoppedJumping)
  98.         {
  99.             if (jumpTimeCounter > 0)
  100.             {
  101.                 myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
  102.                 jumpTimeCounter -= Time.deltaTime;
  103.             }
  104.         }
  105.  
  106.         if (Input.GetKeyUp(KeyCode.Space) || Input.GetMouseButtonUp(0))
  107.         {
  108.             jumpTimeCounter = 0;
  109.             stoppedJumping = true;
  110.         }
  111.  
  112.         if (grounded)
  113.         {
  114.             jumpTimeCounter = jumpTime;
  115.             canDoubleJump = true;
  116.         }
  117.         myAnimator.SetFloat("Speed", myRigidbody.velocity.x);
  118.         myAnimator.SetBool("Grounded", grounded);
  119.     }
  120.  
  121.     void OnCollisionEnter2D(Collision2D other)
  122.     {
  123.         if (other.gameObject.tag == "killbox")
  124.         {
  125.             deathSound.Play();
  126.             theGameManager.RestartGame();
  127.             moveSpeed = moveSpeedStore;
  128.             speedMilestoneCount = speedMilestoneCountStore;
  129.             speedIncreaseMilestone = speedIncreaseMilestoneStore;
  130.         }
  131.     }
  132. }
  133.  
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement