Search Unity

Instantiated object(child) to move according to Instantiated ParentObject's rotation.

Discussion in 'Scripting' started by Ki4Chan, Sep 14, 2017.

  1. Ki4Chan

    Ki4Chan

    Joined:
    Jul 2, 2017
    Posts:
    24
    I have an Object(parent) having 4 spawn points around it which spawns stars. In the game, Parent object rotates but the Spawned stars don't move according to Parent objects rotation. I have tried all the suggestions people have given in the forum.

    Code (CSharp):
    1. public GameObject starPrefab;
    2.         private GameObject newStarPrefab;
    3.         public Transform[] starSpawnPoints;
    4.  
    5.         void Start () {
    6.             if (!GameObject.FindGameObjectWithTag("Box"))
    7.             {
    8.                     RandomPoint();
    9.             }
    10.         }
    11.  
    12.         void RandomPoint()
    13.         {
    14.             int randomIndex = Random.Range(0, starSpawnPoints.Length);
    15.  
    16.             for (int i = 0; i < starSpawnPoints.Length; i++)
    17.             {
    18.                 if (randomIndex == i)
    19.                 {
    20.                     newStarPrefab =  Instantiate(starPrefab, starSpawnPoints[i].position, Quaternion.identity);
    21.                     newStarPrefab.transform.parent = gameObject.transform;
    22.                 }
    23.             }
    24.         }
    I have added this script to the Parent object which rotates after spawning.
     
  2. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    The spawner stars are Particle Effects?
    If so, have you checked the "Simulation Space" as "Local", instead "World"?
     
  3. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    And why you do this instead this?
    Code (CSharp):
    1. void RandomPoint()
    2.         {
    3.             int randomIndex = Random.Range(0, starSpawnPoints.Length);
    4.  
    5.                     newStarPrefab =  Instantiate(starPrefab, starSpawnPoints[randomIndex ].position, Quaternion.identity);
    6.                     newStarPrefab.transform.parent = gameObject.transform;
    7.  
    8.         }
     
  4. Ki4Chan

    Ki4Chan

    Joined:
    Jul 2, 2017
    Posts:
    24
    no, Stars are not particle effects.
     
  5. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    mhhh... maybe they have a RigidBody? In that case they follow the phisics instead parents
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
    that code works here (and they rotate with parent).. so what is your starPrefab like?
     
  7. Ki4Chan

    Ki4Chan

    Joined:
    Jul 2, 2017
    Posts:
    24
    it's a normal 2D sprite. It does not have any other component yet.
     
  8. Ki4Chan

    Ki4Chan

    Joined:
    Jul 2, 2017
    Posts:
    24
    No they do not have any additional component yet.
     
  9. Ki4Chan

    Ki4Chan

    Joined:
    Jul 2, 2017
    Posts:
    24
    I have used "randomIndex != 1" In my previous project. I just made little changes in new one for my future convenience. :)
    Code (CSharp):
    1.  void RandomPoint()
    2.         {
    3.             int randomIndex = Random.Range(0, starSpawnPoints.Length);
    4.             for (int i = 0; i < starSpawnPoints.Length; i++)
    5.             {
    6.                 if (randomIndex != i)
    7.                 {
    8.                     newStarPrefab =  Instantiate(starPrefab, starSpawnPoints[i].position, Quaternion.identity);
    9.                     newStarPrefab.transform.parent = gameObject.transform;
    10.                 }
    11.             }
     
  10. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    You move the 2D Sprite? maybe some animation?
     
  11. Ki4Chan

    Ki4Chan

    Joined:
    Jul 2, 2017
    Posts:
    24
    I closed Unity opened it again, now it's working. Same script, nothing changed.
     
    RoMax92 likes this.
  12. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    LOL, the programmer method works always,
    see you soon