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 :)
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++;
}
}
}
}
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:
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);
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 || $$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
![](https://koobas.hobune.stream/wayback/20220612091708im_/https://answers.unity.com/themes/thub/images/avi.jpg)