我见过有人说,您应该在走平台之后或刚接触地板之前设置一个延迟计时器,因为这会使您的游戏感觉好多了!这是我之前尝试过的代码-
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
//Variables
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
// is the controller on the ground?
if (controller.isGrounded) {
//Feed moveDirection with input.
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
//Multiply it by speed.
moveDirection *= speed;
//Jumping
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
//Applying gravity to the controller
moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
controller.Move(moveDirection * Time.deltaTime);
}
}
如果您想让播放器在触地之前就跳起来,请使用自动倒数的计时器。对其进行更改,以便在按下跳转按钮时不会跳转,而是重置计时器。这是一个示例:
public float jumpRememberTime = 0.2f;
private float jumpRememberTimer;
private void Update()
{
jumpRememberTimer -= Time.deltaTime;
if (Input.GetButtonDown ("Jump"))
jumpRememberTimer = jumpRememberTime;
if (jumpRememberTimer > 0f && controller.isGrounded)
{
moveDirection.y = jumpSpeed; // jump
}
}
为了允许玩家离开平台后立即跳起来,您还需要一个计时器。这称为土狼时间。您使用计时器,并在播放器离开平台后将其倒计时。这是实现此之后的代码。
public float jumpRememberTime = 0.2f;
private float jumpRememberTimer;
public float groundedRememberTime = 0.2f;
private float groundedRememberTimer = 0f;
private void Update()
{
jumpRememberTimer -= Time.deltaTime;
if (Input.GetButtonDown ("Jump"))
jumpRememberTimer = jumpRememberTime;
if (isGrounded)
groundedRememberTimer = groundedRememberTime;
else
groundedRememberTimer -= Time.deltaTime;
if (jumpRememberTimer > 0f && groundedRememberTimer > 0f)
{
moveDirection.y = jumpSpeed; // jump
}
}