Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

How to make something spawn infront of child object

Discussion in 'Getting Started' started by lastnoob765, May 18, 2021.

  1. lastnoob765

    lastnoob765

    Joined:
    Aug 3, 2020
    Posts:
    2
    so i have an child object and only way i can spawn something close to it is by using:

    Code (CSharp):
    1. Vector3 spawnPos = new Vector3(barrel.transform.position.x, barrel.transform.position.y, barrel.transform.position.z);
    and if i use

    Code (CSharp):
    1. Vector3 spawnPos = barrel.transform.right * 1f + barrel.transform.up * 1f + barrel.transform.forward * 2f;
    it spawn on global world coordinates bc child object has tranform.position = (0,0,0) but the parent is on like(123,4,212)
    so when i add transform.forward it just adds the 0,0,1 to the 0,0,0 and spawns the object at 0,0,1 on global coordinates.
    So how do i spawn a gameobject infront of my child object no matter where it is
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    1,985
    Use the first method, but just add values to the object's position.
    The following example will create a spawn position of the child's position plus 1 unit in each axis:
    Code (CSharp):
    1. Vector3 spawnPos = new Vector3(barrel.transform.position.x + 1, barrel.transform.position.y + 1, barrel.transform.position.z + 1);
     
    Joe-Censored likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,760
    For something like a muzzle flash or bullet spawn point, I usually place an empty GameObject as a child of the gun object, for use as the spawn point.
     
    stain2319 and Vryken like this.
  4. lastnoob765

    lastnoob765

    Joined:
    Aug 3, 2020
    Posts:
    2
    Tnx,i was thinking about a code solution but this is way easier.
     
    Joe-Censored likes this.
unityunity