- Home /
How to check if there´s an object at position
Hi, I have this code:
function OnCollisionEnter(collision : Collision)
{
Instantiate (chunk, Vector3(transform.position.x,transform.position.y ,transform.position.z + 7),Quaternion.identity) ;
Instantiate (chunk, Vector3(transform.position.x,transform.position.y ,transform.position.z - 7),Quaternion.identity) ;
Instantiate (chunk, Vector3(transform.position.x+7,transform.position.y ,transform.position.z ),Quaternion.identity) ;
Instantiate (chunk, Vector3(transform.position.x-7,transform.position.y ,transform.position.z ),Quaternion.identity) ;
}
This creates an object every time i collide with an object like it in 4 different positions, the problem is that when i collide with an object that has already an object next to it it creates an object again in the same place so this causes the game to freeze.
I need a code to check if there´s an object at a position and if there´s not then execute my code. Hope you can help me
I just formatted your code (and fixed the title!). You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window =]
Answer by Henrik Poulsen · Nov 11, 2012 at 02:46 PM
I would say that there are multiple ways of solving this. One would be this, though that isn't the solution I would normally use. If your chunks have colliders I would probably just go for using a Raycast and sending a ray in each direction to see if it hits an already pre-existing chunk (or any other obstacle really) with a layer mask.
Thank´s for the help, I´ve managed to make it not create a chunk if there´s one already at the position but another problema came up, every time i Instantiate an object the game lows it´s FPS, a lot and stays at that FPS rate, can you help me with that ?