【问题标题】:Adding velocity to 2d sprite in c# unity在 C# Unity 中将速度添加到 2d 精灵
【发布时间】:2014-03-19 17:18:19
【问题描述】:

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

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;
    }
}

您能否修复错误,因为每次我为精灵广告设置统一的速度时,运行程序精灵一直在下降,无论我点击或点击空格键多少,精灵都不会停止下降如果我增加速度

【问题讨论】:

    标签: c# unity3d 2d flappy-bird-clone


    【解决方案1】:

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

    【讨论】:

    • 你看我的回答了吗?更新方法名不正确,除非正确,否则Unity不会调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多