82 lines
2.4 KiB
C#
82 lines
2.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using BITKit.Core.Tuple;
|
|
using UnityEngine;
|
|
|
|
namespace BITKit.Physics
|
|
{
|
|
[Serializable]
|
|
public class JointConfigure
|
|
{
|
|
public Transform animate;
|
|
public ConfigurableJoint joint;
|
|
public Quaternion InitialRotation { get; set; }
|
|
}
|
|
public class PhysicsBasedAnimation : MonoBehaviour
|
|
{
|
|
[Range(0, 1)] public float Blend;
|
|
[SerializeField] private JointConfigure[] jointConfigures;
|
|
[SerializeField] private float positionSpring;
|
|
[SerializeField] private float positionDamper;
|
|
[SerializeField] private float maximumForce;
|
|
[SerializeField] private Optional<UnityTuple<Rigidbody, Transform>> rootSync;
|
|
[SerializeField] private Optional<ConfigurableJointMotion> overrideMotion;
|
|
[SerializeField] private Optional<ConfigurableJointMotion> overrideAngularMotion;
|
|
|
|
private void Start()
|
|
{
|
|
foreach (var x in jointConfigures)
|
|
{
|
|
x.InitialRotation=x.animate.localRotation;
|
|
}
|
|
}
|
|
private void FixedUpdate()
|
|
{
|
|
//var spring = Mathf.Lerp(Blend,0,angularSprint);
|
|
var drive = new JointDrive
|
|
{
|
|
positionDamper = positionDamper,
|
|
positionSpring =Mathf.Lerp(0,positionSpring,Blend),
|
|
maximumForce = maximumForce,
|
|
};
|
|
foreach (var jointConfigure in jointConfigures)
|
|
{
|
|
jointConfigure.joint.angularXDrive = drive;
|
|
jointConfigure.joint.angularYZDrive = drive;
|
|
jointConfigure.joint.xDrive = drive;
|
|
jointConfigure.joint.yDrive = drive;
|
|
jointConfigure.joint.zDrive = drive;
|
|
|
|
jointConfigure.joint.targetRotation = Quaternion.Inverse(jointConfigure.animate.localRotation) *
|
|
jointConfigure.InitialRotation;
|
|
jointConfigure.joint.targetPosition = jointConfigure.animate.localPosition;
|
|
|
|
if (overrideAngularMotion.Allow)
|
|
{
|
|
jointConfigure.joint.angularXMotion = overrideAngularMotion.Value;
|
|
jointConfigure.joint.angularYMotion = overrideAngularMotion.Value;
|
|
jointConfigure.joint.angularZMotion = overrideAngularMotion.Value;
|
|
}
|
|
|
|
if (overrideMotion.Allow)
|
|
{
|
|
jointConfigure.joint.xMotion = overrideMotion.Value;
|
|
jointConfigure.joint.yMotion = overrideMotion.Value;
|
|
jointConfigure.joint.zMotion = overrideMotion.Value;
|
|
}
|
|
}
|
|
|
|
if (rootSync.Allow)
|
|
{
|
|
var root = rootSync.Value;
|
|
root.Item1.transform.localPosition = root.Item2.localPosition;
|
|
root.Item1.MoveRotation(root.Item2.rotation);
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
}
|