1
我检查了如何使用动画移动gameObject
的Update()
函数内,因此您可以使用Time.deltaTime
但我想移动gameObject
这个功能之外,我也希望不断移动gameObject
一个游戏对象(随机在屏幕上旅行)。我目前没有动画的代码是:移动与动画外更新功能
using UnityEngine;
using System.Collections;
public class ObjectMovement : MonoBehaviour
{
float x1, x2;
void Start()
{
InvokeRepeating("move", 0f, 1f);
}
void move()
{
x1 = gameObject.transform.position.x;
gameObject.transform.position += new Vector3(Random.Range(-0.1f, 0.1f), Random.Range(-0.1f, 0.1f), 0);
x2 = gameObject.transform.position.x;
if (x2 < x1)
gameObject.GetComponent<SpriteRenderer>().flipX = true;
else
gameObject.GetComponent<SpriteRenderer>().flipX = false;
}
void Update()
{
}
}
什么是更好的实现方法?
不能浮子添加的Vector3,线b = gameObject.transform.position.x +新的Vector3(Random.Range(-0.1f,0.1F),Random.Range(-0.1f, 0.1f),0); – DAVIDBALAS1
我假设你的意思没有position.x? – DAVIDBALAS1
好吧,让我的代码工作,我删除了position.x,因为它给了我一个错误,切换了Start()函数内的命令顺序(如果你在updatedestiny之前调用move,那么矢量a,b都是空的,每个gameobject将从(0,0,0)开始)..谢谢:) – DAVIDBALAS1