我有一个脚本,使用Unity引擎的 lerp
功能处理游戏对象的翻译 .
public class MovePiece : MonoBehaviour {
Vector3 startPoint;
Vector3 endPoint;
float speed = 1;
float startTime;
float journeyLength;
void Start() {
startPoint = gameObject.GetComponent<Transform>().position;
endPoint = startPoint + new Vector3(20, 0, 0); //Will make this a variable eventually
startTime = Time.time;
journeyLength = Vector3.Distance(startPoint, endPoint);
}
void Update() {
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
transform.position = Vector3.Lerp(startPoint, endPoint, fracJourney);
}
}
但是,我只想在运行游戏对象时运行一次,然后在完成移动时从该对象分离,以便下次单击其他对象时可以再次运行 .
我有一个连接到相机的光线投射设置,允许我选择游戏对象,我只是不知道我如何在所选对象上运行此脚本!
我怎样才能做到这一点?
1 回答
重命名您的Start方法
对于其他组件可公开访问的内容,即:
然后,让您的Raycast脚本调用您的新函数 . 您需要访问被点击的GameObject的MovePiece组件:
您可能还想为动画引入布尔锁 . 这是您的代码可能是什么样子 .
我希望有所帮助!祝你好运 .