【发布时间】:2018-03-13 00:58:23
【问题描述】:
我有一个 3D 空间中的游戏和刚体组合。给玩家。我使用以下代码实现了一个无法按预期工作的跳转机制:
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public bool Travel;
public Rigidbody rb;
public Transform tf;
public bool Grounded;
public float speed;
int OnRail = 2;
void Update ()
{
float speed = 1f * Time.deltaTime;
if (Input.GetKeyDown("d") && OnRail < 3)
{
tf.Translate(5.4f, 0f, 0f);
OnRail++;
}
else if (Input.GetKeyDown("a") && OnRail > 1)
{
tf.Translate(-5.4f, 0f, 0f);
OnRail--;
}
}
void FixedUpdate ()
{
if (Travel)
{
rb.velocity = new Vector3(0, 0, speed * Time.deltaTime);
}
if (Input.GetKeyDown("space") && Grounded)
{
Grounded = false;
rb.AddForce(new Vector3(0, 500000f * Time.deltaTime, 0));
}
if ((tf.position.y == 2f || tf.position.y == 10f) && rb.velocity.y == 0 && !Grounded)
{
Grounded = true;
}
}
}
玩家应该跳到空中,但只能跳几乎没有距离。 2 和 10 Y 位置是预设的楼层(我真的不需要这 2 个 Y 坐标。除非有人替代了楼层检测)。
【问题讨论】:
-
您可以参考this Unity Answers 帖子来检查您的播放器是如何接地的。
标签: c# unity3d game-physics