我已经在这上面工作了几天了,但没有成功。我制作了一只鸟拍打着翅膀的精灵动画。我想让这只鸟飞到树上的某个特定位置。树是一个图像,它有一个附加了动画的子图像。动画效果很好,但我想把它移到树上的一个分支上。下面列出的类被附加到子图像,但鸟不会向分支移动。
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);
}
}发布于 2015-02-24 01:47:01
我不知道您目前的行为是什么,但有可能是您正在使用FixedUpdate(),所以您应该使用Time.fixedDeltaTime而不是Time.deltaTime
发布于 2015-02-24 06:33:09
您正在使用UI图像,如果我在您的编辑之后没有记错的话。对于这种类型的对象,应该使用它的RectTransform而不是Transform。试试这个:
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
https://stackoverflow.com/questions/28679874
复制相似问题