• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • 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
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

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 MiszczTheMaste · Nov 21, 2016 at 10:10 PM · c#triggersraycastsline of sight

Best way to create line of sight

Howdy, I'm making a top-down shooter and I wanted to determine if enemies can see player, so they can shoot him if there is no obstructions. After while of thinking I made script which base on two triggers.

 public int lineOfSight;
     public GameObject bulletPrefab;
     public bool inCloseRange;
 
     float rateOfFireTimer;
     float rateOfFire;
 
     PolygonCollider2D sightCollider;
     Vector2[] trianglePoints = new Vector2[3];
 
     void Start()
     {
         sightCollider = gameObject.GetComponent<PolygonCollider2D>();
 
         LineOfSightCalculation();
     }
 
     void Update()
     {
         DetermineDirection();
 
     }
 
 
     void OnTriggerStay2D(Collider2D col)
     {
         if (col.tag == "Player") 
         {
             RaycastHit2D inLineOfSigh = Physics2D.Raycast(gameObject.transform.position, GameObject.FindGameObjectWithTag("Player").transform.position - gameObject.transform.position);
             //Debug.DrawRay (transform.position, GameObject.FindGameObjectWithTag ("Player").transform.position - transform.position, Color.green, 120);
 
             if (inLineOfSigh.collider.gameObject.tag == "Player") 
             {
 
                 if (col.IsTouching(GetComponent<CircleCollider2D>()))
                 {
                     Vector3 kiepo = inLineOfSigh.point;
                     Vector3 rayDir = kiepo - gameObject.transform.position;
                     rateOfFireTimer = 0.1f;
 
                     if (rayDir.y > rayDir.x && rayDir.x > 0 && rayDir.y > 0 || rayDir.y > Math.Abs(rayDir.x) && rayDir.x < 0 && rayDir.y > 0)
                     {
                         FaceUp();
                     }
 
                     if (Math.Abs(rayDir.y) > rayDir.x && rayDir.x > 0 && rayDir.y < 0 || rayDir.y < rayDir.x && rayDir.x < 0 && rayDir.y < 0) 
                     {
                         FaceDown();
                     }
 
                     if (rayDir.x > rayDir.y && rayDir.y > 0 && rayDir.x > 0 || rayDir.x > Math.Abs(rayDir.y) && rayDir.y < 0 && rayDir.x > 0) 
                     {
                         FaceRight();
                     }
 
                     if (Math.Abs(rayDir.x) > rayDir.y && rayDir.y > 0 && rayDir.x < 0 || rayDir.x < rayDir.y && rayDir.y < 0 && rayDir.x < 0) 
                     {
                         FaceLeft();
                     }
                 }
 
                 if(!col.IsTouching(GetComponent<CircleCollider2D>()))
                 {
                     rateOfFireTimer = 0.25f;
                 }
 
                 inCloseRange = true; // won't allow move to another point in route
 
                 rateOfFireTimer += Time.deltaTime;
 
                 if (rateOfFireTimer >= rateOfFire)
                 {
                     Instantiate(bulletPrefab, transform.position, Quaternion.identity);
                     rateOfFireTimer = 0;
                 }
 
                 gameObject.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
             }
         }
     }
 
     void OnTriggerExit2D(Collider2D col) //quickfix for patrol route bug
     {
         if (col.tag == "Player" && !col.IsTouching(GetComponent<CircleCollider2D>()))
         {
             inCloseRange = false;
         }
     }
 
     void LineOfSightCalculation()
     {
         Vector2 A,B;
 
         trianglePoints[0] = Vector2.zero;
         B.x = (float) -(lineOfSight * Math.Sqrt(2)) /2;
         B.y = (float) -(lineOfSight * Math.Sqrt(2)) /2;
 
         A.x = (float)  (lineOfSight * Math.Sqrt(2)) /2;
         A.y = (float) -(lineOfSight * Math.Sqrt(2)) /2;
 
         trianglePoints[1] = B;
         trianglePoints[2] = A;
 
         sightCollider.SetPath(0, trianglePoints);
     }
 
     void DetermineDirection()
     {
         var localVel = gameObject.transform.InverseTransformDirection(gameObject.GetComponent<Rigidbody2D>().velocity);
 
         if (localVel.x > 0 && System.Math.Abs(localVel.x) > System.Math.Abs(localVel.y)) 
         {
             FaceRight();
         }
         if (localVel.x < 0 && System.Math.Abs(localVel.x) > System.Math.Abs(localVel.y)) 
         {
             FaceLeft();
         }
         if (localVel.y > 0 && System.Math.Abs(localVel.y) > System.Math.Abs(localVel.x)) 
         {
             FaceUp();
         }
         if (localVel.y < 0 && System.Math.Abs(localVel.y) > System.Math.Abs(localVel.x)) 
         {
             FaceDown();
         }
     }
 
     void FaceRight()
     {
 
         trianglePoints [0] = Vector2.zero;
         trianglePoints [1] = new Vector2 (Math.Abs(trianglePoints[1].x), Math.Abs(trianglePoints[1].y));
         trianglePoints [2] = new Vector2 (Math.Abs(trianglePoints[1].x), -Math.Abs(trianglePoints[1].y));    
 
         sightCollider.SetPath (0, trianglePoints);
         //print ("going right");
 
         GetComponent<Animator> ().Play ("enemy_right", 0);
     }
 
     void FaceLeft()
     {
         trianglePoints [0] = Vector2.zero;
         trianglePoints [1] = new Vector2 (-Math.Abs (trianglePoints [1].x), Math.Abs (trianglePoints [1].y));
         trianglePoints [2] = new Vector2 (-Math.Abs (trianglePoints [1].x), -Math.Abs (trianglePoints [1].y));    
 
         sightCollider.SetPath (0, trianglePoints);
         //print ("going left");
 
         GetComponent<Animator> ().Play ("enemy_left", 0);
     }
 
     void FaceUp()
     {
         trianglePoints [0] = Vector2.zero;
         trianglePoints [1] = new Vector2 (-Math.Abs(trianglePoints[1].x), Math.Abs(trianglePoints[1].y));
         trianglePoints [2] = new Vector2 (Math.Abs(trianglePoints[1].x), Math.Abs(trianglePoints[1].y));    
 
         sightCollider.SetPath (0, trianglePoints);
         //print ("going up");
 
         GetComponent<Animator>().Play ("enemy_up", 0);
     }
 
     void FaceDown()
     {
         trianglePoints [0] = Vector2.zero;
         trianglePoints [1] = new Vector2 (-Math.Abs(trianglePoints [1].x), -Math.Abs(trianglePoints[1].y));
         trianglePoints [2] = new Vector2 (Math.Abs(trianglePoints [1].x), -Math.Abs(trianglePoints[1].y));    
 
         sightCollider.SetPath (0, trianglePoints);
         //print ("going down");
 
         GetComponent<Animator>().Play ("enemy_down", 0);
     }

So generally, when npc is going north he have triangular trigger located above of him and litte circle trigger around him. When player walks into his triangle he stops and start shoting, when player exit trigger he continue his route. And when player enter that circle npc turns around so he can shoot him, and then player must again exit triangular trigger.

But there is one problem. It's working, enemy dont shoot you through the walls, animations also are playing in right moments, but i don't like the way it is working. I just wanted to know if there is any better way to do this (and i suppose there is plenty of ways).

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

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

OnTriggerStay with multiple collision parameters 1 Answer

Initialising List array for use in a custom Editor 1 Answer

AI raycasting problem 0 Answers

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