Instantiating only inside an area.

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?

Hey,

First of all, love the pic :smile:

As for your problem. Since you are instantiating stuff and that takes a position param all you really need is a position :slight_smile:

So what you could do:

  1. While in the designer, take an empty GameObject and put it in the center of the area around which objects may get instantiated;
  2. Assign a Collider and Rigidbody component to it;
  3. Set rigidbody to be kinematic and the collider to be a Trigger;
  4. Size the collider so that its area suits your needs;
  5. Attach a script to your player that would check when its collider enters the Trigger area of your box ( basically do stuff inside the OnTriggerEnter() method in your player’s script);
  6. Choose a random position vector within the box to instantiate your objects;

This doesn’t provide much coding help but it should give you enough info to ask more interesting questions :slight_smile:

I hope this helps.

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.
so I dont think your suggestion will help in that case.
But thanks anyway :slight_smile:

Use bounds for area, and bounds for each object.

To place an object, be sure every point of it’s bounds is within the spawn bounds.

That’s a general idea - Impl. is up to you.

If the player’s position is irrelivant, just check the mouse position and if its within bounds, set a boolean to true. if its true, the player can instantiate, false, they cannot.

Thanks I found a solution :slight_smile:

how

Are you seriously replying to a ten year old thread with one word?! Really!?

Please don’t do that. It’s against forum rules to hijack unrelated threads in any case.

Please start your own new thread. It’s FREE!

Remember that nobody here can read your mind so you need to communicate clearly.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

How to understand compiler and other errors and even fix them yourself:

https://forum.unity.com/threads/assets-mouselook-cs-29-62-error-cs1003-syntax-error-expected.1039702/#post-6730855

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

If you have absolutely no idea what to do, start with some tutorials, such as these:

Imphenzia / imphenzia - super-basic Unity tutorial:

https://www.youtube.com/watch?v=pwZpJzpE2lQ

Brackeys super-basic Unity Tutorial series:

https://www.youtube.com/watch?v=IlKaB1etrik

Sebastian Lague Intro to Game Development with Unity and C#:

https://www.youtube.com/watch?v=_cCGBMmMOFw