简体   繁体   English

Unity 3D在GetButtonDown上的特定时间内移动对象

[英]Unity 3D Move object over specific time on GetButtonDown

I've got an empty game object filled with all my player components, within it is a model which animates itself when the controls are pressed which are instantiated by GetButtonDown. 我有一个空的游戏对象,里面充满了我所有的玩家组件,在其中是一个模型,当按下由GetButtonDown实例化的控件时,该模型会对其自身进行动画处理。 Currently everything in the player game object moves instantly to the next point (basically teleports there with no transition). 当前,玩家游戏对象中的所有内容都会立即移动到下一个点(基本上是在没有过渡的情况下传送到该点)。 Is there a way I can get it all to move over say 1 second rather than going there instantly? 有没有办法让我将所有内容移动超过1秒钟而不是立即到达那里? Here's what happens on GetButtonDown 这是GetButtonDown发生的情况

    if (Input.GetButtonDown ("VerticalFwd"))
    {

        amountToMove.y = 1f;
        transform.Translate (amountToMove);
    }

I've tried transform.Translate (amountToMove * Time.deltaTime * randomint); 我已经尝试过transform.Translate(amountToMove * Time.deltaTime * randomint); and all sorts of ways to use Time however that doesn't seem to work even though it seems the most logical way. 以及各种使用时间的方式,尽管这似乎是最合乎逻辑的方式,但似乎仍然行不通。 I'm guessing because the GetButtonDown only "runs" when it is pressed and has no update function to "time" the move every frame? 我猜是因为GetButtonDown仅在按下时“运行”,并且没有更新功能来“计时”每一帧的移动? Any ideas? 有任何想法吗? amountToMove is saved as a Vector3 aswell. amountToMove也保存为Vector3。

The first formula is wrong. 第一个公式是错误的。 The correct formula would be: 正确的公式为:

this.transform.position += (Distance/MoveTime) * time.deltatime 

Try .Lerp, it runs as a coroutine interpolating a value over some amount of time Vector3.Lerp(Vector3 start, Vector3 end, float time) 尝试.Lerp,它作为协程在某个时间量Vector3.Lerp(Vector3开始,Vector3结束,浮动时间)上插值

See documentation here 在这里查看文档

This should give you a rough idea of whats going on 这应该使您大致了解发生了什么情况

Vector3 Distance = End - Start; 
// this will return the difference 

this.transform.position += Distance/time.deltatime * Movetime 
// this divides the Distance equal to the time.deltatime.. (Use Movetime to make the movement take more or less then 1 second

IE: If the Distance is 1x.1y.1z, and time.deltatime is .1 and Movetime is 1.... you get a movement of .1xyz per tick, taking 1 second to reach the end point IE:如果Distance为1x.1y.1z,并且time.deltatime为.1且Movetime为1 ....,则您每跳动的移动值为.1xyz,花费1秒到达终点

FYI: transform.Translate works as a fancy .position +=, for this purpose you can use them interchangeably 仅供参考:transform.Translate就像一个奇特的.position + =,因此您可以互换使用它们

EDIT Here are a few solutions 编辑这里有一些解决方案

Easy :: 简单 ::

Vector3 amountToMove = new Vector3(0,1,0); //  Vector3.up is short hand for (0,1,0) if you want to use that instead

if (Input.GetButtonDown ("VerticalFwd"))
    {

        transform.position = Vector3.Lerp(transform.position ,transform.position + amountToMove, 1);
    }

Hard and probably unnecessary :: 困难,可能不必要:

Write a Coroutine that does a kind of Linear interpolation for your specific application See documentation here 撰写协程,做了一种线性插值为特定应用程序查看此文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM