【发布时间】:2021-05-20 00:12:17
【问题描述】:
我正在尝试编写一段代码来实例化一个游戏对象,将其旋转设置为此时面向玩家角色的光标方向,并以恒定速度朝该方向移动 2 秒然后停止。然而,我的一段代码正在将游戏对象向光标方向移动,但速度会根据我的光标与玩家角色的距离而改变。
private IEnumerator Rake()
{
Vector3 relativepos =
Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
Quaternion rotation = Quaternion.LookRotation(relativepos, Vector3.up);
float timepassed = 0;
GameObject WcastRB =
Instantiate(Wcast, gameObject.transform.position, rotation);
Rigidbody2D rg;
rg = WcastRB.GetComponent<Rigidbody2D>();
while (timepassed < 2)
{
timepassed += Time.deltaTime;
rg.velocity = WcastRB.transform.forward * 1000 * Time.deltaTime;
if (timepassed >= 2)
{
rg.velocity = WcastRB.transform.forward * 0;
}
yield return null;
}
}
这是我做的。
【问题讨论】:
-
考虑normalizing the
relativepos. 这样距离不会影响速度。 -
我没有使用
relativepos作为速度的倍数,它只用于设置实例化对象的旋转,我使用transform.forward设置对象的速度,我试过了标准化relativepos但问题仍然存在