【发布时间】:2014-12-30 08:08:48
【问题描述】:
我对 Unity 2D 中的脚本有疑问,因为我的角色会无限跳跃,请您帮帮我(我是 Unity 的菜鸟)。 我尝试了一个带有布尔值但没有结果的东西...... 我在 C# 中的代码是:
using UnityEngine;
using System.Collections;
public class Movements2D : MonoBehaviour {
public float movementSpeed = 5.0f;
private float jumpHeight = 500f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey("left") || Input.GetKey("q"))
{
transform.position += Vector3.left * movementSpeed * Time.deltaTime;
}
if(Input.GetKey("right") || Input.GetKey("d"))
{
transform.position += Vector3.right * movementSpeed * Time.deltaTime;
}
if(Input.GetButtonDown("Jump") || Input.GetKey("z"))
{
Jump();
}
}
void Jump()
{
rigidbody.AddForce (new Vector3 (0, jumpHeight, 0), ForceMode.Force);
}
}
感谢您的帮助,
弗洛。
【问题讨论】:
-
我不确定它是否会永远飞下去?您是否启用了rigidbody.useGravity(或rigidbody2d 重力比例不为零)?
-
请注意目前“jump”和“z”键的行为有所不同。当“jump”被按下时,即使“jump”被按下,Jump() 也会执行一次。按住 z 键时,每帧都会执行 Jump(),直到键被释放。如果您希望两者都有类似的行为,您可以使用 Input.GetKeyDown("z")。
标签: unity3d unityscript