球员运动统一,跳跃流畅

时间:2020-06-08 09:08:32

标签: c# unity3d

我见过有人说您应该在平台行走之后或刚接触地板之前设置一个延迟计时器,因为这会使您的游戏感觉好多了!这是我之前尝试过的代码-

 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);
     }
 }

1 个答案:

答案 0 :(得分:1)

如果要让玩家在触地之前就跳起来,只需使用自动倒数的计时器即可。对其进行更改,以便在按下跳转按钮时不会跳转,而是重置计时器。这是一个示例:

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
    }
}
相关问题