Myth
August 7, 2012, 9:34pm
1
If I have a missile that I want to automatically lock onto a target, what is the best way to detect an enemy in front of it?
I have thought about creating a detection cone ( either as a mesh collider or something else ) or something using raycasting but am unsure what will work without slowing down the game too much,
Any help would be appreciated.
first you should find objects that potentially can be target
or
should helps
now just select one from them - this can be angle-to-forward check, distance check, other shields/armors that prevent homing missiles and so on
edit: FindClosestTransform.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class FindClosestTransform : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
Transform deleting = GetClosestTransform(1000f);
if (deleting)
{
Destroy(deleting.gameObject);
}
}
}
Transform GetClosestTransform(float radiusToSearch)
{
List colliders = new List(Physics.OverlapSphere(transform.position, radiusToSearch, -1));
colliders.RemoveAll(x => x.transform == transform);
if (colliders.Count == 0) { return null; }
else
{
Transform answer = colliders[0].transform;
for (int i = 1; i < colliders.Count; i++)
{
if ((answer.transform.position - transform.position).sqrMagnitude > (colliders*.transform.position - transform.position).sqrMagnitude)*
* {*
_ answer = colliders*.transform;*_
_* }*_
_* }*_
_* return answer;*_
_* }*_
_* }*_
_*}*_
_* *_