Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

question abous instantiate?

Discussion in 'Scripting' started by enzoravo, May 15, 2020.

  1. enzoravo

    enzoravo

    Joined:
    Jun 25, 2013
    Posts:
    284
    hello, i'm back after two years, starting a new game project, last unity version i had used was unity 2016.

    now i'm using unity 2019 and i'm learning about the changes made in unity in these two years.

    i'm facing an issue, maybe a newbie one, i'm trying to instantiate a bullet at some empty child from my prefab gameobject transform, but when i move my master object and try to instantiate the bullet again it keep instantiate at 0,0,0

    these is the code i used to instantiate my bullet

    any help will be very useful

    thanks in advance for all your help.

    Code (csharp):
    1.  
    2. if (Time.time > delay)
    3.             {
    4.                 Rigidbody bullet = (Rigidbody)Instantiate(BulletPrefab, WeaponFireLocation.transform.localPosition, Quaternion.identity);
    5.                 bullet.AddForce(WeaponFireLocation.forward * 1000);
    6.                 delay = Time.time + attack_rate;
    7.             }
    8.  
    p.s.: all variables and object are valid, not empty or null variables are in the code.
     
  2. kburkhart84

    kburkhart84

    Joined:
    Apr 28, 2012
    Posts:
    910
    1. This is the wrong place for questions as far as forum placement.

    2. I think you need to read up on the difference between world and local coordinates. You are instantiating the bullet at the "local" position of a transform....but the instantiate function needs world coordinates. You can do a conversion, but I don't think you need that in this case since you have access to the object. You can directly grab the world coordinates.
     
    angrypenguin likes this.
  3. enzoravo

    enzoravo

    Joined:
    Jun 25, 2013
    Posts:
    284
    sorry for posting in the wrong section, i will post in the right place next time.

    thanks for the info, i will try that!
     
  4. ChazBass

    ChazBass

    Joined:
    Jul 14, 2013
    Posts:
    153
    WeaponFireLocation.transform.position