跳跃未按预期工作 Unity 3D

你好,我想找出为什么我的跳跃不一致。我查看了许多问题,但仍然找不到解决方案。如果有人能提供帮助那就太棒了!:D


using UnityEngine;

using System.Collections;


public class BallMovement : MonoBehaviour {


public float speed;


private Rigidbody rb;


void Start ()

{

    rb = GetComponent<Rigidbody>();

}


void FixedUpdate ()

{

    Camera mainCamera = GameObject.FindGameObjectWithTag("8BallCamera").GetComponent<Camera>() as Camera;

    float moveHorizontal = Input.GetAxisRaw ("Horizontal");

    float moveVertical = Input.GetAxisRaw("Vertical");

    Vector3 movement = mainCamera.transform.forward * moveVertical * 30;

    rb.AddForce (movement * speed);


    if (Input.GetKeyDown("space")) {

        rb.AddForce(0,2f,0, ForceMode.Impulse);

    }

}


}


斯蒂芬大帝
浏览 114回答 1
1回答

呼如林

提出问题时,请尝试更详细地了解问题的事实,因为诸如“未按预期工作”和“跳跃不一致”之类的短语非常主观,并且可能意味着不同的内容,具体取决于阅读它的人:)我在我的机器上尝试了代码,发现有时按空格键不会启动跳转。似乎没有出现其他问题(尽管您可能希望稍后为跳跃设置冷却时间)。问题在于您的跳转代码位于FixUpdate()中。FixUpdate()似乎在Update()之前运行,但并不总是被调用。这就是为什么空格输入有时会被忽视的原因。将其放在Update()中将解决该问题。using UnityEngine;using System.Collections;public class BallMovement : MonoBehaviour{&nbsp; &nbsp; public float speed;&nbsp; &nbsp; private Rigidbody rb;&nbsp; &nbsp; void Start()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; rb = GetComponent<Rigidbody>();&nbsp; &nbsp; }&nbsp; &nbsp; private void Update()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Camera mainCamera = GameObject.FindGameObjectWithTag("8BallCamera").GetComponent<Camera>() as Camera;&nbsp; &nbsp; &nbsp; &nbsp; float moveHorizontal = Input.GetAxisRaw("Horizontal");&nbsp; &nbsp; &nbsp; &nbsp; float moveVertical = Input.GetAxisRaw("Vertical");&nbsp; &nbsp; &nbsp; &nbsp; Vector3 movement = mainCamera.transform.forward * moveVertical * 30;&nbsp; &nbsp; &nbsp; &nbsp; rb.AddForce(movement * speed);&nbsp; &nbsp; &nbsp; &nbsp; if (Input.GetKeyDown("space"))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rb.AddForce(0, 2f, 0, ForceMode.Impulse);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}希望这可以帮助!
打开App,查看更多内容
随时随地看视频慕课网APP