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

Rotating attached objects

Discussion in 'Physics' started by PANTSLORD, Sep 11, 2018.

  1. PANTSLORD

    PANTSLORD

    Joined:
    Jun 16, 2018
    Posts:
    14
    I am making a 2D game, and have a question about attaching objects to each other. I have a 3D cube in which I want to attach all of my two dimensional sprites to the surface of said cube. How would I attach the the sprites to one particular side of the cube? And if the cube were to be rotated, would the sprites stay on that particular side of the cube? Thanks in advance! :)
     
  2. StickyHoneybuns

    StickyHoneybuns

    Joined:
    Jan 16, 2018
    Posts:
    207
    I think the easiest way is to simply add empty gameobjects to the cube. This is how I spawn things onto my character for upgrades. Then you simply say sprite.transform.position=gameObject.position.//just an example, syntax may be incorrect which is why I did not add a code tag. You may have to adjust rotation but otherwise it should go directly to the location of the empty gameobject

    You could also just have each sprite spawn directly to the cube. The only issue with this is then you would have to offset the position by whatever side you would want it on. This would require more code then the first method but not a lot.
     
  3. PANTSLORD

    PANTSLORD

    Joined:
    Jun 16, 2018
    Posts:
    14
    So, uh, adding empty game objects could work, but I’m spawning between @500-2000 was objects on this cube within a span of about 2 or 3 minutes (approximate length of a level). It would get pretty confusing with large amounts of empty game objects, so I’m using instantiate to spawn them. I just need a way to attach them to a particular side of the cube, so that when the cube rotates, the 2D objects move along with side of the cube they’re attached to. Sorry for being a pain in the butt, but do you have any ideas on how to do this in particular? Or should I just have the 2D objects move separately from the cube, at the same speed and to the same position as the cube?
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,364
    If you have too many places to attach than you want to have Empty marks, then you will need to calculate the positions in either local or world space by a script. If local space, instantiate and set the parent, and then set the localPosition. Otherwise set the parent and set the regular position, in either order.

    It's definitely better to leverage the natural parent/child transforms for all those attached items. That's what it's for, and it's optimized behind the scenes as needed instead of in slow C# scripts every frame.