【发布时间】:2015-09-10 14:10:28
【问题描述】:
我有这段代码可以在不使用统一物理引擎的情况下模拟弹丸的运动
IEnumerator LaunchProjectile(int angle, float speed)
{
// Move projectile to the position of throwing object + add some offset if needed.
Projectile.position = transform.position + new Vector3(0, 0f, 0);
Projectile.rotation = transform.root.rotation;
Debug.Log(transform.root.eulerAngles);
// Calculate distance to target
float target_Distance = speed * (Mathf.Sin(2 * firingAngle * Mathf.Deg2Rad) / gravity);
// Extract the X Y componenent of the velocity
float Vx = Mathf.Sqrt(speed) * Mathf.Cos(firingAngle * Mathf.Deg2Rad);
float Vy = Mathf.Sqrt(speed) * Mathf.Sin(firingAngle * Mathf.Deg2Rad);
// Calculate flight time.
float flightDuration = target_Distance / Vx;
this.arrowSimulationScript = Projectile.gameObject.GetComponentInChildren<ArrowSimulation>();
float elapse_time = 0;
while (!arrowSimulationScript.Collided())
{
Projectile.Translate(0, (Vy - (gravity * elapse_time)) * Time.deltaTime, Vx * Time.deltaTime);
Projectile.LookAt(Projectile.position - new Vector3(0, (Vy - (gravity * elapse_time)) * Time.deltaTime, Vx * Time.deltaTime));
arrowSimulationScript.Velocity = new Vector3(0, (Vy - (gravity * elapse_time)) * Time.deltaTime, Vx * Time.deltaTime).magnitude;
elapse_time += Time.deltaTime;
yield return null;
}
}
箭头朝附有此脚本的对象的方向发射。为了使箭头旋转,我使用这行代码:
Projectile.LookAt(Projectile.position - new Vector3(0, (Vy - (gravity * elapse_time)) * Time.deltaTime, Vx * Time.deltaTime));
箭头向右移动,但尖端朝向 z 轴方向,而不是弓的方向。 这是该问题的视频: https://www.youtube.com/watch?v=cyK6DXxTw_E
如果我注释掉那行代码,箭的尖端朝向弓的方向飞行,但它不旋转。
【问题讨论】:
-
尽管这与 Unity3D 和更一般的数学无关。由于其物理数学内容,这个问题在gamedev.stackexchange.com 上可能会做得更好。
-
谢谢,也发在那里了。
-
它面向 z,因为 x 为零。我不能真正做心算,但 x 不应该是 ((Vx - (gravity * elapse_time)) * Time.deltaTime) 并且 z 应该是 Vx 和 Vy 的平均值。
-
@Thouartamazing 类似的东西:
LookAt(Projectile.position - new Vector3(Vx * Time.deltaTime, (Vy - (gravity * elapse_time)) * Time.deltaTime, ( ((Vy - (gravity * elapse_time)) * Time.deltaTime) + (Vx * Time.deltaTime) ) / 2));它不起作用:( -
首先删除 deltatimes,这些不会产生影响,并且会不必要地使您的方程式复杂化。