Collision with Vector2int
Hello
So im trying to make a collision with vector2int, i have tried to do some research on this or think of ideas on making colliders with vector2int but i havent found one
Answer by WolverineLight · May 27 at 03:28 PM
Hello emir_ulusoy_49,
Could you please be more specific as to what your goals are? - as in, what would you like to "collide" with this Vector2int? I'm also assuming you are trying to check for collision every frame or so? The more information you can provide, the better.
Answer by emir_ulusoy_49 · May 28 at 06:01 PM
Hi @WolverineLight Thank you for answering
Well my goal is to make an algorithm pathfinding project and im making use of generating grids (and im generating the grid with 1 single sprite that is going to be also 4 other sprites), that is Ai (thats gonna be the pathfinding) and Button ( thats gonna be pressed, and when its being pressed a door opens), Door is the end of the game ( basically the goal), then a Chest ( that is gonna be used for pusing it to button to open the door).
the problem is, is that im using Vector2Int fields, but i cant use Vector2Int in OnTriggerEnter2D method or i cant assign rigidbody or box collider seperatly (so i cant for exemple assign to the chest a box collider then the ai to have collider and then rigidbody) because im using 1 sprite for all of them.
here is some code, so it can be easier to understand hopefully (this is the fields im using)
public CellType[] grid; // 2D array list, this generates the amount of elements public int gridWidth; // This divides the amount of number it is type in public int gridHeight => grid.Length / gridWidth; //Calulates the height of the grid public Vector2Int Player; // Player position in 2D public Vector2Int Enemy; // Enemy position in 2D public Vector2Int GoalDoor; // Goal position in 2D public Vector2Int Button; public Vector2Int Chest; public bool playerTurn; // This checks if its player turn or not public bool hasPressedbutton;
here is a script for in game using System; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; using UnityEngine.UI; using Object = UnityEngine.Object; [Serializable] public class GameView : MonoBehaviour { public BoardPiece BoardPiecesPrefab; private List<BoardPiece> boardPieces = new List<BoardPiece>(); //Created a list of boardPieces private State sttate; public void UpdateView(State state) { foreach (var boardPieces in this.boardPieces) //Destroyes old pieces? { Destroy(boardPieces.gameObject); } boardPieces.Clear(); for (int i = 0; i < state.gridWidth; i++) { for (int j = 0; j < state.gridHeight; j++) { var boardPiece = Instantiate(BoardPiecesPrefab, new Vector3(i, j), Quaternion.identity); boardPiece.sprite.color = Color.green; boardPieces.Add(boardPiece); } } var boardEnemy = Instantiate(BoardPiecesPrefab, new Vector3(state.Enemy.x, state.Enemy.y, -0.01f), Quaternion.identity); //Creates Enemey boardEnemy.sprite.color = Color.red; boardPieces.Add(boardEnemy); var boardPlayer = Instantiate(BoardPiecesPrefab, new Vector3(state.Player.x, state.Player.y, -0.01f), Quaternion.identity); //Creates Player boardPlayer.sprite.color = Color.cyan; boardPieces.Add(boardPlayer); var Button = Instantiate(BoardPiecesPrefab, new Vector3(state.Button.x, state.Button.y, -0.01f), Quaternion.identity); // Creates Goal Button.sprite.color = Color.yellow; boardPieces.Add(Button); var Goal = Instantiate(BoardPiecesPrefab, new Vector3(state.GoalDoor.x, state.GoalDoor.y, -0.01f), Quaternion.identity); // Creates Goal Goal.sprite.color = Color.magenta; boardPieces.Add(Goal); var chest = Instantiate(BoardPiecesPrefab, new Vector3(state.Chest.x, state.Chest.y, -0.01f), Quaternion.identity); // Creates Goal chest.SpriteChest.color = Color.blue; boardPieces.Add(chest); } }
i tried to get a insert a image so it can be more clearify on how the game looks but idk how this site works
oh wow this really do look pretty bad
GameView.cs (what it shows in game) https://gdl.space/ajihifubif.cs
This is a state class https://gdl.space/eraqurokep.cs
I take it that you are looking for a way to tell whether or not certain objects interact on the same "Cell Type" in a grid, sort of like checkers or chess.
In this case, if you want to run a script for ONLY THE PLAYER's collision, every time the game is updated or a cell changed, you must run a custom collision script of your own. For this methodology, simply follow these steps: A. Get the Player's location in coordinates, B. Check the list of "States" or "cells" at the player's index/coordinates -> make sure you check to see if the collision is with an enemy or chest, or anything that the player can "collide" with.
If you want to have collision detection for MORE THAN just the player, you may want to run the same code/logic, but with these other entities.
If you have A LOT of collisions, I would suggest using a for loop to iterate through the grid to detect collisions.
Also, your "GameView" class is very, very inefficient. There is no need to Destroy and Instantiate objects every frame, and unfortunately these two methods (especially instantiate) are very, very slow in Unity. Instead, simply crate the cells as you have done, but make the "pieces" on the board their own Game Object. Simply move them as needed - no need to Destroy every piece on the board every frame only to recreate them.
Hope this helps.
@WolverineLight sorry for late answer. Should i create seperate gameobject for the objects i wanna use instead of generating grids with 1 piece instead? is it better to remake the whole "gameview" class? Thank you for answering :)