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!
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 :)
Your answer
Follow this Question
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