- Home /
Test if object is within collider bounds on start
Hey Unitarians!
Fairly simple question, but I couldn't find the answer anywhere, nor in the documentation.
What I have is a missile that is instantiated when the player fires it. I have a sphere Collider on the object. I want is so that when it detects an enemy, it locks on as a target and homes into it. Checking to see whether an enemy has entered and turning it into a target is easy. I use this script:
using UnityEngine;
using System.Collections;
public class PlayerRocketRangeChecker : MonoBehaviour {
public Transform target;
void OnTriggerEnter (Collider other) {
if(other.gameObject.tag == "Enemy") {
target = other.gameObject.transform;
}
}
void OnTriggerExit (Collider other) {
if(other.gameObject.transform == target) {
target = null;
}
}
}
However, this poses a problem. If the player shoots the missile when it is really close to the enemy, the missile fails to lock in, because it starts off with the enemy within its collider.
So is there a way to check to see if an enemy is inside the collider on start?
This detects it as its inside it. you can make an if it is inside, i.e. stay, do something.
Your answer
Follow this Question
Related Questions
Flying AI Planes 0 Answers
Unable to check WALL to WALL check for tower defense game 0 Answers
I need help with a script(brauche hilfe mit einen Script) 0 Answers
Starting with unity - which scripting language ? 1 Answer
RPC kill counter 1 Answer