- Home /
Homing Missile, get two different targets?
using UnityEngine;
using System.Collections;
public class pRocketMod : MonoBehaviour {
//private Vector3 closestTarget;
private Vector3 leftClosestTarget;
private Vector3 rightClosestTarget;
//public GameObject rocket;
public GameObject leftRocket;
public GameObject rightRocket;
private float speed = 2.0f;
private float lifeTime;
private float maxDistance;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Enemy") {
Destroy(this.gameObject);
Destroy(other.gameObject);
}
}
void FireRocketAtEnemy()
{
Vector3 leftCPos = leftClosestTarget - leftRocket.transform.position;
Vector3 rightCPos = rightClosestTarget - rightRocket.transform.position;
leftRocket.transform.position += leftCPos * speed * Time.deltaTime;
rightRocket.transform.position += rightCPos * speed * Time.deltaTime;
}
// Update is called once per frame
void Update ()
{
FireRocketAtEnemy ();
if (GameObject.FindGameObjectWithTag ("Enemy") != null) {
leftClosestTarget = GameObject.FindGameObjectWithTag ("Enemy").transform.position;
rightClosestTarget = GameObject.FindGameObjectWithTag("Enemy").transform.position;
}
}
}
Current script for the Homing Missile.
Trying to make it so that each missile is sent towards a different target. Current setup fires both at the same because, well thats how the tags are set in the Update.
So how do I make it so that it shifts between objects?
You need a Find method which returns more than one object or something like OverlapSphere
This is a design problem. Do you want two random enemies, or the closest and 2nd closest, ... ?
Look up how to get a list of enemies (or how to check for enemies in a certain area ... depending what you're thinking.) Then figure out the rule you want for picking the targets, then try to figure out how pick two things from the list using that rule.
"Current setup fires both at the same because, well thats how the tags are set in the Update."
I know... I did it like that for testing purposes.. Now Im just wondering on how to make it so that it sends each one toa different object.
Gonna try the OverlapSphere thing.
Your answer
Follow this Question
Related Questions
Stopping navmesh agent next to the enemy 0 Answers
Rigidbody slows down before touching its target 1 Answer
focusing on a unit during unit detection in an RTS 1 Answer
Move player same distance every touch 2 Answers
Help Calculating Shortest Total Distance Travelled By a Combination of Objects 0 Answers