- Home /
Animated Sprite Rotating Around It's Own Axis Instead Of Parent's Using Script. Details Provided.
Hi, everyone!
My problem is probably very simple. I have a heavily animated sprite. The sprite comes in five parts; head, left arm, right arm, left side, and right side. When animating, the character's shoulders move as they should because it's just a series of stationary animated sprites. Now, in order to get his arms to rotate in the correct position, I had to animate a shoulder joint in alignment with each shoulder 'socket', so to speak. So that's the first thing to remember.
Secondly, in order to get the animated arms sprites to align with the new shoulder joint, they had to be 'deparented', parented to a temp sprite, adjusted for any position change (stationary sprites), and then reparented to the shoulder joint.
So, in theory, I figured I'd be able to rotate the shoulder joint and the children (arms) would rotate at that pivot point. Nope. They rotate around their own pivot instead.
If you can think of any solution, a better method of getting the arms aligned and allowing them to rotate, or even an asset from the asset store, please let me know and, as you know, your help is always appreciated.
The code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Add to a joint or child of a joint. The
/// script will then remove the specified child
/// objects (ChildObjects), change their location
/// to the specified location (Location), and
/// parent them to the temporary parent object
/// (TempParent).
/// </summary>
public class AdjustToJoint : MonoBehaviour
{
public Transform[] ChildObjects;
public Transform TempParent;
public Vector3 Location;
void Update ()
{
foreach (Transform ChildObject in ChildObjects)
{
//Parent the child objects to the TempParent.
ChildObject.SetParent(TempParent);
//Set the new location.
ChildObject.localPosition = Location;
//Reparent the objects.
ChildObject.SetParent(transform);
}
}
}
And here's a little pic to help you out considering I'm not very, uh, what's the word...
Any chance you could post a video or animated gif showing the exact scenario? Also please show where in the hierarchy your animator(s) is/are.
I suspect that you may be suffering from a misunderstanding of how the animator component works with childed objects, but I can't be sure without a better understanding of why you made the script you did.
Hi! Thanks for the response. I ended up just animating the arms using markers for the shoulders ins$$anonymous$$d. It was much easier than what I was doing. When I went to bed, I was like, "DOH!' because I obviously wasn't thinking clearly. The method you suggested (rotateAround) worked a little better, but it was obviously better to animate the arms according to the shoulder movement and then rerender the arms in 3D Studio without X or Y movement. That's probably how it's supposed to be done anyway. The result is this, and thanks again for your help.