4
\$\begingroup\$

I have a missile launcher that uses initial velocity for the trajectory. I edit the launch angle from the editor, and then the script calculates the initial velocity required to land the missile to the target location. The inputs for the calculation are

  • Launcher position
  • Target position
  • Firing angle

In this scenario, the target and the launcher position has the same Y coordinates. I simply instantiate the missile prefab and set its initial rotation to match the firing angle (initial X rotation), rotate it to face the target position (corrected Y rotation), calculate the initial velocity using local space (calculate the Y and Z component of initial velocity), and then set its velocity so the trajectory is handled by the physics engine.

// initial X rotation of the missile: -_firingAngle;
GameObject missile = Instantiate(_projectile, transform.position, Quaternion.Euler(-_firingAngle, 0f, 0f)) as GameObject;
missile.transform.LookAt(_targetPosition);  // fix the Y rotation of the missile
// ...
// velocity calculations
// ...

// launch the missile
missile.GetComponent<Rigidbody>().velocity = Local2GlobalVelocity;

The missile lands its target just fine, however, the X rotation of the projectile during the trajectory doesn't change.

enter image description here

This is how the missile prefab looks like:

enter image description here


I want the missile to be rotated by the physics engine, or put another way, I want it to look natural while in motion. As you may have noticed from the first image, I have visited numerous unity forums to solve the issue. I tried updating the rotation of the missile on each frame as follows

transform.rotation = Quaternion.LookRotation(rb.velocity);

However, this results in a funny trajectory -- imagine an erect missile tilted a little backward, and at the time of landing, the missile is tilted a little forward. I'm not sure why, but when I try to edit the X rotation of the missile mid air when I use the LookRotation(rb.velocity) while the game is paused, the Z value of the rotation also changes. Since the velocity is a Vector3 with non-zero X,Y and Z components, I think the missile is subject to Gimbal Lock.

What are my options to make it look like a trajectory of a badminton ball?

Can I achieve this by adding rigidbodies to the parts that are at the top of the missile and adjust their mass so that the physics engine handles the trajectory rotation as it is a badminton ball, instead of me messing up with the rotation by scripting? If yes, how can I do that exactly?

\$\endgroup\$
1
  • 4
    \$\begingroup\$ Actually, rethinking my question, I realized transform.rotation = Quaternion.LookRotation(rb.velocity); solves my problem. All I need to do is to change the rotation of the body of the prefab. I'll post a detailed answer shortly. This is a case in which asking a detailed question made me think and solve my own problem. \$\endgroup\$
    – Varaquilex
    Jun 1, 2015 at 18:00

1 Answer 1

2
\$\begingroup\$

However, this results in a funny trajectory -- imagine an erect missile tilted a little backward, and at the time of landing, the missile is tilted a little forward.

The answer lies here. Physics engine actually successfully simulates the trajectory rotation of the object in relation with its rigidbodies velocity when you use LookRotation() in the objects Update() function. The only issue here is the rotation of the prefab. If you rotate the prefab's first child (body, in your case) by 90 degrees on X axis, you'll achieve the natural missile trajectory look.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .