This may seem like a simple thing to anyone who uses unity, But this was one of the more challenging things to make and look realistic so they turn at a realistic pace and have a chance to miss.

 

First you many would say use the built in “transform.lookAt()” function but this meant the missile turned instantly and exploded as it collided with the helicopter, So i added a simple 0.5s gap between firing and turning, For those who don’t know a simple time gap is done like so:

float startTime = Time.time + 0.5f;

if(Time.time > startTime){
    // Do stuff
}

But this still lead to a very unrealistic turning after the time gap. While looking for examples of other ways people where talking about using Rigid body for this as the drag applied would cause a more realistic turn, But this time it looked at the target but carried on in a straight line. But i kept with rigid body ways to keep the gravity and drag for the most realistic missile.

 

after a while I came up with using a mix of rigid body methods and transform ( Rigid body takes into account physics and collisions, where as transform does not ) and came up with a realistic turn.

void FixedUpdate () {
	if(ifTarget){
		if(target == null){
			rigidbody.velocity = (transform.forward * 1500 * Time.deltaTime);

		} else {

			if(Time.time > startTime){
				Vector3 dir = target.position - transform.position;
				Quaternion rot = Quaternion.LookRotation(dir);
				transform.rotation = Quaternion.Slerp(transform.rotation, rot, 2f * Time.deltaTime);
			}

			rigidbody.velocity = (transform.forward * 1500 * Time.deltaTime);

		}			
	}
}

A playable demo, Press tab to target and F to switch to HellFire missiles: (Have to give a sound warning here aswell)

Heli
Heli
heli.zip
Version: prototype
13.8 MiB
1355 Downloads
Details

 

This demo also showed a bug, If you fire at the first tank it will start to go around indefinitely it because it didn’t turn in time, Which means i would have to come up with a detection system or a time limit for this type of occurrence.

 

 

 


Leave a Reply

Your email address will not be published.