【问题标题】:Rotate arrow without using Unity3d physics engine在不使用 Unity3d 物理引擎的情况下旋转箭头
【发布时间】: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,这些不会产生影响,并且会不必要地使您的方程式复杂化。

标签: c# unity3d rotation


【解决方案1】:

试试这个

while (!arrowSimulationScript.Collided())
{
    Vector3 delta = new Vector3(0, (Vy - (gravity * elapse_time)) * Time.deltaTime, Vx * Time.deltaTime);
    Vector3 newPos = Projectile.position + delta;
    Vector3 offset = (newPos - Projectile.position);
    Vector3 magnitude = offset.magnitude;
    Vector3 newDir = offset.normalized;

    Projectile.rotation = Quaternion.LookRotation(newDir, Vector3.Cross(newDir, Projectile.right)));

    arrowSimulationScript.Velocity = magnitude;

    elapse_time += Time.deltaTime;
    Projectile.position = newPos;

    yield return null;
}

【讨论】:

  • 我得到一个编译错误:Projectile.rotation = Quaternion.LookRotation((newPos - Projectile.position).normalized, Vector3.Dot(Projectile.right, newDir)); LookRotation,将两个 Vector3 对象作为参数,第二个是浮点数。
  • 对不起,我的意思是制作 Cross 产品,而不是 Dot 产品
  • 谢谢,但它的工作方式与旧代码一样,效果与您在视频中看到的一样
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多