#if ENABLE_INPUT_SYSTEM using UnityEngine.InputSystem; #endif using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { public CharacterController controller; public float speed = 12f; public float gravity = -10f; public float jumpHeight = 2f; public Transform groundCheck; public float groundDistance = 0.4f; public LayerMask groundMask; Vector3 velocity; bool isGrounded; #if ENABLE_INPUT_SYSTEM InputAction movement; InputAction jump; void Start() { movement = new InputAction("PlayerMovement", binding: "/leftStick"); movement.AddCompositeBinding("Dpad") .With("Up", "/w") .With("Up", "/upArrow") .With("Down", "/s") .With("Down", "/downArrow") .With("Left", "/a") .With("Left", "/leftArrow") .With("Right", "/d") .With("Right", "/rightArrow"); jump = new InputAction("PlayerJump", binding: "/a"); jump.AddBinding("/space"); movement.Enable(); jump.Enable(); } #endif // Update is called once per frame void Update() { float x; float z; bool jumpPressed = false; #if ENABLE_INPUT_SYSTEM var delta = movement.ReadValue(); x = delta.x; z = delta.y; jumpPressed = Mathf.Approximately(jump.ReadValue(), 1); #else x = Input.GetAxis("Horizontal"); z = Input.GetAxis("Vertical"); jumpPressed = Input.GetButtonDown("Jump"); #endif isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); if (isGrounded && velocity.y < 0) { velocity.y = -2f; } Vector3 move = transform.right * x + transform.forward * z; controller.Move(move * speed * Time.deltaTime); if(jumpPressed && isGrounded) { velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity); } velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); } }