Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine.InputSystem;
- using UnityEngine;
- using Unity.VisualScripting;
- public class PlayerMovement : MonoBehaviour
- {
- public PlayerInput playerControls;
- //from input management slides
- InputAction move;
- InputAction jump;
- public bool isGrounded;
- //from raycast slides
- public float speed;
- public float height;
- float thresh;
- float offset;
- Rigidbody2D rd;
- public LayerMask layerMask;
- public float raySpawnSpot; //renamed from raySpawnDist
- private void Awake()
- {
- playerControls = new PlayerInput();
- rd = GetComponent<Rigidbody2D>();
- //physics offset, whatever that is
- offset = speed * 2.5f;
- //thresh math
- thresh = speed / 1000;
- }
- private void Update()
- {
- //movement
- if (Input.GetButtonDown("Up") && isGrounded)
- {
- rd.AddForce(Vector2.up * height, ForceMode2D.Impulse);
- isGrounded = false;
- }
- if (Input.GetButtonDown("Right"))
- {
- rd.AddForce(Vector2.right * speed, ForceMode2D.Force);
- }
- if (Input.GetButtonDown("Left"))
- {
- rd.AddForce(Vector2.left * speed, ForceMode2D.Force);
- }
- }
- private void FixedUpdate()
- {
- RaycastHit2D hit = Physics2D.Raycast(transform.position - new Vector3(0f, raySpawnSpot, 0f), transform.TransformDirection(Vector2.down), 0.01f, layerMask);
- //visual raycast
- Debug.DrawRay(transform.position - new Vector3(0f, raySpawnSpot, 0f), transform.TransformDirection(Vector2.down), Color.red);
- //make sure collider exists
- if (hit.collider != null)
- {
- isGrounded = true;
- }
- }
- private void OnEnable()
- {
- move = playerControls.Player.Move;
- move.Enable();
- jump = playerControls.Player.Jump;
- jump.Enable();
- }
- private void OnDisable()
- {
- move.Disable();
- jump.Disable();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement