[英]Player Movement In Unity With Smoother Jumping
我看到有人说你应该在走完一个平台之后或者在你触地之前设置一个延迟计时器,因为这会让你的游戏感觉好多了! 这是我之前尝试过的代码 -
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.