Homing Missiles!
Hello, I am currently attempting to create a game that is a top down spaceship game. This game is an android made app that is similar to the "Missiles!" application currently on the Google Play Store. I have made the actual player (The space ship), but I am having immense trouble with the homing missiles that follow and try to destroy the player. I want the missiles to be like the one in the aforementioned "Missiles!" game. But I cannot recreate them even though I have went through multiple threads and videos about this. I am really stuck right now, and help would be greatly appreciated... I want this to be a 2D missile which follows a 2D character object, on a 2D background. But as I said, the missiles don't currently work. Thank you!
Answer by Phantom_101 · Jun 29, 2017 at 06:14 PM
Here's the code I created for a missile.
using UnityEngine;
using System.Collections;
public class Missile : MonoBehaviour {
public float RangeTilShutdown;
public int DamageToShields;
public int DamageToHull;
public GameObject ExplosionSmall;
public Vector3 PositionSpawned;
private GameObject[] Flares;
private GameObject[] Hostiles;
private GameObject MissileTarget;
private bool Ready = false;
void Start () {
PositionSpawned = gameObject.transform.position;
Flares = GameObject.FindGameObjectsWithTag ("Flare");
Hostiles = GameObject.FindGameObjectsWithTag ("Hostile");
if (Flares.Length > 0) {
MissileTarget = Flares [0];
} else if (Hostiles.Length > 0) {
MissileTarget = Hostiles [0];
}
}
void Update () {
if (MissileTarget != null) {
GetComponent<SlerpToLookAt> ().Target = MissileTarget.transform;
} else {
Destroy (gameObject);
}
if (Vector3.Distance (gameObject.transform.position, PositionSpawned) >= RangeTilShutdown) {
Destroy (gameObject);
}
}
void OnCollisionEnter(Collision Other) {
ContactPoint Contact = Other.contacts [0];
Quaternion Rot = Quaternion.FromToRotation (Vector3.up, Contact.normal);
Vector3 Pos = Contact.point;
Instantiate (ExplosionSmall, Pos, Rot);
if (Other.gameObject.GetComponent<Shield> ().ComponentDisabled == false) {
int TargetDamageVariance = Other.gameObject.GetComponent<Shield> ().DamageVariance;
int DamageDealt = DamageToShields + Mathf.RoundToInt(Random.Range(-TargetDamageVariance, TargetDamageVariance));
Other.gameObject.GetComponent<Shield> ().Power -= DamageDealt;
} else if (Other.gameObject.GetComponent<HullIntegrity> ().ComponentDisabled == false) {
int TargetDamageVariance = Other.gameObject.GetComponent<HullIntegrity> ().DamageVariance;
int DamageDealt = DamageToHull + Mathf.RoundToInt (Random.Range (-TargetDamageVariance, TargetDamageVariance));
Other.gameObject.GetComponent<HullIntegrity> ().Integrity -= DamageDealt;
}
Destroy (gameObject);
}
}
This is SlerpToLookAt.cs:
using UnityEngine;
using System.Collections;
public class SlerpToLookAt: MonoBehaviour {
public Transform Target;
public float RotationSpeed;
private Quaternion _LookRotation;
private Vector3 _Direction;
void Update () {
_Direction = (Target.position - transform.position).normalized;
_LookRotation = Quaternion.LookRotation (_Direction);
gameObject.transform.rotation = Quaternion.Slerp (transform.rotation, _LookRotation, Time.deltaTime * RotationSpeed);
}
}
Be sure to also attach SlerpToLookAt.cs onto the missile. Also attach rigidbody and constant force. On rigidbody, uncheck "Use Gravity". On constant force, put the speed desired in the z axis of relative force. By the way, the flares gameobject[] is for antimissile flares. You can remove that if you want. I used this for a 3D game, but it probably can also work in 2D.
Your answer
Follow this Question
Related Questions
Trying to get missile to home in on player position instead of one fixed position 0 Answers
Is it possible to get a number of pre-set hashtags per minute from twitter? 0 Answers
How can I achieve a homing missile to hover over or near player? 0 Answers
SOLVED: How to properly follow in high velocity in 2D? 0 Answers