Advertisement
johnnygoodguy2000

PlayerController.cs

Apr 25th, 2024 (edited)
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.36 KB | Gaming | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlayerController : MonoBehaviour
  4. {
  5.     public float moveSpeed; // Set the moveSpeed variable
  6.     private float moveVelocity; // Set the moveVelocity variable
  7.     public float jumpHeight; // Set the jumpHeight variable
  8.  
  9.     public Transform groundCheck;        // Set the groundCheck variable
  10.     public float groundCheckRadius;     // Set the groundCheckRadius variable
  11.     public LayerMask whatIsGround;  // Set the whatIsGround variable
  12.  
  13.     private bool grounded;  // Set the grounded variable
  14.     private bool doubleJumped; // Set the doubleJumped variable
  15.     public AudioClip jumpSound; // Set the jumpSound variable
  16.  
  17.     private Animator anim; // Set the Animator variable
  18.     private Rigidbody2D rb;   // Set the Rigidbody2D variable
  19.  
  20.     public Transform firePoint; // Set the firePoint variable
  21.     public GameObject bullet; // Set the bullet variable
  22.  
  23.     public float bulletDelay;// Set the bulletDelay variable
  24.     private float bulletDelayCounter; // Set the bulletDelayCounter to the bulletDelay
  25.  
  26.     public float knockback;// Set the knockback variable
  27.     public float knockbackLength;
  28.     public float knockbackCount;// Set the knockbackCount to the knockbackLength
  29.  
  30.     public bool knockFromRight;// Set the knockFromRight to true if the player's scale is less than 0
  31.  
  32.     public bool onLadder; // Set the onLadder variable
  33.  
  34.     public float climbSpeed; // Set the climbSpeed variable
  35.  
  36.     private float climbVelocity; // Set the climbVelocity variable
  37.     private float gravityStore; // Set the climbMultiplier variable
  38.  
  39.     void Start()
  40.     {
  41.         anim = GetComponent<Animator>(); // Assign the Animator component
  42.         rb = GetComponent<Rigidbody2D>(); // Assign the Rigidbody2D component
  43.  
  44.         // Set the gravity multiplier to the gravityStore variable
  45.  
  46.         gravityStore = rb.gravityScale; // Store the gravity scale
  47.     }
  48.  
  49.     void FixedUpdate()
  50.     {
  51.         grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
  52.         anim.SetBool("Grounded", grounded); // Update the "Grounded" parameter in the animator
  53.     }
  54.  
  55.     void Update()
  56.     {
  57.         //jump code and double jumps
  58.         if (grounded)
  59.         {
  60.             doubleJumped = false;
  61.         }
  62.  
  63.         if (Input.GetButton("Jump") && grounded)
  64.         {
  65.             Jump();
  66.         }
  67.  
  68.         if (Input.GetButtonDown("Jump") && !doubleJumped && !grounded)
  69.         {
  70.             Jump();
  71.             doubleJumped = true;
  72.         }
  73.         //end of jump code and double jumps
  74.  
  75.        // moveVelocity = 0f;
  76. //player movewment better with controllers
  77. moveVelocity = moveSpeed * Input.GetAxisRaw("Horizontal"); // Update the moveVelocity variable
  78.  
  79.  
  80.  
  81.         // player movement code keyboard during build .01
  82.  
  83.        /* if (Input.GetKey(KeyCode.D))
  84.         {
  85.             moveVelocity = moveSpeed;
  86.         }
  87.  
  88.         if (Input.GetKey(KeyCode.A))
  89.         {
  90.             moveVelocity = -moveSpeed;
  91.         }*/
  92.  
  93.         //knockback code
  94.  
  95.         if (knockbackCount <= 0)// Check if the player is not being knocked back
  96.         {
  97.             rb.velocity = new Vector2(moveVelocity, rb.velocity.y); // Update the player's velocity
  98.         }
  99.         else
  100.         {  
  101.             Vector2 knockbackDirection = knockFromRight ? Vector2.right : Vector2.left; // Determine knockback direction
  102.             rb.velocity = knockbackDirection * knockback; // Apply knockback velocity
  103.             knockbackCount -= Time.deltaTime; // Update the knockbackCount
  104.         }
  105.        
  106.          // End of knockback code
  107.  
  108.          
  109.  
  110.         anim.SetFloat("Speed", Mathf.Abs(rb.velocity.x));     // Update the "Speed" parameter in the animator
  111.  
  112.         if (rb.velocity.x > 0)                               // Check if the player is moving right
  113.             transform.localScale = new Vector3(1f, 1f, 1f); // Set the player's scale to 1
  114.         else if (rb.velocity.x < 0)                          // Check if the player is moving left
  115.             transform.localScale = new Vector3(-1f, 1f, 1f);
  116.  
  117.         if (Input.GetButtonDown("Fire1"))              // Check if the player pressed the Return key
  118.         {
  119.             Instantiate(bullet, firePoint.position, firePoint.rotation); // Instantiate the bullet at the firePoint position and rotation
  120.             bulletDelayCounter = bulletDelay; // Set the bulletDelayCounter to the bulletDelay
  121.         }
  122.  
  123.         if (Input.GetButton("Fire1"))               // Check if the player is holding the Return key
  124.         {
  125.             bulletDelayCounter -= Time.deltaTime; // Update the bulletDelayCounter
  126.  
  127.             if (bulletDelayCounter <= 0) // Check if the bulletDelayCounter is less than or equal to 0
  128.             {
  129.                 bulletDelayCounter = bulletDelay;   // Set the bulletDelayCounter to the bulletDelay
  130.                 Instantiate(bullet, firePoint.position, firePoint.rotation);    // Instantiate the bullet at the firePoint position and rotation
  131.             }
  132.         }
  133.        
  134.                             //sword animation begins
  135.         if (anim.GetBool("Sword"))// Check if the player is using the sword
  136.         anim.SetBool("Sword", false);// Set the "Sword" parameter in the animator to false
  137.        
  138.        
  139.            
  140.  
  141.             //sword controlls begin
  142.        
  143.         if (Input.GetButtonDown("Fire2"))// Check if the player pressed the "Fire2" button l on keyboard
  144.         {
  145.             anim.SetBool("Sword", true);// Set the "Sword" parameter in the animator to true
  146.         }
  147. //sword controlls end
  148.  
  149.         //ladder controlls begin didnt work
  150.  
  151.         /* origional code
  152.         if(onLadder)// Check if the player is on a ladder
  153.         {
  154.  
  155.            
  156.           rb.gravityScale = 0f;// Set the gravity scale to 0
  157.           climbVelocity = climbSpeed * Input.GetAxisRaw("Vertical"); // Update the climbVelocity variable
  158.           rb.velocity = new Vector2(rb.velocity.x, climbVelocity); // Update the player's velocity
  159.           anim.SetBool("Climbing", true);// Set the "Climbing" parameter in the animator to true
  160.  
  161.  
  162.          
  163.  
  164.  
  165.         }
  166.  
  167.         if (!onLadder)// Check if the player is not on a ladder
  168.         {
  169.            rb.gravityScale = gravityStore;// Set the gravity scale to the gravityStore variable
  170.            anim.SetBool("Climbing", false);// Set the "Climbing" parameter in the animator to false added by me
  171.            
  172.         }
  173.         //ladder controlls end*/
  174.  
  175.         // Ladder controls begin
  176. if (onLadder) {
  177.     rb.gravityScale = 0f;  // Neutralize gravity while on the ladder
  178.     climbVelocity = climbSpeed * Input.GetAxisRaw("Vertical");  // Calculate vertical movement speed
  179.  
  180.     // Only set Climbing to true if there is vertical movement
  181.     bool isClimbing = Mathf.Abs(Input.GetAxisRaw("Vertical")) > 0;  // Check if there's significant vertical input
  182.     anim.SetBool("Climbing", isClimbing);
  183.  
  184.     if (isClimbing) {
  185.         rb.velocity = new Vector2(rb.velocity.x, climbVelocity);  // Apply vertical movement
  186.     } else {
  187.         rb.velocity = new Vector2(rb.velocity.x, 0);  // Stop vertical movement if not actively climbing
  188.     }
  189. } else {
  190.     rb.gravityScale = gravityStore;  // Restore original gravity when not on the ladder
  191.     anim.SetBool("Climbing", false);  // Ensure Climbing is set to false when off the ladder
  192. }
  193.         // Ladder controls end plays jump animation between frames and when idle
  194.  
  195.  
  196.         /*// ladder controls begin
  197.         if (onLadder) {
  198.         rb.gravityScale = 0f;
  199.         climbVelocity = climbSpeed * Input.GetAxisRaw("Vertical");
  200.         bool isClimbing = Mathf.Abs(Input.GetAxisRaw("Vertical")) > 0;
  201.  
  202.         anim.SetBool("Climbing", isClimbing);  // Active climbing
  203.         anim.SetBool("ClimbIdle", !isClimbing && onLadder);  // Idle on ladder
  204.  
  205.         if (isClimbing) {
  206.             rb.velocity = new Vector2(rb.velocity.x, climbVelocity);
  207.         } else {
  208.             // Stop vertical movement but maintain position on ladder
  209.             rb.velocity = new Vector2(rb.velocity.x, 0);
  210.         }
  211.     } else {
  212.         rb.gravityScale = gravityStore;
  213.         anim.SetBool("Climbing", false);
  214.         anim.SetBool("ClimbIdle", false);
  215.     }*/
  216.  
  217.  
  218.  
  219.     }
  220.  
  221.     //sword animation code ends
  222.  
  223.     public void Jump()  // Define the Jump function
  224.     {
  225.         Debug.Log("Jumping!");  // Add this line to check if Jump is being called
  226.         rb.velocity = new Vector2(rb.velocity.x, jumpHeight); // Update the player's velocity
  227.         GetComponent<AudioSource>().clip = jumpSound; // Set the audio clip to the jump sound
  228.         GetComponent<AudioSource>().Play(); // Play the jump sound
  229.         anim.SetTrigger("Jump"); // Trigger the jump animation
  230.     }
  231.     //moving platform code
  232.  
  233.     void OnCollisionEnter2D(Collision2D other)  // Define the onCollisionEnter2D function
  234.     {
  235.         if (other.transform.tag == "MovingPlatform")  // Check if the other object has the "Ground" tag
  236.         {
  237.             transform.SetParent(other.transform);  // Set the parent of the player to the other object
  238.         }
  239.     }
  240.  
  241.     void OnCollisionExit2D(Collision2D other)  // Define the onCollisionExit2D function
  242.     {
  243.         if (other.transform.tag == "MovingPlatform")  // Check if the other object has the "Ground" tag
  244.         {
  245.             transform.SetParent(null);  // Set the parent of the player to null
  246.         }
  247.        
  248.     }
  249.     //moving platform code
  250. }
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement