- Home /
How to tell if the player is behind a enemy?
Hi
What could I use if I want to add a feature that can determinate if the player is behind or in-front of the enemy? Is there any way of comparing if the players position is within or outside of 180-360 degrees? Or does anyone know any good script that tells this?
Answer by mpavlinsky · Jan 07, 2012 at 04:42 PM
You can solve this easily with linear algebra. Basically you want to compare the vector from the enemy to the player and the vector that describes the direction the enemy is facing (the forward property of it's transform) using a dot product which shows how similar two vectors are (Equivalent vectors will return a dot product of 1, perpendicular vectors will return 0, and opposite vectors will return -1. Everything in between returns some floating point value depending on how similar it is.). Here's a script to demonstrate:
using UnityEngine;
using System.Collections;
public class FrontTest : MonoBehaviour {
public Transform target = null;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.S)) {
Vector3 toTarget = (target.position - transform.position).normalized;
if (Vector3.Dot(toTarget, transform.forward) > 0) {
Debug.Log("Target is in front of this game object.");
} else {
Debug.Log("Target is not in front of this game object.");
}
}
}
}
Good solution, but deserves a little comment: the dot product range from -1 to 1 when the two vectors are normalized (have length = 1). If they have different lengths, the dot product will range from -length1*lenght2 to +length1*lenght2. With normalized vectors, the dot product is the cosine of the angle between them: cos(0)=1, cos(90)=0, cos(180)=-1.
Thanks for the much more thorough explanation, aldonaletto.
It's a very old answer, but I thought I could improve on it a little bit, for a very specific scenario which in my case was checking if the player sees another game character from behind. You can check it out on my blog :) https://mostly-unity.com/2019/01/13/checking-if-a-transform-is-behind-another-transform-in-unity3d/
Answer by Fimgers · Jun 16, 2016 at 06:07 AM
Cant get this to work properly...I have modded it a bit to work for my project but it is basically switching from behind to in front of the target. (Im not using the Transform target, I am using the AI's Base script and grabbing the transform.position from that)
public void OnTriggerEnter (Collider other)
{
// Player
if (UsedByPlayer) {
if (IsColliding) {
if (other.gameObject != User) {
if (other.tag == "Enemy") {
Target = other.GetComponent<AIBase> ();
target = other.transform;
if (MaxCrushingDamage > 0) {
Vector3 toTarget = (target.position - Char.transform.position).normalized;
if (Vector3.Dot(toTarget, Char.transform.forward) > 0) {
Debug.Log("Target is in front of this game object.");
}
else if (Vector3.Dot(toTarget, Char.transform.forward) < 0) {
Debug.Log("Target is not in front of this game object.");
}
}
Answer by robert_mathew · Jan 07, 2012 at 04:16 PM
http://answers.unity3d.com/questions/144468/detecting-enemy-near-to-player.html
http://answers.unity3d.com/questions/175921/detecting-if-a-certain-tag-near-another-tag.html
check this answer how to detect enemy near to player
Well... I know how to make the enemy detect if a player is near. I knew that a long time ago. But I never used a function that could tell if the player is behind the enemy.