Skip to content

Instantly share code, notes, and snippets.

@baroquedub
Last active November 1, 2019 12:48
Show Gist options
  • Select an option

  • Save baroquedub/e162ff8625d581fe9753bed0199e3532 to your computer and use it in GitHub Desktop.

Select an option

Save baroquedub/e162ff8625d581fe9753bed0199e3532 to your computer and use it in GitHub Desktop.
Trying to mirror Humanoid transforms (not working)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text.RegularExpressions;
public class AvatarPuppet : MonoBehaviour
{
[SerializeField]
GameObject sourceAvatar;
Transform[] sourceTransforms;
Transform[] destinationTransforms;
// used by Mirrored Mode (i.e. invert = true)
// maps transforms to name of opposite side. i.e. Left becomes Right
Dictionary<string, Transform> destinationTransformsDictionary = new Dictionary<string, Transform>();
public bool invert = false;
void Awake()
{
sourceTransforms = sourceAvatar.GetComponentsInChildren<Transform>();
destinationTransforms = GetComponentsInChildren<Transform>();
if(invert){
PrepareMirroredRig();
}
}
void Update()
{
if(invert){
for (int i=0; i<sourceTransforms.Length; i++ ){
string sourceName = sourceTransforms[i].gameObject.name;
destinationTransformsDictionary[sourceName].localPosition = sourceTransforms[i].localPosition;
//destinationTransformsDictionary[sourceName].localRotation = sourceTransforms[i].localRotation;
/* try mirror rotations */
destinationTransforms[i].localRotation = new Quaternion(sourceTransforms[i].localRotation.x * -1.0f,
sourceTransforms[i].localRotation.y,
sourceTransforms[i].localRotation.z,
sourceTransforms[i].localRotation.w * -1.0f);
}
}else{
//normal copy of transforms
for (int i=0; i<sourceTransforms.Length; i++ ){
destinationTransforms[i].localPosition = sourceTransforms[i].localPosition;
destinationTransforms[i].localRotation = sourceTransforms[i].localRotation;
}
}
}
private void PrepareMirroredRig(){
for (int i=0; i<destinationTransforms.Length; i++ ){
string transformName = destinationTransforms[i].gameObject.name;
string destinationName = "";
if( Regex.Matches(transformName, "Left").Count > 0){
destinationName = transformName.Replace("Left","Right");
}else{
destinationName = transformName.Replace("Right","Left");
}
//Debug.Log("source: "+transformName+" > "+destinationName);
// string "mixamorig:LeftArm" renamed "mixamorig:RightArm"
// mapped to Transform for 'mixamorig:LeftArm' in Dictionary:
destinationTransformsDictionary.Add(destinationName, destinationTransforms[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment