- Home /
Checking for an object at a certain position
Hey, Im trying to make a procedural generation system with a pretty simple grid/tile pattern (2D). In this grid there can be different sprites but due to the fact that square tiled colliders often stop the player when moving I need the game to generate colliders by itself. This is ok, but to do that i need to be able to check where sprites start and end.
All methods for checking i know are using Physics, but since there are no colliders yet i cant do that.
Answer by Dreamora · Apr 29, 2017 at 07:42 PM
You can ask RectTransforms for the 2D objects actual world extension using RectTransform.GetWorldCorners
RectTransform also provides method for GetLocalCorners as well as the Rect property if you need to know it in local coordinates.
If you need complex colliders at this point, you could dive deeper into what Sprite offers you or alternatively use prefabs that are already fully setup and then use those prefabs in your procedural generator instead of trying to also create the 'blocks' in your procedural general procedurally themself.
how can i use the GetWorldCorners method? I can access the RectTrensform class but not the method... to do this should i be using a sprite's textureRect?
Answer by tMahon · Apr 29, 2017 at 10:12 PM
You can have the procedural creator make different instances of prefabs that have colliders on them already, you could just use an array of sprites and swap them out at random on an existing grid and do subsequent functions to edit components on a switch(case) for if the tile is ground, wall, item, enemy, etc. If the square colliders being included is causing player movement issues, consider using the colliders as Triggers. Or, depending on how precise your collision needs to be, you could just draw a Physics2D.CircleCastAll from the player or each tile. There's a number of different ways to tackle this, and it's going to depend on more of what you need for the project. There's a really good tutorial on here for a simple way to do procedural top-down 2D I'd recommend for a good starting point on the basics: https://unity3d.com/learn/tutorials/projects/2d-roguelike-tutorial
the problem with using prefabs with coliders is that the floor will be made of many square colliders... and this, due to some weird unity bugs, sometimes stops the player from moving.
Have you considered checking from the player's eye in real time? Do a raycast circle from the player and ask each object what its tag is. Here's an example looking for a wall tag.
Also note: When you instantiate the objects set their tags at the same time, and their colliders to Trigger and this should work. It eli$$anonymous$$ates your standard collision and replaces it manually, I'd recommend using a player controller script that moves transform.position manually and avoid a Rigidbody component altogether to cut down on Debugging code that could bog down movement as you scale.
// Use this for initialization
void Start () {
wallCollidingWith = null;
}
bool isCollidingWithWall;
Collider2D wallCollidingWith;
float xAxis$$anonymous$$ovement;
private void Update() {
if (!isCollidingWithWall)
{
//$$anonymous$$ove normally
}
else
{
//Detect wall position and freeze that axis
if (wallCollidingWith.transform.position.y == transform.position.y)
{
xAxis$$anonymous$$ovement = 0.0f;
}
}
}
//Call physics stuff in FixedUpdate
void FixedUpdate () {
//Cast a circle of 0.5 radius from player location and store all colliders
Collider2D[] colliders = Physics2D.OverlapCircleAll(point: transform.position, radius: 0.5f);
//Set the wall colliders to 'Trigger' to avoid unwanted collision with player collider
for (int i = 0; i < colliders.Length; i++)
{
//Check in the array for wall tags. $$anonymous$$ake sure to add something if you're intersecting 2 walls, this will ruin corners as is but you get the point
if (colliders[i].gameObject.CompareTag("Wall"))
{
wallCollidingWith = colliders[i];
}
}
}
Your answer
Follow this Question
Related Questions
Dialouge System [SOLVED] 1 Answer
The player randomly freezes in place while other objects move ingame 1 Answer
Bullet hit the collider of a child object 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers