• Unity
  • Services
  • Made with Unity
  • Learn
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MOLB · Jan 07, 2012 at 03:39 PM · playervector3enemy

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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.");
             }
         }
     }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aldonaletto · Jan 07, 2012 at 05:51 PM 1
Share

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.

avatar image MOLB · Jan 08, 2012 at 08:18 AM 0
Share

Thank you for this solution.

avatar image mpavlinsky · Jan 08, 2012 at 07:06 PM 0
Share

Thanks for the much more thorough explanation, aldonaletto.

avatar image MarekM2 · Jan 16 at 01:29 PM 0
Share

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/

avatar image
-1

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.");
                             }
                         }
 

 



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MOLB · Jan 08, 2012 at 08:17 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

EnemyHover Script Error? 0 Answers

Distance destroy object 3 Answers

AI Script attached to Enemy and is Rotating around player 0 Answers

enemy AI doesnt work when enemy is spawned but does if it is already in the scene 1 Answer

How to see an enemy from certain distance and change texture 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges