我目前的问题是,无论我将 moveSpeed 设置为多少,播放器都非常慢,播放器不会跳跃,播放器在对角线移动时移动很快。脚本如下所示。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 10f;
public float jumpForce = 10f;
public float groundCheckRadius = 0.2f;
public LayerMask groundLayer;
private Rigidbody rb;
private bool isGrounded;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float horizontalInput = Input.GetAxisRaw("Horizontal"); //Get horizontal input
float verticalInput = Input.GetAxisRaw("Vertical"); //Get vertical input
Vector3 moveDirection = (transform.right * horizontalInput + transform.forward * verticalInput) * moveSpeed;
rb.MovePosition(transform.position + moveDirection * Time.deltaTime); //Move the player using Rigidbody's MovePosition method
isGrounded = Physics.CheckSphere(transform.position, groundCheckRadius, groundLayer);
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
}
我试过将 Update 更改为 Fixed update,并将 Time.deltaTime 更改为 Time.fixedDeltaTime(无论 moveSpeed 设置为多少,导致玩家移动得非常快
我试过将 Input.GetAxisRaw 更改为 Input.GetAxis,导致移动过于流畅,但我会追求响应速度
我试过删除 Time.deltaTime,导致断断续续和玩家穿过墙壁。
我试过改变跳跃的方法,当按下空格键并且玩家被接地时,将 rb.velocity.y 更改为 jumpForce。
检查 Inspector 选项卡中的 Unity 脚本。通常,您在检查器选项卡中设置的变量值会否决您在脚本中设置的值。