Skip to content

Instantly share code, notes, and snippets.

@zurra

zurra/Commons.cs Secret

Created January 9, 2017 19:46
Common thingies for Fleshwound
using RootMotion.Dynamics;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Fleshwound.Core
{
public static class Commons
{
public enum Hand { Right, Left, None }
public enum InputType { Yaw, Pitch, Roll }
public enum TransformType { Rotate, Scale, Move }
public enum ActionButtons { None = 0, ActionButton1 = 1, ActionButton2 = 2, ActionButton3 = 3 }
public enum MoveAxis { X = 1, Y = 2, Z = 3 }
public static Rigidbody GetMuscleRigidbody(HumanBodyBones bone, PuppetMaster puppet, Animator animator)
{
Rigidbody rb = null;
foreach (var item in puppet.muscles)
{
if (item.target == animator.GetBoneTransform(bone))
rb = item.joint.GetComponent<Rigidbody>();
}
return rb;
}
public static Transform GetMuscleTarget(HumanBodyBones bone, PuppetMaster puppet, Animator animator)
{
Transform target = null;
foreach (var item in puppet.muscles)
{
if (item.target == animator.GetBoneTransform(bone))
target = item.target;
}
return target;
}
public static HumanBodyBones GetHumanbodyBonesByTransform(Transform trans, Animator animator)
{
HumanBodyBones hb = HumanBodyBones.Head;
foreach (HumanBodyBones item in Enum.GetValues(typeof(HumanBodyBones)))
{
if (animator.GetBoneTransform(item) == trans)
hb = item;
}
return hb;
}
public static ConfigurableJoint GetMuscleJoint(HumanBodyBones bone, PuppetMaster puppet, Animator animator)
{
ConfigurableJoint joint = null;
foreach (var item in puppet.muscles)
{
if (item.target == animator.GetBoneTransform(bone))
joint = item.joint;
}
return joint;
}
}
static public class MethodExtensionForMonoBehaviourTransform
{
/// <summary>
/// Gets or add a component. Usage example:
/// BoxCollider boxCollider = transform.GetOrAddComponent<BoxCollider>();
/// </summary>
static public T GetOrAddComponent<T>(this Component child) where T : Component
{
T result = child.GetComponent<T>();
if (result == null)
{
result = child.gameObject.AddComponent<T>();
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment