Farhan Ali
Farhan Ali

Reputation: 509

Make child unaffected by parents rotation Unity

I have 2 gameobjects within my character say right_obj and left_obj, I want them to ignore parent rotation, like if player change its direction (left, right), these 2 gameobjects stay on their position and rotation, so far I use 2 approaches but fail. here is my code, I use this script on my two gameobjects, but they ignour this lines

// first one approch
Quaternion rotation;
void Awake()
{
    rotation = transform.rotation;
}

void LateUpdate () 
{
    transform.rotation = rotation;
}

// 2nd approch
void LateUpdate () 
{
    transform.rotation = Quaternion.identity;;
}

Upvotes: 10

Views: 50933

Answers (5)

姜金浩
姜金浩

Reputation: 126

If you really don't want destory parent-child hierarchy, you could rotate child every frame to counteract parent rotation. Let's say we have the follow scene,

enter image description here

Add the follow RestoreOriginRotation.cs to child object, we can get a stable child rotation.

enter image description here

What we can get.

enter image description here

****If you don't concern the underlying math, you chould stop here. ***

enter image description here

Upvotes: 5

Andrew Senner
Andrew Senner

Reputation: 2509

As @aaronrums stated, a Rotation Constraint is an easy way to achieve this. rotating player

Here, I have 3 game objects. The main parent object container ("Player Seat") which holds all the children will always be set to a rotation of 0 on the Z axis. The GO I'm trying to keep positioned vertically (so the text and avatar doesn't rotate) is "DrewPlayer." I have it offset on the X-axis by 100 in its parent "Player Position." I added a rotation constraint on the item I didn't want to rotate. (a.k.a "DrewPlayer") Once the constraint was on it, I dragged over the "Player Seat" to the source list and when I rotated "Player Seat," I was able to achieve the desired effect in the gif below.

Thanks @aaronrums! Saved me some effort here. :)

Upvotes: 4

aaronrums
aaronrums

Reputation: 67

You could also use a Rotation Constraint component. This is similar to the Parent Constraint mentioned above, but it's simpler to understand in my opinion - it just uses the Rotation and only the rotation of whichever GameObjects you slot in.

You just have to make an empty game object and set the rotation to 0,0,0 , then set this empty game object as the source of the rotation constraint. I have this exact setup in my game, and it overrides the rotation of the actual parent, which is what I want.

Perhaps not the absolute cleanest solution, but it's something to consider.

Here is the docs link - https://docs.unity3d.com/Manual/class-RotationConstraint.html

Upvotes: 6

Neph
Neph

Reputation: 2001

LeviathanCode's "Parent Constraints" solution only works with Unity 2018.1+ because it's a new addition.

My suggestion isn't a "pretty" solution but it'll also work with older versions if you want to prevent imitating rotations while still copying movement:

Get rid of the parent/child relationship, add a new parent GameObject (as LeviathanCode suggested) and a script to it that simply copies the player's movement each frame:

public GameObject player; //Drag the "player" GO here in the Inspector    

public void LateUpdate() {
    transform.position = player.transform.position;
}

This way your "right_obj" and "left_obj" won't rotate like the player but still move "with" it.

Upvotes: 3

Leviathan
Leviathan

Reputation: 179

Do you really need this type of relationship?
If you don't really need the father/child relationship you can spawn an empty GameObject at 0,0,0 and spawn the actual parent and child as childs of the GameObject in the middle of the scene, this way they would still be grouped in some sort of relationship but instead of a father/child you'd get a brother/brother relationship, making the first independent from the second.

If you need to make two objects dependent from each other but not in every single way as it's supposed to be in father/child try using Parent Constraints

This way you'll obtain this type of hierarchy:
- Father GO (at 0,0,0)
-- Child1 (your actual father)
-- Child2 (your actual child)

Child1 and Child2 are syncing position by their ParentConstraint but not their rotation.

Else there is no other way than applying the opposite rotation to the child.

Upvotes: 13

Related Questions