【问题标题】:How do I limit the player from moving around while airborne?如何限制玩家在空中移动时移动?
【发布时间】:2021-04-19 08:33:56
【问题描述】:

我对 Unity 中的 GameDev 非常陌生,需要帮助解决玩家移动问题。我的动作设置方式,让你在半空中转身和移动,这给人一种不切实际的感觉。我想知道一旦你跳向一个方向,我将如何让它到达哪里,直到你回到地面,你才能改变它。

我知道它应该只是一个简单的布尔值来检查玩家是否在空中,我只是不知道如何在我当前的移动脚本中对其进行编码。谢谢。

using System.Collections;

使用 System.Collections.Generic; 使用 UnityEngine;

公共类 PlayerMovement : MonoBehaviour { Vector3 速度;

public float speed = 12f;
public float gravity = -9.81f;
public CharacterController controller;
public float jumpHeight = 3f;

public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;

bool isGrounded;

// Update is called once per frame
void Update()
{
    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

    if(isGrounded && velocity.y < 0)
    {
        velocity.y = -2f;
    }

    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    Vector3 move = transform.right * x + transform.forward * z;

    controller.Move(move * speed * Time.deltaTime);

    if(Input.GetButtonDown("Jump") && isGrounded)
    {
        velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    }

    velocity.y += gravity * Time.deltaTime;

    controller.Move(velocity * Time.deltaTime);
}}

【问题讨论】:

  • 您已经知道您的播放器是否isGrounded,因此当播放器未接地时,您可以忽略播放器输入,因此在输入之前执行 if(isGrounded){return;} 之类的操作。与此相关的一个问题是,您似乎在计算自己的重力?即使刚体重力应该已经影响您的对象,除非您关闭组件上的重力。

标签: c# unity3d


【解决方案1】:

当你不在现场时,你可以通过一个简单的 if 语句来禁用它:

 public float speed = 12f;
 public float gravity = -9.81f;
 public CharacterController controller;
 public float jumpHeight = 3f;

 public Transform groundCheck;
 public float groundDistance = 0.4f;
 public LayerMask groundMask;

 bool isGrounded;

 // Update is called once per frame
 void Update()
 {
      isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

     if(isGrounded && velocity.y < 0)
     {
          velocity.y = -2f;
     }

     float x = Input.GetAxis("Horizontal");
     float z = Input.GetAxis("Vertical");
     Vector3 move = transform.right * x + transform.forward * z;
     if (!isGrounded)
     {
         controller.Move(move * speed * Time.deltaTime);
     }
     if(Input.GetButtonDown("Jump") && isGrounded)
     {
         velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
     }

      velocity.y += gravity * Time.deltaTime;

      controller.Move(velocity * Time.deltaTime);
 }
 }

我所做的是向 controller.Move() 函数添加一个 if 语句。这是没有其余代码的样子:

if (isGrounded)
{
    controller.Move(move * speed * Time.deltaTime);
}

这是我写的。 if 语句说:如果角色在地上,则移动它。这意味着它将在空中停止更新角色的位置。

如果您想要更真实的物理,请将其添加到您的脚本中:

if (isGrounded)
{
    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");
    Vector3 move = transform.right * x + transform.forward * z;
    controller.Move(move * speed * Time.deltaTime);
}
else
{
    velocity.x = Mathf.Sqrt(move.x * var2);
    velocity.z = Mathf.Sqrt(move.z * var2);
}

我复制了您的跳转脚本,并对其稍作更改以复制您可能想要的物理效果。更改 var1 和 var2 的名称。 Var 2 是您在空中时施加的力的大小,您可以随意尝试。

【讨论】:

  • 我应用了代码,但由于某种原因,它只是在我跳跃时停止了我,并且不会继续跳跃。我成功地得到了它停止接受 WASD 输入但以冻结跳跃为代价的部分。我想我需要一种方法来记住我在跳跃之前所面对的方向,以便引擎可以继续跳跃。我只是不知道这是否需要一个新的向量或者什么。尽管如此,还是感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2023-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多