首页 文章

Unity3D鸟来回飞来飞去

提问于
浏览
0

我正在尝试创建一条沿着x轴在屏幕上移动的鸟 .

bird.transform.position = Vector3.Lerp (pos1, pos2, (Mathf.Abs(speed * Time.time) + 1.0f) / 2.0f);

在Update()中使用它,这只鸟只飞过一次 . 我希望它飞到右边后,它应该等待2-3秒,然后用不同的精灵飞回来 . transform.translate不能像这样工作 . 任何帮助将不胜感激 .

3 回答

  • 0

    你需要在另一个方向投入另一个LERP并且有一个变量,以便鸟类如此粗略地飞行:

    bool goleft;
    
    if(goleft)
    {
         if(transform.position != pos2)
         {
              transform.position = Vector3.Lerp (pos1, pos2, (Mathf.Abs(speed * Time.time) + 1.0f) / 2.0f);
         }
         else
         {
               goleft = false;
               //change the direction the bird is facing here
         }
    }
    else
    {
         if(transform.position != pos1)
         {
              transform.position = Vector3.Lerp (pos2, pos1, (Mathf.Abs(speed * Time.time) + 1.0f) / 2.0f);
         }
         else
         {
               goleft = true;
               //change the direction the bird is facing here
         }
    }
    

    希望有所帮助

  • 0

    没有测试,但我会从这里开始:

    Vector3[] posts;
    int current = 0;
    float speed = 5.0f;
    float threshold = Single.Epsilon;
    float delay = 2.0f;
    
    public void Update() {
        if(!waiting && posts.Length > 0)
        {
            if(!Mathf.Approximately((posts[current].position - transform.position).sqrMagnitude, threshold)
            {
                float step = speed * Time.deltaTime;
                transform.position = Vector3.MoveTowards(transform.position, posts[current].position, step);
            }
            else StartCoroutine("Next");
        }
    }
    
    IEnumerator Next() {
        waiting = true;
        yield return new WaitForSeconds(delay);
        current = (current + 1) % posts.Length;
        waiting = false;
    }
    

    这也可以让你拥有你想拥有的尽可能多的帖子,你所有的动作动态都可以在 Next() 中处理,而如果你想让他从帖子 0 ... 1 ... 2 ... 3 ... 0 ... 1 ..或 0 ... 1 ... 2 ... 3 ... 2 ... 1 ...

    如果你想要后者你只需将 current = (current + 1) % posts.Length; 更改为 Mathf.PingPong(current + 1, posts.Length);

  • 0

    我有点像这样:

    float flipCooldown
    float defaultFlipCooldown = 2.0f;
    bool isGoingRight;
    Vector2 pos1;
    Vector2 pos2;
    
    void Start() {
        flipCooldown = defaultFlipCooldown;
        isGoingRight = true;
        pos1 = new Vector2(0, 0);
        pos2 = new Vector2(5, 0); // whatever floats your boat
    }
    
    void Update()
    {
        Vector2 initialPosition = null;
        Vector2 finalPosition = null;
    
        if (flipCooldown <= 0) {
            isGoingRight = !isGoingRight
            flipCooldown = defaultFlipCooldown;
            ChangeSprite();
        }
    
        if (isGoingRight) {
            initialPos = pos1;
            finalPos = pos2;
        } else {
            initialPos = pos2;
            finalPos = pos1;
        }
    
        bird.transform.position = Vector3.Lerp (initialPos, finalPos, (Mathf.Abs(speed * Time.time) + 1.0f) / 2.0f);
    
        flipCooldown -= Time.deltaTime;
    }
    

    你想得到的是 Time.deltaTime 正在降低鸟的冷却时间 . 您可以轻松更改 defaultFlipCooldown 变量中的冷却时间 . 当它只是翻转位置时, Lerp 功能将完成其余的工作 . ChangeSprite 函数只是一个 GetComponent<SpriteRenderer>().sprite 更改 .

    如果你没有飞行,请定义最终位置,然后只需更改 pos1pos2 .

    同样重要的是要注意 WaitForSeconds 将与 Coroutines 一起使用,这是一个使用线程的概念,永远不会像 Update 这样的方法 . 您可以在Unity手册中了解有关Coroutines的更多信息:http://docs.unity3d.com/Manual/Coroutines.html

相关问题