Missile Trajectory Tracking

I am a little new to Unity 3D. I have searched the forum on this subject and could not find an answer.

I have taken the missile example scripts from the FPS example and created a 2D PLANE where multiple enemy cubes roll around the outside of the plane and a missile turret in the middle fires at them.

Here are my 2 issues:

  1. When the turret finds the closest target in range, it will stay focused on that target until it is out of range. How do I pass the target name to the projectile I am firing? I read a lot about instantiated projectiles but couldn’t quite figure this one out. If I can’t pass the target name to the projectile then I am guessing that while I update the trajectory of the missile, it will change target if a different target becomes closer (which I don’t want).

  2. I am new to the whole Vector thing and couldn’t figure out how to rotate and change the direction of the projectile. I am guessing the code goes in the Update statement of the instantiated projectile, and I am guessing you rotate the projectile towards the target and change the direction, but just need a little help getting there.

I appreciate any assistance. So far these forums have been a huge help to me.

Thanks in advance.

Actually I may have figured out the tracking issue, but passing the target to the projectile is still a problem.

Hello!

If your projectile is a rigidbody, you can apply to it a torque to make it “rotate”.

hope it helps :wink:

Once you have instantiated the projectile, you can get the script attached to it using GameObject.GetComponent:-

var obj: GameObject = Instantiate(projectile);
var projScript = obj.GetComponent(ScriptName);

Once you have the script component, you can access its public variables:-

projScript.projectileTargetName = "Enemy1";

You just need to declare a public variable in the projectile script for the name of the target.

Thanks for the help guys. I eventually figured this one out. When the turret uses “SendMessage” to Instantiate the projectile, I also send the target name.