我有一个要跳转的脚本,但它的跳转取决于 JumpFactor 常量的值。我希望使用 JumpToPositionX(浮动目标)方法进行跳转。我想我需要根据 JumpToPositionX 方法中目标的值以某种方式计算 JumpFactor。我在维基百科上找到了一篇文章,但我什至不知道从哪里开始。我还没有找到有关如何使用 CharacterController 执行此操作的示例。
public class ExampleClass : MonoBehaviour
{
const float Speed = 6f;
const float JumpFactor = 14f;
const float Gravity = 20f;
private Vector3 moveDirection = Vector3.zero;
CharacterController controller;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
if (controller.isGrounded)
{
moveDirection = new Vector3(1f, 0f, 0f); // Move X Direction
moveDirection *= Speed;
if (Input.GetButton("Jump"))
moveDirection.y = JumpFactor;
}
moveDirection.y -= Gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
} …Run Code Online (Sandbox Code Playgroud)