- Home /
Detect if player is in range?
Trying to detect if the play is within range of the enemy for a simple AI script. It's been a while since i last coded so I am suffering memory loss lol. I tried OnCollisionEnter, OnTriggerEnter, but was having trouble remembering, Rigid body on just one? or Both? Collider on one or both? is trigger isn't trigger? is Kinematic isn't kinematic? So I thought well lets just detect distance on a Vector3 but not even that is working for me now. Here is what I have, let me know some tips, or any advice to solve this issue.
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public GameObject target;
public int maxRange;
public int minRange;
private Vector3 targetTran;
// Use this for initialization
void Start ()
{
target = GameObject.FindWithTag("Player");
targetTran = target.transform.position;
}
// Update is called once per frame
void Update ()
{
if ((Vector3.Distance(transform.position, target.transform.position) < maxRange)
&& (Vector3.Distance(transform.position, target.transform.position) > minRange))
{
transform.LookAt(targetTran);
transform.Translate(Vector3.forward * Time.deltaTime);
}
}
}
What do you mean with not working? It does not rotate, it does it when it should not... ? What happens if you add some debug lines to print the distance on each frame?
I mean its just not working. Nothing is happening, no comments to the console nothing. No matter what i try, none of the OnTriggers will work. I've used other peoples scripts, the one I posted below, followed tutorials, nothing is working when i try to detect collisions
Answer by TonyLi · Mar 21, 2014 at 03:04 PM
If you use the Physics system like you originally thought, you can avoid the distance computations every frame. Add a SphereCollider to your AI. Set the radius to minRange. You could add the collider by hand or automatically within the EnemyAI script. Tick Is Trigger to make it a trigger. Since the AI will presumably move, add a Rigidbody and tick Is Kinematic (unless the AI already has a rigidbody).
Then add OnTriggerEnter and OnTriggerExit methods to keep track of when the player enters the trigger area:
private Transform target = null;
void OnTriggerEnter(Collider other) {
if (other.tag == "Player") target = other.transform;
}
void OnTriggerExit(Collider other) {
if (other.tag == "Player") target = null;
}
In Update, move to the target (or away, if it's too close):
void Update() {
if (target == null) return;
transform.LookAt(target);
float distance = Vector3.Distance(transform.position, target.position);
bool tooClose = distance < minRange;
Vector3 direction = tooClose ? Vector3.back : Vector3.forward;
transform.Translate(direction * Time.deltaTime);
}
I don't think you have to reinstall Unity. Try the attached test scene. When playing, in the Scene view I can move the Cube into the AI trigger and it prints the debug line. You can also enable the "$$anonymous$$ove" script on the Cube and watch it move itself into the trigger. Let us know if this works on your machine.link text
Answer by JoshMBeyer · Mar 21, 2014 at 03:09 PM
I just tried creating an empty scene. Made a cube(With a cube collider set to "is Trigger") and a Sphere, with a Sphere collider, and a rigid body. Pressed play, moved the sphere clear through the cube, several times in several different directions and nothing??? I am starting to think it isn't me, it is unity now.
here is the script for testing it.
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
Debug.Log("Made A collision");
}
}
i even tried every possible combination of the is trigger, is kinematic and stil nothing
I am posting a video of my screen to youtube so you can see everything i see
To detect collisions at least one of them has to have a rigidbody attached.
As shown in the video, and mentioned in the commentary of my post here.