- Home /
GameObjects creation within boundry
For my 2d game, I want to certain game objects with certain area at random position. For this I have added following type of calculation.
float xPosition = Random.Range (minHorizontalRange, maxHorizontalRange);
float yPosition = Random.Range (minVerticleRange, maxVerticleRange);
And it got generated at random position. But at this point I want some game objects get overlapped by other one. So I want to remove this problem for my game.
For this I have added following code
void OnTriggerEnter2D (Collider2D other)
{
if (!other.CompareTag (Constants.TAG_BALL)) {
// place game object at some other position
}
}
But overlapped object don't detect OnTriggerEnter2D() method gets called.
So basically here I define my requirements and problem, provide some help in this. If you have any better algorithm for this then please suggest me.
$$anonymous$$y inspector window for each created game object.
Answer by siddharth3322 · May 29, 2014 at 11:32 AM
I can able to solve this problem using other way. OnCollisionEnter2D() and OnTriggerEnter2D() methods didn't called at all.
I used Physics2D.OverlapCircleAll() method to solve this problem. Here is my code snippet that work for me and thanks for you suggestions.
private void CheckForOverlap (GameObject holeInstance)
{
Collider2D [] otherColliders = Physics2D.OverlapCircleAll (new Vector2 (holeInstance.transform.position.x, holeInstance.transform.position.y), 1.5f);
if (otherColliders.Length > 1) {
float xPosition = Random.Range (minHorizontalRange, maxHorizontalRange);
float yPosition = Random.Range (minVerticleRange, maxVerticleRange);
float zPosition = -1f;
Vector3 holePosition = new Vector3 (xPosition, yPosition, zPosition);
holeInstance.transform.position = holePosition;
CheckForOverlap (holeInstance);
}
}
Answer by wijesijp · May 29, 2014 at 06:31 AM
after getting the position do a test to see whether there are any objects within range You can use Physics2D.OverlapCircle to test
But this is a costly operation.
I can't able to understand why my OnTriggerEnter2d() method not called? Any idea you have. Then I don't have to perform any extra stuff.
You do have rigid body 2D added to your game objects right?
anyway you need to do some test before spawning right? Even if OnTriggerEnter2D worked what are you going to do now? Objects are created and they are overlapping ! Are you going to move them or delete them?
GameObject has rigidbody component attached already. I want to move same object at other position.
I think the problem may be
if (!other.CompareTag (Constants.TAG_BALL))
it is saying if compare tag fails do something. Just a guess I don't know how you setup your objects
Answer by Noob_Vulcan · May 29, 2014 at 06:41 AM
Change OnTriggerEnter2D()
to OnTriggerStay2D()
. Try might work
I have already tested OnTriggerStay2D but didn't worked.
k.. I think non of the things will get called trigger/collision . Since your objects spawn inside other (half or full). You will have to manage via code. As far as i know
Your answer
Follow this Question
Related Questions
At same positioned game objects collision detection 0 Answers
Game Development Approach 0 Answers
Access Prefab Object Via Scirpt 2 Answers
Game AI Implementation 0 Answers
Object dragging at manual point 2 Answers