Advertisement
Guest User

Untitled

a guest
Apr 29th, 2025
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1. using UnityEngine.InputSystem;
  2. using UnityEngine;
  3. using Unity.VisualScripting;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7.     public PlayerInput playerControls;
  8.  
  9.     //from input management slides
  10.     InputAction move;
  11.     InputAction jump;
  12.  
  13.     public bool isGrounded;
  14.  
  15.     //from raycast slides
  16.     public float speed;
  17.     public float height;
  18.  
  19.     float thresh;
  20.     float offset;
  21.  
  22.     Rigidbody2D rd;
  23.     public LayerMask layerMask;
  24.     public float raySpawnSpot; //renamed from raySpawnDist
  25.  
  26.  
  27.     private void Awake()
  28.     {
  29.         playerControls = new PlayerInput();
  30.  
  31.         rd = GetComponent<Rigidbody2D>();
  32.  
  33.         //physics offset, whatever that is
  34.         offset = speed * 2.5f;
  35.  
  36.         //thresh math
  37.         thresh = speed / 1000;
  38.     }
  39.  
  40.     private void Update()
  41.     {
  42.         //movement
  43.         if (Input.GetButtonDown("Up") && isGrounded)
  44.         {
  45.             rd.AddForce(Vector2.up * height, ForceMode2D.Impulse);
  46.             isGrounded = false;
  47.         }
  48.         if (Input.GetButtonDown("Right"))
  49.         {
  50.             rd.AddForce(Vector2.right * speed, ForceMode2D.Force);
  51.         }
  52.         if (Input.GetButtonDown("Left"))
  53.         {
  54.             rd.AddForce(Vector2.left * speed, ForceMode2D.Force);
  55.         }
  56.     }
  57.  
  58.     private void FixedUpdate()
  59.     {
  60.         RaycastHit2D hit = Physics2D.Raycast(transform.position - new Vector3(0f, raySpawnSpot, 0f), transform.TransformDirection(Vector2.down), 0.01f, layerMask);
  61.  
  62.         //visual raycast
  63.         Debug.DrawRay(transform.position - new Vector3(0f, raySpawnSpot, 0f), transform.TransformDirection(Vector2.down), Color.red);
  64.  
  65.         //make sure collider exists
  66.         if (hit.collider != null)
  67.         {
  68.             isGrounded = true;
  69.         }
  70.     }
  71.  
  72.     private void OnEnable()
  73.     {
  74.         move = playerControls.Player.Move;
  75.         move.Enable();
  76.  
  77.         jump = playerControls.Player.Jump;
  78.         jump.Enable();
  79.     }
  80.  
  81.     private void OnDisable()
  82.     {
  83.         move.Disable();
  84.         jump.Disable();
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement