所以我这里有一个脚本,当你点击它的时候,它移动到-1.25,但是我希望它不断的添加-1.25,直到你释放按钮。现在,它只移动一次,当你点击按钮。我的代码:
var character : GameObject;
function OnMouseDown () {
character.GetComponent(Animator).enabled = true;
BlahBlah ();
}
function OnMouseUp () {
character.GetComponent(Animator).enabled = false;
}
function BlahBlah () {
character.transform.position.x = character.transform.position.x + -1.25;
}有人有什么想法吗?谢谢!
发布于 2014-11-04 09:41:08
您只是忘了在Update()中工作
var character : GameObject;
function OnMouseDown ()
{
character.GetComponent(Animator).enabled = true;
}
function OnMouseUp ()
{
character.GetComponent(Animator).enabled = false;
}
function BlahBlah ()
{
// I added time.deltaTime, since you'll be updating the value every "frame",
// and deltaTime refers to how much time passed from the last frame
// this makes your movement indipendent from the frame rate
character.transform.position.x = character.transform.position.x - (1.25* time.deltaTime);
}
// The standard Unity3D Update method runs every frame
void Update()
{
if (character.GetComponent(Animator).enabled)
{
BlahBlah ();
}
}我在这里所做的就是使用统一逻辑。几乎所有东西都在Update()函数中工作,每个帧都会被调用。请注意,帧速率可能会根据场景的机器/复杂性而变化,因此,在添加相关内容时,请确保始终使用Time.deltaTime。
另一个注意事项:直接修改位置不会使对象对碰撞做出反应(因此,您将在对象中移动,但仍然会“触发”碰撞)。所以,如果你想要管理碰撞,记得使用物理!
发布于 2014-11-04 09:39:18
您可以使用Input.GetMouseButton,因为它注册每个帧的,所以当您按住鼠标时,鼠标就会得到它,但是因为它会在鼠标关闭时检查每一个帧,所以您的对象将以如此快的速度移动,所以您可能希望添加一个计时器,以便它移动得更慢一些,这样我们就可以检查我们在中设置的指定时间--heldE 212是E 113传递的E 214/code>,鼠标是E 115按住了e 216,然后我们移动我们的对象。
float timeLeft=0.5f;
void Update(){
timeLeft -= Time.deltaTime;
if (Input.GetMouseButton(0)){
if(timeLeft < 0)
{
BlahBlah ();
}
}
}
void BlahBlah () {
timeLeft=0.5f;
character.transform.position.x = character.transform.position.x + -1.25;
}发布于 2014-11-04 09:39:42
正如我所记得的,如果您按住按钮,onMouseDown会触发每一个帧,所以您需要在该函数中执行移动操作,让我检查一下。
https://stackoverflow.com/questions/26731207
复制相似问题