首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正确使用Unity4.6 MoveTowards

正确使用Unity4.6 MoveTowards
EN

Stack Overflow用户
提问于 2015-02-24 01:40:07
回答 2查看 651关注 0票数 0

我已经在这上面工作了几天了,但没有成功。我制作了一只鸟拍打着翅膀的精灵动画。我想让这只鸟飞到树上的某个特定位置。树是一个图像,它有一个附加了动画的子图像。动画效果很好,但我想把它移到树上的一个分支上。下面列出的类被附加到子图像,但鸟不会向分支移动。

代码语言:javascript
复制
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class FlyBird : MonoBehaviour {

    public Transform target; // Target is empty game object defined in the inspector
    public float MoveSpeed; // Set to 10 in the inspector

    // Script is attached to an Image object and the child of the image is a sprite animation

    void FixedUpdate()
    {
        transform.position = Vector2.MoveTowards(transform.position, target.position, MoveSpeed * Time.deltaTime);
        Debug.Log(transform.position.x);
    }
}
EN

回答 2

Stack Overflow用户

发布于 2015-02-24 01:47:01

我不知道您目前的行为是什么,但有可能是您正在使用FixedUpdate(),所以您应该使用Time.fixedDeltaTime而不是Time.deltaTime

票数 0
EN

Stack Overflow用户

发布于 2015-02-24 06:33:09

您正在使用UI图像,如果我在您的编辑之后没有记错的话。对于这种类型的对象,应该使用它的RectTransform而不是Transform。试试这个:

代码语言:javascript
复制
rectTransform.position = Vector2.MoveTowards(rectTransform.position, target.rectTransform.position, MoveSpeed * Time.deltaTime);

当然,假设您可以从目标对象获取rectTransform。另一种方法是操纵rectTransform.anchoredPosition。

请在此处查看有关此类型的Rect转换的更多信息:http://docs.unity3d.com/460/Documentation/ScriptReference/RectTransform.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28679874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档