Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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 emir_ulusoy_49 · May 26 at 04:34 PM · collision

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

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · 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
0

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

Comment
Add comment · Show 3 · 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 emir_ulusoy_49 · May 28 at 06:05 PM 0
Share

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

avatar image WolverineLight · May 30 at 04:41 PM 0
Share

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.

avatar image emir_ulusoy_49 · 5 days ago 0
Share

@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 :)

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

255 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My main character isn't being able to colide with the objects. 1 Answer

Is there any good sample code for shooting a projectile and collision detection? 0 Answers

Physics object bouncing on collision. 0 Answers

Why is Collider.IsTouching not working in this sample? 0 Answers

Trying to get Points to accumulate in game 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