Search Unity

Is it Possible to Stop a Child Object Moving and Rotating like the Parent Object Does?

Discussion in 'Scripting' started by VAN-D00M, Jul 31, 2014.

  1. VAN-D00M

    VAN-D00M

    Joined:
    Dec 24, 2013
    Posts:
    44
    Hi

    This is my problem. I have an asteroid in my top down shooter which rotates randomly and moves randomly on the X and Z axis. Everything must stay at 0 on the Y axis. When my asteroid is destroyed, it spawns 4 more where it is destroyed. To achieve this I had to attach 4 empty game objects to spawn my other asteroids which I placed above, below, left and right of it (looking down onto the asteroid from above that is) (See screenshot). Screen Shot 2014-07-31 at 14.52.22.png

    At the moment when the asteroid rotates, the spawn points follow its rotation so when they do spawn they can be spawning off of 0 on the Y axis lets say something like -2 to 2.

    How can I make the child objects (spawn points) follow the asteroids position X and Z position without moving on the Y.

    I don't think I'm explaining it very well so bear with me. I have a screen shot to sort of explain the set up. I may be approaching this all wrong.

    Thanks for reading and I hope someone knows what I'm getting at. Cheers :)


    EDIT: The Spawn prefab with the game object placed at position 0, 0, 0 Screen Shot 2014-07-31 at 16.14.54.png
     
    Last edited: Jul 31, 2014
  2. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    670
    Easiest approach is to reset the y position each frame (e.g. in Update() ).
     
  3. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    579
    just make another prefab with 4 small ones and spawn it on big asteroid position,
    dont need to parent anything hierarchy is not for that.
     
  4. VAN-D00M

    VAN-D00M

    Joined:
    Dec 24, 2013
    Posts:
    44
    At the moment I have my Large Asteroid prefabbed with the 4 spawn positions attached to it and a small Asteroid prefab. Do you mean if I was to create an empty game object at 0, 0, 0 and have the 4 small asteroids placed on that, prefab it as 'Spawn' and then instantiate Spawn prefab when Large Asteroid gets destroyed? @pointcache
     
  5. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    579
    Or even better, one small stone is a prefab, and big one instantiates it 4 times offsetting direction vector to random value,
    then destroying himself.
     
  6. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    579
    Also small stone prefab can use random stone models when instantiating.
     
  7. VAN-D00M

    VAN-D00M

    Joined:
    Dec 24, 2013
    Posts:
    44
    Ahhhh, I like that idea.

    This is my instantiation code on the large Asteroid that is run when it detects a collision and I already have a small asteroid prefab. So to achieve that I would only need to change a few lines? Would you mind showing me an example of the sort of code you would us for that please. @pointcache

    Code (CSharp):
    1.  
    2.         Destroy (other.gameObject);
    3.         Destroy (gameObject);
    4.  
    5.         Instantiate(mAsteroid1, spawnUpper.position, transform.rotation);
    6.         Instantiate(mAsteroid1, spawnLower.position, transform.rotation);
    7.         Instantiate(mAsteroid1, spawnLeft.position, transform.rotation);
    8.         Instantiate(mAsteroid1, spawnRight.position, transform.rotation);
    9.  
     
  8. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    579
    Code (CSharp):
    1.    
    2.     void RektAsteroid(int HowManySmallToProduce, bool isRandom)
    3.     {
    4.         if (isRandom)
    5.         {
    6.             for (int i = 0; i < HowManySmallToProduce; i++)
    7.             {
    8.                 Vector3 dir = Random.insideUnitSphere;
    9.                 dir.y = 0;
    10.                 GameObject currentAsteroid = Instantiate(SmallAsteroid, BigAsteroid.transfor.postion, Quaternion.identity);
    11.                 currentAsteroid.transform.rotation = Quaternion.Euler(dir);
    12.                 //apply impulse here
    13.  
    14.             }
    15.         }
    16.  
    17.         else
    18.         {
    19.             //use your predefined values , say:
    20.             Vector3 up = Vector3.up; //and then kill the y
    21.             up.y = 0;
    22.         }
    23.     }
    i would do something like this

    i probably made a lot of mistakes there esp with euler/quternion but im showin the idea.
    I like this approach, when you are able to customize the outcome of whats going on, and substitute elements. Once written this "explosion spawner" method you can reuse it feeding
    him say scrap metal from ship explosions, etc etc.
     
  9. VAN-D00M

    VAN-D00M

    Joined:
    Dec 24, 2013
    Posts:
    44
    Crikey! I can just about keep up with that but I can see how efficient it is. I don't know if i'll be able to make that work but I will give it a go. Might take a while. Thanks for making that up for us. :)

    However, I did try the other idea about making a spawn prefab with asteroids in it (all at 0 on the Y). Took a screen shot of it (In the main description now) and the code that I am instantiating it with.

    Code (CSharp):
    1. Instantiate(mAsteroid1, other.transform.position, transform.rotation);
    (mAsteroid1 - holds my Spawn prefab)

    now to me that should work perfectly, but I get the same result. They are still being instated all over the Y axis and I don’t see how they can be. @pointcache
     
    Last edited: Jul 31, 2014
  10. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    579
    instead of directly using other object transform, put it into variable first, kill the y axis in it, and then use it for instantiation.
    by killing i mean = 0
     
  11. VAN-D00M

    VAN-D00M

    Joined:
    Dec 24, 2013
    Posts:
    44
    I would kill the Y by doing something like this right?

    Code (CSharp):
    1. currentPosition = transform.position;
    2. currentPosition = transform.position.y = 0.0f;
     
  12. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    579
    Code (CSharp):
    1. Vector3 store_position = transform.position;
    2. store_position.y = 0;
    3. instantiate(bla-bla, store_position)
    4.  
     
  13. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Couldn't you just make your asteroid object the child of an empty object... have the asteroid do its tumbling and such but the actual movement across the screen is done on the parent.... have your spawners children of that parent as well.
     
  14. VAN-D00M

    VAN-D00M

    Joined:
    Dec 24, 2013
    Posts:
    44
    Big Bang Theory moment! You must be looking at us like Penny does here. lol


    Cheers thats a simple way of doing it.
     
  15. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    heh I wasn't looking at you guys like that... just figured I'd throw up a solution I'd use since I didn't see it mentioned.
     
    VAN-D00M likes this.
  16. VAN-D00M

    VAN-D00M

    Joined:
    Dec 24, 2013
    Posts:
    44
    Well, I've made a parent object for the asteroid and the spawn points. I've ended up having to put all the scripts onto the main parenting object which works so far. The only snag is my RandomRotator script to make the asteroid tumble I have to put it on the asteroid child object. I'm starting to get the impression that scripts on child objects don't tend to work at all? @novashot
     
  17. VAN-D00M

    VAN-D00M

    Joined:
    Dec 24, 2013
    Posts:
    44
    Tell a lie. Rigidbody would help.

    Now thats rotating, the asteroid doesn't want to move with the parenting game object. The spawn points are running off and leaving the tumbling asteroid behind. One thing after another. :( No idea. @novashot

    No it makes no difference whether the asteroid is rotating or not. :(

    Answer = Cant have two rigidbodies spread over parenting and child objects.
     
    Last edited: Jul 31, 2014
  18. VAN-D00M

    VAN-D00M

    Joined:
    Dec 24, 2013
    Posts:
    44
    This way will work, I can't have two rigid bodies spread over parenting and chid objects. Is there a clever solution to this @pointcache. Sorry I keep discovering problems.
     
  19. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    What is the intended behavior... do your shots alter its trajectory? alter rotation?

    it's true you can have multiple rigid bodies put down, but they will be combined in the parent/child relationship... if the shots only alter the trajectory, have the rigid on the parent and just script a simple rotational effect for the asteroid object... if the shots are supposed to affect trajectory and rotation of the asteroid... it will get a bit trickier.
     
  20. VAN-D00M

    VAN-D00M

    Joined:
    Dec 24, 2013
    Posts:
    44
    The shots from the player are designed just to destroy. My RandomRotation script was on the main parenting object for spawn points and asteroid but that caused the spawn points to rotate with the asteroid. So I tried to put the RandomMover script on the main parenting object whilst the rotator script was on the child asteroid object. Both scripts use velocity and require the ridbody. Which doesn't work very well as either the astoroid will rotate and the spawn points will move off without the asteroid or the spawn points will move off and asteroid won't follow properly and just jerk around in the same spot. They don't combine very well. @novashot