您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

89 行
2.3 KiB

#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: "<Gamepad>/leftStick");
movement.AddCompositeBinding("Dpad")
.With("Up", "<Keyboard>/w")
.With("Up", "<Keyboard>/upArrow")
.With("Down", "<Keyboard>/s")
.With("Down", "<Keyboard>/downArrow")
.With("Left", "<Keyboard>/a")
.With("Left", "<Keyboard>/leftArrow")
.With("Right", "<Keyboard>/d")
.With("Right", "<Keyboard>/rightArrow");
jump = new InputAction("PlayerJump", binding: "<Gamepad>/a");
jump.AddBinding("<Keyboard>/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<Vector2>();
x = delta.x;
z = delta.y;
jumpPressed = Mathf.Approximately(jump.ReadValue<float>(), 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);
}
}