Guest User

Metroidvania Prototype #1

a guest
Jan 9th, 2025
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7.     [Header("Horizontal Movement Setting")]
  8.     [SerializeField] private float walkSpeed = 1;
  9.  
  10.     [Header("Ground Check Setting")]
  11.     [SerializeField] private Transform groundCheckPoint;
  12.     [SerializeField] private float groundCheckY = 0.2f;
  13.     [SerializeField] private float groundCheckX = 0.5f;
  14.     [SerializeField] private LayerMask whatIsGround;
  15.     [SerializeField] private float jumpForce = 45;
  16.  
  17.     private float xAxis;
  18.     private Rigidbody2D rb;
  19.     private bool isGrounded;
  20.     private Animator anim;
  21.  
  22.     // Start is called once before the first execution of Update after the MonoBehaviour is created
  23.     void Start()
  24.     {
  25.         rb = GetComponent<Rigidbody2D>();
  26.         anim = GetComponent<Animator>();
  27.     }
  28.  
  29.     // Update is called once per frame
  30.     void Update()
  31.     {
  32.         GetInputs();
  33.         Move();
  34.         Jump();
  35.         UpdateAnimations();
  36.     }
  37.  
  38.     void GetInputs()
  39.     {
  40.         xAxis = Input.GetAxisRaw("Horizontal");
  41.     }
  42.  
  43.     private void Move()
  44.     {
  45.         rb.linearVelocity = new Vector2(walkSpeed * xAxis, rb.linearVelocity.y);
  46.     }
  47.  
  48.     private void Jump()
  49.     {
  50.         if (Input.GetButtonUp("Jump") && rb.linearVelocity.y > 0)
  51.         {
  52.             rb.linearVelocity = new Vector2(rb.linearVelocity.x, 0);
  53.         }
  54.  
  55.         if (Input.GetButtonDown("Jump") && Grounded())
  56.         {
  57.             Debug.Log("Jump button pressed and player is grounded");
  58.             rb.linearVelocity = new Vector3(rb.linearVelocity.x, jumpForce);
  59.         }
  60.     }
  61.  
  62.     private void UpdateAnimations()
  63.     {
  64.         anim.SetBool("Walking", rb.linearVelocity.x != 0 && Grounded());
  65.         anim.SetBool("Jumping", !Grounded());
  66.     }
  67.  
  68.     public bool Grounded()
  69.     {
  70.         bool groundCheck1 = Physics2D.Raycast(groundCheckPoint.position, Vector2.down, groundCheckY, whatIsGround);
  71.         bool groundCheck2 = Physics2D.Raycast(groundCheckPoint.position + new Vector3(groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround);
  72.         bool groundCheck3 = Physics2D.Raycast(groundCheckPoint.position + new Vector3(-groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround);
  73.  
  74.         return groundCheck1 || groundCheck2 || groundCheck3;
  75.     }
  76. }
  77.  
Tags: metroidvania
Advertisement
Add Comment
Please, Sign In to add comment