我正在尝试让敌人向后移动并从一个位置移动到下一个位置。我正在使用乒乓球方法,但似乎不起作用。
public class Enemy : MonoBehaviour
{
public float movementSpeed = 5.0f;
Vector2 pointA;
Vector2 pointB;
Rigidbody2D rb;
void Start()
{
rb=GetComponent<Rigidbody2D>();
pointA = new Vector2(9, 0);
pointB = new Vector2(2, 0);
}
void Update()
{
float move = Mathf.PingPong(Time.time * movementSpeed, 1);
transform.position = Vector2.Lerp(pointA, pointB, move);
}
}
我所期待的正如我所说的。 让物体来回移动
我刚刚测试了你的代码,对我来说它工作得很好。这就是我最终使用的
public class Test : MonoBehaviour
{
Vector2 a = new Vector2(0, 0);
Vector2 b = new Vector2(0, 5);
float movementSpeed = 2f;
private void Update()
{
float move = Mathf.PingPong(Time.time * movementSpeed, 1);
transform.position = Vector2.Lerp(a, b, move);
}
}
你确定是代码本身有问题吗?是否是游戏引擎中的其他内容未正确设置,例如,您是否将此脚本作为组件附加到要移动的对象?是否有任何其他脚本可能影响该对象的位置?我不认为问题出在这段代码中,问题来自其他地方。