Search Unity

Instantiate prefab towards object

Discussion in 'Scripting' started by ali31, Apr 30, 2012.

  1. ali31

    ali31

    Joined:
    Mar 30, 2011
    Posts:
    129
    I'm trying to instantiate a prefab in the direction of an object the player will click, is there a simple way to achieve this that I am missing? Currently I have a very crude method in mind and would rather not use that unless its my only option.

    Any help is appreciated, thanks in advance.
     
  2. Bluntweapon

    Bluntweapon

    Joined:
    Feb 24, 2012
    Posts:
    158
    What do you mean in the direction of...?

    Also it's probably prudent to write some code and test it first before asking for help.
     
  3. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Well, you could instantiate an empty gameObject where the payer clicked, then have the other instantiated Obj look at it on start.

    All methods for this have examples in the doc.(just incase you don't already know lol)
     
  4. Zethariel1

    Zethariel1

    Joined:
    Mar 21, 2012
    Posts:
    439
    In the instantiate, you can try and alter the Quaternion if you feel brave.

    I'd suggest however instantiating the pre-fab according to the pseudo-code
    Code (csharp):
    1.  
    2. Transform prefab;
    3. prefab = (Transform) Instantiate (prefab with position and stuff);
    4. prefab.LookAt(the target your prefab must look at);
    5. // You might have to lock the z or x rotation so that the object does not look directly at the base of the object it is supposed to look at. It can look pretty silly if it is a character.
    6.  
     
  5. Bluntweapon

    Bluntweapon

    Joined:
    Feb 24, 2012
    Posts:
    158
    LOL that's awesome. I like your style.
     
  6. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Code (csharp):
    1. Instantiate (prefabReference, spawnPosition, Quaternion.LookRotation (lookTarget.transform.position - spawnPosition));
     
    ssojyeti2 likes this.
  7. ali31

    ali31

    Joined:
    Mar 30, 2011
    Posts:
    129
    My apologies, I guess I didn't explain this properly.

    I'm trying to shoot "bullet" prefabs in the direction of an enemy object the user will click in screen space. The enemy object is moving towards the player so it's important that the bullet travel fast enough, and in the proper direction to make contact before the enemy has moved. One more thing is the player object shooting the bullet prefabs is stationary and will not turn to face the enemies, it will simply shoot in their direction.

    I hope that makes sense.
     
  8. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    I think you'll need to be more clear about exactly which bits you're asking about. You've got the directional instantiate question answered.
     
  9. ssojyeti2

    ssojyeti2

    Joined:
    Mar 16, 2020
    Posts:
    16
    this was what I needed, thanks!