2
\$\begingroup\$

I have a hierarchy like this

Body
  - Head
  - Arms

Currently this works fine in that the Body's transformation is reacting as it should. Being children, the Head and Arms match the Body's transform. Is it possible however to separate just the rotation transform? I would like the head to translate with the body but not necessarily rotate the same way.

I tried using Transform.DetachChildren but that removes the translate transform which is a bit of an issue as I move the object using physics so there's a chance the head and arms would react differently and physically separate from the body.

Thanks

\$\endgroup\$
2
\$\begingroup\$

This is a script that keeps the rotation of the head to the rotation you set in the script, however it is very simple and may need adjustment to your purpose.(I have included sliders for easy editing). I hope this answers your question.

using UnityEngine;
using System.Collections;

[ExecuteInEditMode] // This allows it to run during editmode(Makes it easier to work with)
public class KeepHeadRotation : MonoBehaviour 
{
Quaternion rotation;
Quaternion newRotation;

[Range(-90,90)]
public float X = 0, Y = 0, Z = 0, W = 0;

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

void Update()
{
    newRotation = new Quaternion(rotation.x - X, rotation.y - Y, rotation.z - Z, rotation.w - W);
    transform.rotation = newRotation;
}
}

It works by storing the rotation of the object at the beginning and fixing it to that rotation and the sliders affect the rotation by either adding or subtracting from it allowing you to change it via the script.

Here is the link to the Quaternion page on the Unity API: http://docs.unity3d.com/ScriptReference/Quaternion.html

\$\endgroup\$
  • \$\begingroup\$ Rotation quaternions are typically of unit length, so I'm not sure why you're bounding X, Y, Z, and W in the range -90...90. It looks like you might be getting confused with Euler angles. \$\endgroup\$ – DMGregory Jun 14 '15 at 21:01
  • \$\begingroup\$ My bad, yeah it should be a lower range than that. I am getting mixed up with Euler angles. In which case it may not be so easy to intuitively change the angles. you could use Quaternion.euler to fix that then. Thanks for pointing that out. \$\endgroup\$ – Joseph Cooper Jun 14 '15 at 21:26
  • \$\begingroup\$ Thanks, I'll mess with the Quaternion.Euler side but the concept of separation makes sense. \$\endgroup\$ – TryNCode Jun 15 '15 at 18:07
  • \$\begingroup\$ Glad I could help. :D Good Luck btw. \$\endgroup\$ – Joseph Cooper Jun 18 '15 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.