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")] public bool isGrounded; private Vector3 rawInputMovement; private void FixedUpdate() { Debug.Log(isGrounded); if (canWalk || canJump) { rb.AddForce(new Vector3(rawInputMovement.x * movementSpeed, rawInputMovement.y, rawInputMovement.z * movementSpeed), ForceMode.Impulse); } RaycastHit hit; if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, 1.5f)) { Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.down) * hit.distance, Color.yellow); isGrounded = true; } else { rawInputMovement = new Vector3(rawInputMovement.x, 0, rawInputMovement.z); isGrounded = false; } } 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) { Vector2 inputMovement = value.ReadValue(); rawInputMovement = new Vector3(rawInputMovement.x, jumpPower, rawInputMovement.z); } } }