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 · Sep 04, 2016 at 09:54 AM · raycastraycasting

Count objects in multiple raycasts

Hi! Some days ago I asked a question, how to count objects in a raycast, and got a pretty well working answer. Now I want to count the objects of each raycast individually. That's what I do: I can place a "stone" by clicking on something. This stone can be an "orange Stone", or a "blue Stone", which is defined by tags. When I am clicking next time, the next stone will be placed. Each of this stones has 4 raycasts on it. Every raycast should count the objects indivually, so when one of those stones has counted for example 3 objects of the tag "orange" in one of his raycast and is an orange Stone himself, something happens. For solving this problem in one raycast, I make 2 new Hashsets, one for the orange Stones and one for the blue stones. But with multiple raycasts in one stone, it gets very weird, and works only sometimes. The following script is in every stone placed by mouseclick:

public int playerWon; HashSet requiredOrangeObjects; HashSet requiredBlueObjects; public Boolean playerWinsIsCreated; 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);


     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++;
             }
         }
     }

     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 || Math.Abs(GetComponent<Rigidbody2D>().velocity.y) <= 0.00001)) {
             if (!playerWinsIsCreated) {
                 Instantiate(playerWins, new Vector2(0, 0), Quaternion.identity);
                 playerWins.GetComponent<SpriteRenderer>().sprite = player1Wins;
                 print("Player 1 won!");
                 playerWinsIsCreated = true;
             }
             
         }

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

         }

     }


 }

}

With this script, player one can win for example, when there are only 3 stones a row and one stone on one of the stones in that row. I think, there might a problem with the hashSets, but I really don't know what I have to change so it works. I hope you understood my problem :)

Thanks in advance!

Comment
Add comment · Show 1
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 · Sep 06, 2016 at 08:15 PM 0
Share

Ok, I found a solution. I just splitted the 4 raycasts in 4 different scripts and put each one in the stone, and now it works fantasticly :)

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

raycast bug when hitting two adjacent objects 0 Answers

Raycast line renderer not drawing correctly 0 Answers

Raycast objects with a tag C# 0 Answers

Raycast on left side and right side of the player 1 Answer

Raycast hit 2 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