Modifying bone without overwriting animation?

I am modifying a skeleton by overwriting the localRotation and localPosition variables of a single bone. Im just feeding it variables from a sinus function.

transform.localPosition.x = Mathf.Sin(Time.time)

This is straightforward and I achieve my goal, just until I want to keep the original animation on the bone. What I really would like was to add the sinus on top of the animation of the bone.

I would ask my rigger to insert an extra bone without any animation in the hierachy that I could operate on, but unfortunately our team is without rigger at the moment.

I tried inserting an extra gameobject in the bone hierachy, in an attempt to hack it up myself, but it seems to break the animations on the bones downstream.

Is there a place to read the localPositon/localRotation of the bone so I can add it to my change, or is there a whole other way to add change to the skeleton.

Sort of like the SkelControlSingleBone from Unreal3

kind regards

Tax

Animations are applied after Update but before LateUpdate.

So in your case, you can read off the end result of the animation in LateUpdate and add your function to it.

transform.localPosition.x = transform.localPosition.x+ Mathf.Sin(Time.time)

Untested, but something like this might do it.

Thanx for the reply. I tried the following, but im not in the clear yet. :slight_smile:

using UnityEngine;
using System.Collections;

public class Sway : MonoBehaviour {
	//Translation
	private Vector3 pOrig;
	private Vector3 pNow;
	
	void Update()
	{
		//Read animation position
		pOrig = transform.localPosition;
		
	}
	
	void LateUpdate ()
	{
		pNow = new Vector3(1.0f,0.0f, 0.0f) * Mathf.Sin(Time.time);
		transform.localPosition = pOrig + pNow;
		
	}
}

It seems that once I set the transform.localPosition the animation does not reset it? -So the result is that I accumulate my sinus changes over time, making my bone drift through my world.

Is there a way to tell the bone to read its position from animation data?

One alternative could be to store added changes to the bone, but I think its an ugly solution that would accumulate numerical errors.

What do you think?

Kind regards

Tax

Can you record the movement that you make in the LateUpdate and then just reverse it in the Update (on the following frame)?

I did that today, but im worried that I might be accumulating some error?

Adding and subtracting quaternions an infinite number of times makes me worry a bit about drifting numerical values.

Update gets the current position, before animations are applied. The order of your operations is bypassing the applied animation, which is between Update and LateUpdate.

Do this instead:

using UnityEngine;
using System.Collections;

public class Sway : MonoBehaviour {
   //Translation
   private Vector3 pOrig;
   private Vector3 pNow;
   
   void Update()
   {
        // Animation has not yet been applied here
        // Localposition just holds position from previous frame
   }
   
   void LateUpdate ()
   {
      //Read position right after animation is applied
      pOrig = transform.localPosition;
      
      pNow = new Vector3(1.0f,0.0f, 0.0f) * Mathf.Sin(Time.time);
      transform.localPosition = pOrig + pNow;
   }
}