Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by Tschlompf · Aug 28, 2016 at 10:48 AM · raycastraycasting

Count objects in a raycast

Hello! I got a raycast, which I use to detect 2 different objects. Now I want to count, how many objects are in one raycast. That's my script (or parts of it):

public class RaycastWin : MonoBehaviour { int rightHitsToWin = 1;

void Update() { var transformStone = GetComponent();

Vector2 right = transform.TransformDirection(Vector2.right) * 5;

Ray2D rightRay = new Ray2D(new Vector2(transformStone.position.x + 1, transformStone.position.y), right);

RaycastHit2D[] rightHits = Physics2D.RaycastAll(rightRay.origin, rightRay.direction, 2);

foreach (RaycastHit2D rightHit in rightHits) {

if (gameObject.CompareTag("orange")) {

if (rightHit.collider.CompareTag("orange")) {

Debug.Log("HIT! OrangeRight");

rightHitsToWin++; } }

         if (gameObject.CompareTag("blue")) {
             if (rightHit.collider.CompareTag("blue")) {
                 Debug.Log("HIT! BlueRight");
                 rightHitsToWin++;
             }
         }
     }

     }

}

So, as you see, in my script I count every frame ++ one and the same object. How can I count one object only once?

Thanks in Advance :)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by villevli · Aug 28, 2016 at 12:54 PM

I made a version of your script that lists all objects that have the same tag as the object the script is attached to. When a raycast hits one of them, it's removed from the list so it's only counted once. (I'm not sure what that transformStone is supposed to be?)

 using System.Collections.Generic;
 
 public class RaycastWin : MonoBehaviour
 {
     HashSet<GameObject> requiredObjects;
     int rightHitsToWin = 1;
 
     void Start()
     {
         // Find objects with the same tag as this object
         requiredObjects = new HashSet<GameObject>(GameObject.FindGameObjectsWithTag(tag));
     }
 
     void Update()
     {
         var transformStone = transform;
 
         Vector2 right = transform.TransformDirection(Vector2.right) * 5;
         Ray2D rightRay = new Ray2D(new Vector2(transformStone.position.x + 1, transformStone.position.y), right);
         RaycastHit2D[] rightHits = Physics2D.RaycastAll(rightRay.origin, rightRay.direction, 2);
 
         foreach (RaycastHit2D rightHit in rightHits) {
             if (requiredObjects.Contains(rightHit.transform.gameObject)) {
                 Debug.Log("HIT! " + tag + "Right");
                 requiredObjects.Remove(rightHit.transform.gameObject);
                 rightHitsToWin++;
             }
         }
     }
 }
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 Tschlompf · Aug 28, 2016 at 06:19 PM 0
Share

Hi! Thanks for your answer. For one raycast that's working grate, but I forgot to mention that I have multiple raycasts. I have 4 raycasts, one in each direction, which are attached to my gameobject "stone". When I'm placing that stone, it should count the objects right, up, down and left of it. Those objects are stones too. There are blue types and orange types of them. I want, that something happens when an orange stone hits 3 other orange stones with it's raycast. Same thing with blue stones.

For that I wrote this script:

avatar image Tschlompf · Aug 28, 2016 at 06:20 PM 0
Share

public class RaycastWin : $$anonymous$$onoBehaviour { public int playerWon; HashSet requiredOrangeObjects; HashSet requiredBlueObjects;

 int rightHitsToWin = 1;
 int leftHitsToWin = 1;
 int upHitsToWin = 1;
 int downHitsToWin = 1;

 public Sprite player1Wins;
 public Sprite player2Wins;
 public GameObject playerWins;

 // Use this for initialization
 void Start() {
     
     requiredOrangeObjects = new HashSet<GameObject>(GameObject.FindGameObjectsWithTag("orange"));
     requiredBlueObjects = new HashSet<GameObject>(GameObject.FindGameObjectsWithTag("blue"));
     player1Wins = Resources.Load<Sprite>("Player1wins");
     player2Wins = Resources.Load<Sprite>("Player2wins");
 }


 // Update is called once per frame
 void Update() {
     var transformStone = GetComponent<Transform>();
     Vector2 right = transform.TransformDirection(Vector2.right) * 5;
     Vector2 left = transform.TransformDirection(Vector2.left) * 5;
     Vector2 up = transform.TransformDirection(Vector2.up) * 5;
     Vector2 down = transform.TransformDirection(Vector2.down) * 5;

     Ray2D leftRay = new Ray2D(new Vector2(transformStone.position.x - 1, transformStone.position.y), left);
     RaycastHit2D[] leftHits = Physics2D.RaycastAll(leftRay.origin, leftRay.direction, 2);

     Ray2D rightRay = new Ray2D(new Vector2(transformStone.position.x + 1, transformStone.position.y), right);
     RaycastHit2D[] rightHits = Physics2D.RaycastAll(rightRay.origin, rightRay.direction, 2);

     Ray2D upRay = new Ray2D(new Vector2(transformStone.position.x, transformStone.position.y + 1), up);
     RaycastHit2D[] upHits = Physics2D.RaycastAll(upRay.origin, upRay.direction, 2);

     Ray2D downRay = new Ray2D(new Vector2(transformStone.position.x, transformStone.position.y - 1), down);
     RaycastHit2D[] downHits = Physics2D.RaycastAll(downRay.origin, downRay.direction, 2);
avatar image Tschlompf · Aug 28, 2016 at 06:21 PM 0
Share
     foreach (RaycastHit2D rightHit in rightHits) {
         if (requiredOrangeObjects.Contains(rightHit.transform.gameObject)) {
             if (gameObject.CompareTag("orange")) {
                 Debug.Log("HIT! OrangeRight");
                 requiredOrangeObjects.Remove(rightHit.transform.gameObject);
                 rightHitsToWin++;
             }
         }
         if (requiredBlueObjects.Contains(rightHit.transform.gameObject)) {
             if (gameObject.CompareTag("blue")) {
                 Debug.Log("HIT! BlueRight");
                 requiredBlueObjects.Remove(rightHit.transform.gameObject);
                 rightHitsToWin++;
             }
         }
     }

     foreach (RaycastHit2D leftHit in leftHits) {
         if (requiredOrangeObjects.Contains(leftHit.transform.gameObject)) {
             if (gameObject.CompareTag("orange")) {
                 Debug.Log("HIT! OrangeLeft");
                 requiredOrangeObjects.Remove(leftHit.transform.gameObject);
                 leftHitsToWin++;
             }
         }

         if (requiredBlueObjects.Contains(leftHit.transform.gameObject)) {
             if (gameObject.CompareTag("blue")) {
                 Debug.Log("HIT! BlueLeft");
                 requiredBlueObjects.Remove(leftHit.transform.gameObject);
                 leftHitsToWin++;
             }
         }
     }
avatar image Tschlompf · Aug 28, 2016 at 06:22 PM 0
Share
     foreach (RaycastHit2D upHit in upHits) {
         if (requiredOrangeObjects.Contains(upHit.transform.gameObject)) {
             if (gameObject.CompareTag("orange")) {
                 Debug.Log("HIT! OrangeUp");
                 requiredOrangeObjects.Remove(upHit.transform.gameObject);
                 upHitsToWin++;
             }
         }

         if (requiredBlueObjects.Contains(upHit.transform.gameObject)) {
             if (gameObject.CompareTag("blue")) {
                 Debug.Log("HIT! BlueUp");
                 requiredBlueObjects.Remove(upHit.transform.gameObject);
                 upHitsToWin++;
             }
         }
     }

     foreach (RaycastHit2D downHit in downHits) {
         if (requiredOrangeObjects.Contains(downHit.transform.gameObject)) {
             if (gameObject.CompareTag("orange")) {
                 Debug.Log("HIT! OrangeDown");
                 requiredOrangeObjects.Remove(downHit.transform.gameObject);
                 downHitsToWin++;
             }
         }

         if (requiredBlueObjects.Contains(downHit.transform.gameObject)) {
             if (gameObject.CompareTag("blue")) {
                 Debug.Log("HIT! BlueDown");
                 requiredBlueObjects.Remove(downHit.transform.gameObject);
                 downHitsToWin++;
             }
         }
     }

     if ((rightHitsToWin == 4 || leftHitsToWin == 4 || upHitsToWin == 4 || downHitsToWin == 4) && gameObject.CompareTag("orange")) {
         if ((null == this || $$anonymous$$ath.Abs(GetComponent<Rigidbody2D>().velocity.y) <= 0.00001)) {

             Instantiate(playerWins, new Vector2(0, 0), Quaternion.identity);
             playerWins.GetComponent<SpriteRenderer>().sprite = player1Wins;
             print("Player 1 won!");
         }

         if ((rightHitsToWin == 4 || leftHitsToWin == 4 || upHitsToWin == 4 || downHitsToWin == 4) && gameObject.CompareTag("blue")) {
             if ((null == this || $$anonymous$$ath.Abs(GetComponent<Rigidbody2D>().velocity.y) <= 0.00001)) {
                 print("Player 2 won!");
                 Instantiate(playerWins, new Vector2(0, 0), Quaternion.identity);
                 playerWins.GetComponent<SpriteRenderer>().sprite = player2Wins;
                 
             }

         }

     }


 }

}

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Raycast Troubleshooting 1 Answer

Can't call raycast on plane to get mouse position? Beginner Question 1 Answer

Count objects in multiple raycasts 0 Answers

Raycasting else not working properly 0 Answers

How would I project a Gameobject onto the surface of another? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges