0

嗨,我想知道是否有人可以帮助我修复练习代码(我在制作真实的东西之前制作练习代码,因为它只是我滚动的方式)它基本上是一个需要用户单击屏幕才能使其不出现的对象很像飞扬的小鸟一样触地但是虽然我已经正确地对精灵应用了重力我无法修复速度部分(我编码是每次用户用鼠标单击或点击空格键时对象会像飞扬的小鸟一样向上移动)

using UnityEngine;
using System.Collections;

public class BirdMovment : MonoBehaviour {

    Vector3 Velocity = Vector3.zero;
    public Vector3 gravity;
    public Vector3 flapVelocity;
    public float maxSpeed = 5f; 

    bool didFlap = false;

    // Use this for initialization
    void Start () {

    }

    void update (){
        if (Input.GetKeyDown (KeyCode.Mouse0)) 
        {
            didFlap = true; 
        }
    }

    // Update is called once per frame
    void FixedUpdate () {
        Velocity += gravity* Time.deltaTime;

        if (didFlap) {
            didFlap = false;
            Velocity += flapVelocity;

        }
        Velocity = Vector3.ClampMagnitude (Velocity, maxSpeed);

        transform.position += Velocity * Time.deltaTime;
    }
}

你能解决这个错误吗,因为每次我为精灵广告设置统一的速度时,运行程序精灵一直在下降,无论我点击多少或点击空格键,即使我增加精灵也不会停止下降速度

4

1 回答 1

1

首先,正确的更新函数是大写的 U,所以Update()不是update(). 然后,由于您没有对物理做任何事情,因此您可以做所有事情Update而根本不使用FixedUpdate。因此,您可以删除didFlap变量并Velocity直接添加到if (Input.GetKeyDown ...)块内。此外,关于重力,你将它乘以那里的两倍Time.deltaTime,所以删除第一个。那应该让你开始。

于 2014-03-03T16:47:40.910 回答