Last active
October 17, 2018 06:39
-
-
Save neon-izm/f1ab7bbb0854f681866015ddd3ce259d to your computer and use it in GitHub Desktop.
Rotation Limit Finger Script ,to solve perception neuron finger drifting. Need Final IK, Set Script Execution Order to 32000(max)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using RootMotion.FinalIK; | |
/// <summary> | |
/// Perception Neuronを取り付けたキャラクターの指が骨折するのを抑止するコード | |
/// 適当に軸を指定して、角度の限界をminとmaxで指定する。 | |
/// FinalIKのRotationLimitHingeのまんまだけど、単体で使えることに意味はあるかなあと。 | |
/// 指の左右開き軸とかも制御したければRotationLimitAngle.csを参考にしたら良いと思う。 | |
/// </summary> | |
[DefaultExecutionOrder(20000)] | |
public class RotationLimitFinger : RotationLimit | |
{ | |
#region Main Interface | |
/// <summary> | |
/// Should the rotation be limited around the axis? | |
/// </summary> | |
public bool useLimits = true; | |
/// <summary> | |
/// The min limit around the axis. | |
/// </summary> | |
public float min = -45; | |
/// <summary> | |
/// The max limit around the axis. | |
/// </summary> | |
public float max = 90; | |
#endregion Main Interface | |
[HideInInspector] | |
public float zeroAxisDisplayOffset; // Angular offset of the scene view display of the Hinge rotation limit | |
private Quaternion lastRotation = Quaternion.identity; | |
private float lastAngle; | |
/* | |
* Limits the rotation in the local space of this instance's Transform. | |
* */ | |
protected override Quaternion LimitRotation(Quaternion rotation) | |
{ | |
lastRotation = LimitHinge(rotation); | |
return lastRotation; | |
} | |
private Quaternion LimitHinge(Quaternion rotation) | |
{ | |
// If limit is zero return rotation fixed to axis | |
if (min == 0 && max == 0 && useLimits) return Quaternion.AngleAxis(0, axis); | |
// Get 1 degree of freedom rotation along axis | |
Quaternion free1DOF = Limit1DOF(rotation, axis); | |
if (!useLimits) return free1DOF; | |
// Get offset from last rotation in angle-axis representation | |
Quaternion addR = free1DOF * Quaternion.Inverse(lastRotation); | |
float addA = Quaternion.Angle(Quaternion.identity, addR); | |
Vector3 secondaryAxis = new Vector3(axis.z, axis.x, axis.y); | |
Vector3 cross = Vector3.Cross(secondaryAxis, axis); | |
if (Vector3.Dot(addR * secondaryAxis, cross) > 0f) addA = -addA; | |
// Clamp to limits | |
lastAngle = Mathf.Clamp(lastAngle + addA, min, max); | |
return Quaternion.AngleAxis(lastAngle, axis); | |
} | |
// ScriptExecutionOrderですごく大きな値にしておきましょう。 | |
void LateUpdate () { | |
transform.localRotation = LimitRotation(transform.localRotation); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment