using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class CharacterMovement : MonoBehaviour { [SerializeField] private Rigidbody rb; [Header("Enable switches")] [SerializeField] private bool canWalk; [SerializeField] private bool canJump; [Header("Movement settings")] [SerializeField] private float movementSpeed; [SerializeField] private float jumpPower; [Header("Character properties")] [SerializeField] private float groundDrag; [SerializeField] private float airDrag; [SerializeField] private float playerHeight; public bool isGrounded; public bool isJumping; private Vector3 rawInputMovement; private void FixedUpdate() { if (canWalk || canJump) { //The walk is actually handled here rb.AddForce(new Vector3(rawInputMovement.x * movementSpeed, rawInputMovement.y, rawInputMovement.z * movementSpeed), ForceMode.Impulse); } Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.down) * playerHeight, Color.yellow); //Ground Check RaycastHit hit; if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, playerHeight, LayerMask.GetMask("Floor")) || Physics.Raycast(transform.position, transform.TransformDirection(Vector3.up), out hit, playerHeight, LayerMask.GetMask("Floor")) || Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, playerHeight, LayerMask.GetMask("Floor")) || Physics.Raycast(transform.position, transform.TransformDirection(Vector3.back), out hit, playerHeight, LayerMask.GetMask("Floor")) || Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out hit, playerHeight, LayerMask.GetMask("Floor")) || Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out hit, playerHeight, LayerMask.GetMask("Floor"))) { isGrounded = true; } else { rawInputMovement = new Vector3(rawInputMovement.x, 0, rawInputMovement.z); isGrounded = false; } //Handle Grounded if (isGrounded) { rb.drag = groundDrag; isJumping = false; } else rb.drag= airDrag; } public void Walk(InputAction.CallbackContext value){ if (canWalk) { Vector2 inputMovement = value.ReadValue(); rawInputMovement = new Vector3(inputMovement.x, 0, inputMovement.y); } } public void Jump(InputAction.CallbackContext value) { if(canJump && isGrounded) { isJumping = true; float inputMovement = value.ReadValue(); rb.velocity = new Vector3(rb.velocity.x, jumpPower* inputMovement, rb.velocity.z); //rawInputMovement = new Vector3(rawInputMovement.x, jumpPower, rawInputMovement.z); } } }