0
\$\begingroup\$

I have a vertical missile with launcher that should find closest target and fire it's rocket to hitting target!

I use complete code of this 12min tutorial for launching my rocket, and the different between mine and its about the direction of rocket, mine is vertical and its horizontal!

homingMissile.velocity = transform.forward * missileVelocity;
Quaternion targetRotation = Quaternion.LookRotation(target.position - transform.position);
homingMissile.MoveRotation(Quaternion.RotateTowards(transform.rotation, targetRotation, turn));

After running the code, the rotation of my rocket change from (0,0,0) to (-8 .3,-4.7,0)!!!! and just change a little while hit target and explosion do.

In fact my rocket move to target in right path, but in wrong rotation!, in STRANGE!

Standby Rocket Standby Rocket

Moving to Target Moving to Target

Anyone can help me for this problem?

\$\endgroup\$
8
  • \$\begingroup\$ You could try using this transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turn); instead of this homingMissile.MoveRotation(Quaternion.RotateTowards(transform.rotation, targetRotation, turn)); furthermore I would like to know the value of turn, it must be between 0 to 1. \$\endgroup\$ Jun 24, 2019 at 8:21
  • \$\begingroup\$ hey @DigvijaysinhGohil, it's not work again. just rotation from (-8.5,-4.5,0) changed to (-10,-27,0)! The value of turn was 20 in first, but now i change it to 0.5 but not solving my problem. \$\endgroup\$ Jun 24, 2019 at 8:43
  • \$\begingroup\$ Is z axis or blue arrow of rocket points to the tip of the rocket in local space? \$\endgroup\$ Jun 24, 2019 at 9:54
  • \$\begingroup\$ @DigvijaysinhGohil nope, it's y axis \$\endgroup\$ Jun 24, 2019 at 9:58
  • 1
    \$\begingroup\$ That is the reason it is not rotating correctly, just create empty gameobject and assign the script to it, then make rocket as the child of empty object and rotate it in a way that the tip points to the z axis of empty object, also swap the collider and rigidbody to the empty gameobject as well \$\endgroup\$ Jun 24, 2019 at 10:09

1 Answer 1

2
\$\begingroup\$

The reason it is not rotating correctly is because the forward vector or z axis of the modal is not pointing to the tip of the rocket.

To solve it, either set the forward vector in some 3D modelling software or just create empty gameobject and assign the script to it, then make rocket as the child of empty object and rotate it in a way that the tip points to the z axis of empty object, also swap the collider and rigidbody to the empty gameobject as well.

\$\endgroup\$

You must log in to answer this question.

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