Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 跳跃未按预期工作_C#_Unity3d - Fatal编程技术网

C# 跳跃未按预期工作

C# 跳跃未按预期工作,c#,unity3d,C#,Unity3d,你好,我想知道为什么我的跳跃不一致。我已经研究了很多StackOverflow问题,但仍然找不到解决方案。如果有人能帮上忙,那就太棒了D using UnityEngine; using System.Collections; public class BallMovement : MonoBehaviour { public float speed; private Rigidbody rb; void Start () { rb = GetComponent<Rigid

你好,我想知道为什么我的跳跃不一致。我已经研究了很多StackOverflow问题,但仍然找不到解决方案。如果有人能帮上忙,那就太棒了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);
    }
}

}
使用UnityEngine;
使用系统集合;
公共类棒球运动:单一行为{
公众浮标速度;
私人刚体;
无效开始()
{
rb=GetComponent();
}
无效固定更新()
{
Camera Main Camera=GameObject.FindGameObjectWithTag(“8BallCamera”).GetComponent()作为摄像头;
float moveHorizontal=Input.GetAxisRaw(“水平”);
float moveVertical=Input.GetAxisRaw(“垂直”);
Vector3移动=mainCamera.transform.forward*moveVertical*30;
rb.AddForce(移动*速度);
if(Input.GetKeyDown(“空格”)){
rb.附加力(0,2f,0,力模式脉冲);
}
}
}

提问时,尽量详细说明问题的事实,因为“未按预期工作”和“跳跃不一致”等短语相当主观,根据阅读者的不同,其含义可能有所不同:)

我在我的机器上试过这个代码,发现有时候按空格键并不能启动跳转。似乎没有出现其他问题(尽管您可能希望在稍后的跳转中进行冷却)

问题在于跳转代码位于FixedUpdate()FixedUpdate()似乎在Update()之前运行,但并不总是调用它。这就是为什么空间输入有时被忽略的原因

将其放在Update()中将解决此问题

using UnityEngine;
using System.Collections;

public class BallMovement : MonoBehaviour
{

    public float speed;
    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        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);
        }
    }
}
使用UnityEngine;
使用系统集合;
公共类棒球运动:单一行为
{
公众浮标速度;
私人刚体;
void Start()
{
rb=GetComponent();
}
私有void更新()
{
Camera Main Camera=GameObject.FindGameObjectWithTag(“8BallCamera”).GetComponent()作为摄像头;
float moveHorizontal=Input.GetAxisRaw(“水平”);
float moveVertical=Input.GetAxisRaw(“垂直”);
Vector3移动=mainCamera.transform.forward*moveVertical*30;
rb.AddForce(移动*速度);
if(Input.GetKeyDown(“空格”))
{
rb.附加力(0,2f,0,力模式脉冲);
}
}
}

希望这有帮助

提问时,尽量详细说明问题的事实,因为“没有按预期工作”和“跳跃不一致”之类的短语相当主观,根据阅读者的不同,可能会有不同的含义:)

我在我的机器上试过这个代码,发现有时候按空格键并不能启动跳转。似乎没有出现其他问题(尽管您可能希望在稍后的跳转中进行冷却)

问题在于跳转代码位于FixedUpdate()FixedUpdate()似乎在Update()之前运行,但并不总是调用它。这就是为什么空间输入有时被忽略的原因

将其放在Update()中将解决此问题

using UnityEngine;
using System.Collections;

public class BallMovement : MonoBehaviour
{

    public float speed;
    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        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);
        }
    }
}
使用UnityEngine;
使用系统集合;
公共类棒球运动:单一行为
{
公众浮标速度;
私人刚体;
void Start()
{
rb=GetComponent();
}
私有void更新()
{
Camera Main Camera=GameObject.FindGameObjectWithTag(“8BallCamera”).GetComponent()作为摄像头;
float moveHorizontal=Input.GetAxisRaw(“水平”);
float moveVertical=Input.GetAxisRaw(“垂直”);
Vector3移动=mainCamera.transform.forward*moveVertical*30;
rb.AddForce(移动*速度);
if(Input.GetKeyDown(“空格”))
{
rb.附加力(0,2f,0,力模式脉冲);
}
}
}

希望这有帮助

请不要引用我的话,但我认为输入是在更新中读取的,而不是在FixeUpdate中读取的,因此,根据您的帧速率,可能会使跳转在每个输入中发生多次,如果您的帧速率不同,则所有输入都不会发生跳转,或者两者混合使用。如果您在更新中阅读了您的输入,它应该可以解决您的问题。感谢您的评论。我如何准确地读取输入而不是我的方式?可能重复我所说的@derHugo,我已经尝试了其他解决方案。这是一个不同的问题。有人建议我从Update()移动到FixedUpdate(),但解决方案并非如此。它添加了一个冷却计时器来修复它。:)请不要引用我的话,但我认为输入是在更新中读取的,而不是在FixeUpdate中读取的,因此,根据您的帧速率,可能会使跳转在每个输入中发生多次,如果您的帧速率不同,则所有输入都不会发生跳转,或者两者混合使用。如果您在更新中阅读了您的输入,它应该可以解决您的问题。感谢您的评论。我如何准确地读取输入而不是我的方式?可能重复我所说的@derHugo,我已经尝试了其他解决方案。这是一个不同的问题。有人建议我从Update()移动到FixedUpdate(),但解决方案并非如此。它添加了一个冷却计时器来修复它。:)你建议如何做一个冷却?我试了几秒钟(1);但它不起作用:(声明一个具有所需冷却时间(比如1.5f)的浮点变量。声明另一个浮点变量,比如“CooldownFinishedTime”。在if语句代码中,将CooldownFinishedTime设置为Time.Time(这是自游戏开始以来的秒数)+冷却时间。对于if语句条件,还添加了“&&Time.Time>冷却完成时间"。这意味着你只能在按下空格键并且冷却结束后才能跳转。太棒了!成功了!有没有关于跳转的建议,这样就不会那么快了?再次感谢!!@BenPalladino你需要按deltaTime进行
*
。@penleychan这会让他跳得慢吗?如果是的话,我该把它放在哪里?你建议怎么做一个冷却道指n?我尝试了新的WaitForSeconds(1);但它成功了