- Home /
Instantiating inside an area only.
Hi In my game players can instantiate objects, but they should only be able to do it inside of an area.
I only want to be able to instantiate objects inside of that box
I have tried using Vector3.distance, but that will only use a single int and not like a x and y distance.
Any suggestions?
Can the player only instantiate when there in the box, or do you mean that the player can only instantiate the item into the box?
sorry i forgot to say that i use Raycasting for instantiating the objects at mouse position. players should also be able to instantiate objects when standing outside of the area.
Answer by Justin Warner · May 22, 2011 at 01:26 AM
Get the coordinates of the corners, have the instantiate check before it actual instantiates if the X/Y/Z's are within bounds... We kinda did a project like this in CS class. But yeah, just check it before, if it passes all tests, than instantiate, if not, then ignore.
So, if spot to spawn is above this and is below this, and is on this side and this side of this of both the x and z, then spawn, else, dont do anything =).
if(X <= __ && Y <= __ && Z <= __ && X >= __ && Y >= __ && Z >= __)
{
//Spawn
}
Ok nice thanks, but is there any way to get the corner positions from a script? because in my game i will have many areas like this one in different sizes :)
Answer by Muuskii · Jul 21, 2012 at 01:44 PM
Try creating a trigger collider to define where you are allowed to instantiate objects inside and then in a script that has a reference to that collider use this:
if(myCollider.bounds.Contains(ray.GetPoint(hitdistance) ) )
{
//spawn
}
This works with box colliders, sphere colliders, etc, etc.