【问题标题】:Player Movement In Unity With Smoother Jumping球员运动统一,跳跃更流畅
【发布时间】:2020-06-08 09:08:32
【问题描述】:

我看到有人说您应该在走完平台后或触地之前设置一个延迟计时器,因为这会让您的游戏感觉更好!这是我之前尝试过的代码 -

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

【问题讨论】:

  • 你的问题是什么?
  • 如何让运动感觉更流畅!

标签: c# unity3d


【解决方案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
    }
}

【讨论】:

    猜你喜欢
    • 2014-01-16
    • 2023-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    相关资源
    最近更新 更多