using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player : MonoBehaviour { [SerializeField] private Transform groundCheckTransform = null; [SerializeField] private LayerMask playerMask; private bool jumpKeyPressed; private float horizontalInput; private Rigidbody rigidbodyComponent;
// Start is called before the first frame update void Start() { rigidbodyComponent = GetComponent<Rigidbody>(); }
// Update is called once per frame void Update() { horizontalInput = Input.GetAxis("Horizontal");
// Avoid jumping in the air (always colliding with the capsule collider) if(Physics.OverlapSphere(groundCheckTransform.position, 0.1f, playerMask).Length == 0){ return; }
if(Input.GetKeyDown(KeyCode.Space)){ jumpKeyPressed = true; } } // FixedUpdate is called once every physic update (Default 100/s) private void FixedUpdate() { if(jumpKeyPressed){ rigidbodyComponent.AddForce(Vector3.up * 5,ForceMode.VelocityChange); jumpKeyPressed = false; }
rigidbodyComponent.velocity = new Vector3(horizontalInput * 2, rigidbodyComponent.velocity.y, 0); }