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

How to rotate a child transform independent of parent?

Discussion in 'Scripting' started by Velo222, Jan 19, 2014.

  1. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    I know this has been asked quite a bit, but I'm also trying to think of different ways to accomplish what I'm looking for.

    In my game I'm rotating a unit (a melee unit lets say) whenever they get in range of an enemy unit for an attack animation, so that the unit is facing it's enemy at all times. The problem is, the collider on my unit keeps the unit from rotating to the proper direction when there are other units around it (I havn't figured out why this is the case, other than the colliders are blocking each other and keeping the others from moving/rotating correctly).

    So, ideally, I'd like to have just the object's renderers rotate, but NOT the object's collider. This is proving harder than it seems. If I make all of the renderers children of an empty game object, and then try to grab the renderer's transforms only and rotate them, they will not rotate -- they always seem to keep the parents rotation, no matter what.

    I've gone so far as to instantiate a collider independent of the renderers themselves and set the colliders position to the renderers every update, but the colliders won't collide for some reason when I tried this. But that's a separate problem in and of itself.

    Is there a way to force a child object to rotate in a different direction than its parent object?
     
  2. hektac

    hektac

    Joined:
    Nov 9, 2012
    Posts:
    28
    apotema likes this.
  3. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Ya I've tried that hektac, but I found out that if I make a script and put it directly on the child object, and then set its transform.localRotation every update, that it actually does rotate it independently of the parent. However, I've only tested this on one child so far, and I'm curious to see how it behaves when there are child's of children even.

    It's kind of confusing to me though why when I grabbed the child object via reference from a script on the parent, and then tried to rotate the child object, it wouldn't rotate? My guess is it might have been something to do with order of operation, or maybe even order of script execution......

    Well, I have some more testing to do, thanks for the suggestions hektac.
     
  4. MDragon

    MDragon

    Joined:
    Dec 26, 2013
    Posts:
    329
    You can set order of script execution (basically adding weights to individual scripts) in Edit --> Project Settings --> Script Execution Order.

    Also, I haven't tested this yet but may soon:
    http://docs.unity3d.com/Documentation/ScriptReference/Transform.GetChild.html

    And you can also do transform.parent to get the parent, so if you want, you can even put the whole script on the child.
     
  5. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Okay, I think I found my problem.

    I have the parent object with the "Animator" component on it. I also have 6-7 child objects of this parent object that all have skinned mesh renderers on them. I want to rotate the child objects with the skinned mesh renderers on them (all of them to the same direction/angle) while the unit is "attacking".

    I have a feeling the child objects won't rotate at all because they are skinned? So they only obey the bones? Can anyone confirm/deny this for me?

    First of all, I have no idea why these child objects (they're little things like a horses saddle, reins, armor, base body, etc...) were skinned, instead of just made to be regular un-skinned objects -- whoever made the model did it like this. Secondly though, other than creating new animations in a separate 3d modeling program, I have no idea how to rotate child objects with skinned meshes.

    This is frustrating to say the least. All I want is the horse to face a certain direction independent of its collider while it's attacking.
     
  6. Kiino

    Kiino

    Joined:
    Jul 15, 2014
    Posts:
    1
    Have you tried setting all but the parent object as kinematic? That way the parent is the only thing thats physical while the rest are there for animations.
     
    Swiss-Miss likes this.