Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Instantiating only inside an area.

Discussion in 'Scripting' started by zhapness, May 22, 2011.

  1. zhapness

    zhapness

    Joined:
    Apr 10, 2011
    Posts:
    58
    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?
     
  2. alexfeature

    alexfeature

    Joined:
    Apr 1, 2010
    Posts:
    132
    Hey,

    First of all, love the pic :D

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

    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 :)


    I hope this helps.
     
  3. zhapness

    zhapness

    Joined:
    Apr 10, 2011
    Posts:
    58
    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 :)
     
  4. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Last edited: May 22, 2011
  5. csprason

    csprason

    Joined:
    Apr 29, 2011
    Posts:
    5
    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.
     
  6. zhapness

    zhapness

    Joined:
    Apr 10, 2011
    Posts:
    58
    Thanks I found a solution :)
     
    Last edited: Jul 6, 2011
  7. xpxilom

    xpxilom

    Joined:
    Aug 28, 2014
    Posts:
    30
    [QUOTE = "zhapness, post: 585198, miembro: 41666"] Gracias encontré una solución :)[/ QUOTE]
    Cual fue? Estoy necesitando esto =(
     
  8. zafranalon

    zafranalon

    Joined:
    Dec 22, 2020
    Posts:
    9
    how
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    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/ass...3-syntax-error-expected.1039702/#post-6730855

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

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

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

    Imphenzia / imphenzia - super-basic Unity tutorial:



    Brackeys super-basic Unity Tutorial series:



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