【发布时间】:2014-11-14 16:01:24
【问题描述】:
我正在使用最简单的方法移动对象,这是在我学得更好之前我要做的事情:在每个Update 上,我使用Vector3.MoveTowards 将transform.position 移动到Vector3 destination。确切地说:
private GameObject actor; //Object to move
private void MakeMove()
{
//Move towards destination
actor.transform.position = Vector3.MoveTowards(actor.transform.position, destination, Time.deltaTime * moveSpeed);
//Update HUD info
actor.updatePosition();
//Terminate running status if the target was reached
if (actor.transform.position == destination && targetObj==null)
{
Stop();
}
}
这有两点很糟糕:
- 无论对象旋转如何,运动都会发生,而不是向前移动
- 对象将穿过任何其他对象到达目标
第二个问题是我想要的。假设我会在移动之前记住原来的位置:
Vector3 oldPos = actor.transform.position;
actor.transform.position = Vector3.MoveTowards( ... );
//Get true if there an object colliding with the one that was just moved
if(actor.gameObject.collider.something something) {
//revert back to old position
actor.transform.position = oldPos;
}
上述情况远非理想。我什至不知道如何实现这样一个简单的解决方案。你知道吗?
【问题讨论】: