Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- public float moveSpeed; // Set the moveSpeed variable
- private float moveVelocity; // Set the moveVelocity variable
- public float jumpHeight; // Set the jumpHeight variable
- public Transform groundCheck; // Set the groundCheck variable
- public float groundCheckRadius; // Set the groundCheckRadius variable
- public LayerMask whatIsGround; // Set the whatIsGround variable
- private bool grounded; // Set the grounded variable
- private bool doubleJumped; // Set the doubleJumped variable
- public AudioClip jumpSound; // Set the jumpSound variable
- private Animator anim; // Set the Animator variable
- private Rigidbody2D rb; // Set the Rigidbody2D variable
- public Transform firePoint; // Set the firePoint variable
- public GameObject bullet; // Set the bullet variable
- public float bulletDelay;// Set the bulletDelay variable
- private float bulletDelayCounter; // Set the bulletDelayCounter to the bulletDelay
- public float knockback;// Set the knockback variable
- public float knockbackLength;
- public float knockbackCount;// Set the knockbackCount to the knockbackLength
- public bool knockFromRight;// Set the knockFromRight to true if the player's scale is less than 0
- public bool onLadder; // Set the onLadder variable
- public float climbSpeed; // Set the climbSpeed variable
- private float climbVelocity; // Set the climbVelocity variable
- private float gravityStore; // Set the climbMultiplier variable
- void Start()
- {
- anim = GetComponent<Animator>(); // Assign the Animator component
- rb = GetComponent<Rigidbody2D>(); // Assign the Rigidbody2D component
- // Set the gravity multiplier to the gravityStore variable
- gravityStore = rb.gravityScale; // Store the gravity scale
- }
- void FixedUpdate()
- {
- grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
- anim.SetBool("Grounded", grounded); // Update the "Grounded" parameter in the animator
- }
- void Update()
- {
- //jump code and double jumps
- if (grounded)
- {
- doubleJumped = false;
- }
- if (Input.GetButton("Jump") && grounded)
- {
- Jump();
- }
- if (Input.GetButtonDown("Jump") && !doubleJumped && !grounded)
- {
- Jump();
- doubleJumped = true;
- }
- //end of jump code and double jumps
- // moveVelocity = 0f;
- //player movewment better with controllers
- moveVelocity = moveSpeed * Input.GetAxisRaw("Horizontal"); // Update the moveVelocity variable
- // player movement code keyboard during build .01
- /* if (Input.GetKey(KeyCode.D))
- {
- moveVelocity = moveSpeed;
- }
- if (Input.GetKey(KeyCode.A))
- {
- moveVelocity = -moveSpeed;
- }*/
- //knockback code
- if (knockbackCount <= 0)// Check if the player is not being knocked back
- {
- rb.velocity = new Vector2(moveVelocity, rb.velocity.y); // Update the player's velocity
- }
- else
- {
- Vector2 knockbackDirection = knockFromRight ? Vector2.right : Vector2.left; // Determine knockback direction
- rb.velocity = knockbackDirection * knockback; // Apply knockback velocity
- knockbackCount -= Time.deltaTime; // Update the knockbackCount
- }
- // End of knockback code
- anim.SetFloat("Speed", Mathf.Abs(rb.velocity.x)); // Update the "Speed" parameter in the animator
- if (rb.velocity.x > 0) // Check if the player is moving right
- transform.localScale = new Vector3(1f, 1f, 1f); // Set the player's scale to 1
- else if (rb.velocity.x < 0) // Check if the player is moving left
- transform.localScale = new Vector3(-1f, 1f, 1f);
- if (Input.GetButtonDown("Fire1")) // Check if the player pressed the Return key
- {
- Instantiate(bullet, firePoint.position, firePoint.rotation); // Instantiate the bullet at the firePoint position and rotation
- bulletDelayCounter = bulletDelay; // Set the bulletDelayCounter to the bulletDelay
- }
- if (Input.GetButton("Fire1")) // Check if the player is holding the Return key
- {
- bulletDelayCounter -= Time.deltaTime; // Update the bulletDelayCounter
- if (bulletDelayCounter <= 0) // Check if the bulletDelayCounter is less than or equal to 0
- {
- bulletDelayCounter = bulletDelay; // Set the bulletDelayCounter to the bulletDelay
- Instantiate(bullet, firePoint.position, firePoint.rotation); // Instantiate the bullet at the firePoint position and rotation
- }
- }
- //sword animation begins
- if (anim.GetBool("Sword"))// Check if the player is using the sword
- anim.SetBool("Sword", false);// Set the "Sword" parameter in the animator to false
- //sword controlls begin
- if (Input.GetButtonDown("Fire2"))// Check if the player pressed the "Fire2" button l on keyboard
- {
- anim.SetBool("Sword", true);// Set the "Sword" parameter in the animator to true
- }
- //sword controlls end
- //ladder controlls begin didnt work
- /* origional code
- if(onLadder)// Check if the player is on a ladder
- {
- rb.gravityScale = 0f;// Set the gravity scale to 0
- climbVelocity = climbSpeed * Input.GetAxisRaw("Vertical"); // Update the climbVelocity variable
- rb.velocity = new Vector2(rb.velocity.x, climbVelocity); // Update the player's velocity
- anim.SetBool("Climbing", true);// Set the "Climbing" parameter in the animator to true
- }
- if (!onLadder)// Check if the player is not on a ladder
- {
- rb.gravityScale = gravityStore;// Set the gravity scale to the gravityStore variable
- anim.SetBool("Climbing", false);// Set the "Climbing" parameter in the animator to false added by me
- }
- //ladder controlls end*/
- // Ladder controls begin
- if (onLadder) {
- rb.gravityScale = 0f; // Neutralize gravity while on the ladder
- climbVelocity = climbSpeed * Input.GetAxisRaw("Vertical"); // Calculate vertical movement speed
- // Only set Climbing to true if there is vertical movement
- bool isClimbing = Mathf.Abs(Input.GetAxisRaw("Vertical")) > 0; // Check if there's significant vertical input
- anim.SetBool("Climbing", isClimbing);
- if (isClimbing) {
- rb.velocity = new Vector2(rb.velocity.x, climbVelocity); // Apply vertical movement
- } else {
- rb.velocity = new Vector2(rb.velocity.x, 0); // Stop vertical movement if not actively climbing
- }
- } else {
- rb.gravityScale = gravityStore; // Restore original gravity when not on the ladder
- anim.SetBool("Climbing", false); // Ensure Climbing is set to false when off the ladder
- }
- // Ladder controls end plays jump animation between frames and when idle
- /*// ladder controls begin
- if (onLadder) {
- rb.gravityScale = 0f;
- climbVelocity = climbSpeed * Input.GetAxisRaw("Vertical");
- bool isClimbing = Mathf.Abs(Input.GetAxisRaw("Vertical")) > 0;
- anim.SetBool("Climbing", isClimbing); // Active climbing
- anim.SetBool("ClimbIdle", !isClimbing && onLadder); // Idle on ladder
- if (isClimbing) {
- rb.velocity = new Vector2(rb.velocity.x, climbVelocity);
- } else {
- // Stop vertical movement but maintain position on ladder
- rb.velocity = new Vector2(rb.velocity.x, 0);
- }
- } else {
- rb.gravityScale = gravityStore;
- anim.SetBool("Climbing", false);
- anim.SetBool("ClimbIdle", false);
- }*/
- }
- //sword animation code ends
- public void Jump() // Define the Jump function
- {
- Debug.Log("Jumping!"); // Add this line to check if Jump is being called
- rb.velocity = new Vector2(rb.velocity.x, jumpHeight); // Update the player's velocity
- GetComponent<AudioSource>().clip = jumpSound; // Set the audio clip to the jump sound
- GetComponent<AudioSource>().Play(); // Play the jump sound
- anim.SetTrigger("Jump"); // Trigger the jump animation
- }
- //moving platform code
- void OnCollisionEnter2D(Collision2D other) // Define the onCollisionEnter2D function
- {
- if (other.transform.tag == "MovingPlatform") // Check if the other object has the "Ground" tag
- {
- transform.SetParent(other.transform); // Set the parent of the player to the other object
- }
- }
- void OnCollisionExit2D(Collision2D other) // Define the onCollisionExit2D function
- {
- if (other.transform.tag == "MovingPlatform") // Check if the other object has the "Ground" tag
- {
- transform.SetParent(null); // Set the parent of the player to null
- }
- }
- //moving platform code
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement