使用vector3.MoveTowards移动对象 [英] Moving an object with vector3.MoveTowards

查看:2396
本文介绍了使用vector3.MoveTowards移动对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与Unity一起开发游戏应用程序.我在移动GameObject时遇到问题.

I am working on a game app with Unity. I have an issue when it comes to move a GameObject .

在我的游戏中,当玩家使用其设备向上滑动时,GameObject从A点移动到B点,而当他向下滑动时,GameObject从B点移动到A点.

In my game, when the player swipes up with his device, the GameObject moves from a point A to B, and when he swipes down, it goes from B to A.

我用游戏逻辑编写了一个C#脚本,但是涉及到这个问题.

I wrote a C# script with the game logic, but I have an issue when it comes to this.

问题在于GameObject立即从A移动到B.

The problem is that the GameObject moves instantly from A to B.

这是我用来移动GameObject的代码行:

Here is the code line I use to move my GameObject :

transform.localPosition = Vector3.MoveTowards (PositionA,PositionB,Time.deltaTime * speed);

speed是一个值为10.0f的浮点数.

speed is a float with a value of 10.0f.

我希望我的GameObject缓慢移动以将A指向B.尽管速度值发生了变化,但没有任何变化,它仍在瞬间移动.

I would like my GameObject to move slowly to point A to B. And despite the changes on speed value, nothing change, it's still moving instantly.

我该如何解决? (我尝试使用Vector3.Lerp并获得了相同的结果).

How can I fix that? (I tried with Vector3.Lerp and I had the same results).

推荐答案

Vector3.MoveTowards获取当前位置,目标位置和步骤,但似乎您的第一个参数是移动的起点,而不是当前位置位置.通常,您会在Update()中完成这样的操作:

Vector3.MoveTowards takes the current position, the target position, and the step, but it seems like your first argument here is origin of the move, rather than current position. Normally you'd do this something like this, in your Update():

transform.localPosition = Vector3.MoveTowards (transform.localPosition, PositionB, Time.deltaTime * speed);

以当前位置作为第一个参数.

with the current position as the first argument.

这篇关于使用vector3.MoveTowards移动对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆