123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using UnityEngine;
- using System.Collections;
- namespace RootMotion.FinalIK {
-
-
-
- public class Inertia : OffsetModifier {
-
-
-
- [System.Serializable]
- public class Body {
-
-
-
- [System.Serializable]
- public class EffectorLink {
- [Tooltip("Type of the FBBIK effector to use")]
- public FullBodyBipedEffector effector;
- [Tooltip("Weight of using this effector")]
- public float weight;
- }
- [Tooltip("The Transform to follow, can be any bone of the character")]
- public Transform transform;
- [Tooltip("Linking the body to effectors. One Body can be used to offset more than one effector")]
- public EffectorLink[] effectorLinks;
- [Tooltip("The speed to follow the Transform")]
- public float speed = 10f;
- [Tooltip("The acceleration, smaller values means lazyer following")]
- public float acceleration = 3f;
- [Tooltip("Matching target velocity")]
- [Range(0f, 1f)] public float matchVelocity;
- [Tooltip("gravity applied to the Body")]
- public float gravity;
- private Vector3 delta;
- private Vector3 lazyPoint;
- private Vector3 direction;
- private Vector3 lastPosition;
- private bool firstUpdate = true;
-
- public void Reset() {
- if (transform == null) return;
- lazyPoint = transform.position;
- lastPosition = transform.position;
- direction = Vector3.zero;
- }
-
- public void Update(IKSolverFullBodyBiped solver, float weight, float deltaTime) {
- if (transform == null) return;
-
- if (firstUpdate) {
- Reset();
- firstUpdate = false;
- }
-
- direction = Vector3.Lerp(direction, ((transform.position - lazyPoint) / deltaTime) * 0.01f, deltaTime * acceleration);
-
- lazyPoint += direction * deltaTime * speed;
-
- delta = transform.position - lastPosition;
- lazyPoint += delta * matchVelocity;
-
-
- lazyPoint.y += gravity * deltaTime;
-
- foreach (EffectorLink effectorLink in effectorLinks) {
- solver.GetEffector(effectorLink.effector).positionOffset += (lazyPoint - transform.position) * effectorLink.weight * weight;
- }
- lastPosition = transform.position;
- }
- }
- [Tooltip("The array of Bodies")]
- public Body[] bodies;
- [Tooltip("The array of OffsetLimits")]
- public OffsetLimits[] limits;
-
- public void ResetBodies() {
- lastTime = Time.time;
- foreach (Body body in bodies) body.Reset();
- }
-
- protected override void OnModifyOffset() {
-
- foreach (Body body in bodies) body.Update(ik.solver, weight, deltaTime);
-
- ApplyLimits(limits);
- }
- }
- }
|