[unity] SkeletonRootMotion component now offers a paramter to apply Rigidbody2D gravity. Defaults to false to keep existing behaviour. Closes #1941.

This commit is contained in:
Harald Csaszar 2021-08-16 18:14:52 +02:00
parent b4286cd615
commit 32449a3b4d
2 changed files with 27 additions and 7 deletions

View File

@ -42,6 +42,7 @@ namespace Spine.Unity.Editor {
protected SerializedProperty rootMotionTranslateXPerY;
protected SerializedProperty rootMotionTranslateYPerX;
protected SerializedProperty rigidBody2D;
protected SerializedProperty applyRigidbody2DGravity;
protected SerializedProperty rigidBody;
protected GUIContent rootMotionBoneNameLabel;
@ -52,6 +53,7 @@ namespace Spine.Unity.Editor {
protected GUIContent rootMotionTranslateXPerYLabel;
protected GUIContent rootMotionTranslateYPerXLabel;
protected GUIContent rigidBody2DLabel;
protected GUIContent applyRigidbody2DGravityLabel;
protected GUIContent rigidBodyLabel;
protected virtual void OnEnable () {
@ -64,6 +66,7 @@ namespace Spine.Unity.Editor {
rootMotionTranslateXPerY = serializedObject.FindProperty("rootMotionTranslateXPerY");
rootMotionTranslateYPerX = serializedObject.FindProperty("rootMotionTranslateYPerX");
rigidBody2D = serializedObject.FindProperty("rigidBody2D");
applyRigidbody2DGravity = serializedObject.FindProperty("applyRigidbody2DGravity");
rigidBody = serializedObject.FindProperty("rigidBody");
rootMotionBoneNameLabel = new UnityEngine.GUIContent("Root Motion Bone", "The bone to take the motion from.");
@ -79,6 +82,8 @@ namespace Spine.Unity.Editor {
"\n\n" +
"Note that animation and physics updates are not always in sync." +
"Some jitter may result at certain framerates.");
applyRigidbody2DGravityLabel = new UnityEngine.GUIContent("Apply Gravity",
"Apply Rigidbody2D Gravity");
rigidBodyLabel = new UnityEngine.GUIContent("Rigidbody",
"Optional Rigidbody: Assign a Rigidbody here if you want " +
" to apply the root motion to the rigidbody instead of the Transform." +
@ -107,6 +112,12 @@ namespace Spine.Unity.Editor {
protected virtual void OptionalPropertyFields () {
EditorGUILayout.PropertyField(rigidBody2D, rigidBody2DLabel);
if (rigidBody2D.objectReferenceValue != null || rigidBody2D.hasMultipleDifferentValues) {
using (new SpineInspectorUtility.IndentScope())
EditorGUILayout.PropertyField(applyRigidbody2DGravity, applyRigidbody2DGravityLabel);
}
EditorGUILayout.PropertyField(rigidBody, rigidBodyLabel);
}
}

View File

@ -55,6 +55,7 @@ namespace Spine.Unity {
[Header("Optional")]
public Rigidbody2D rigidBody2D;
public bool applyRigidbody2DGravity = false;
public Rigidbody rigidBody;
public bool UsesRigidbody {
@ -93,7 +94,18 @@ namespace Spine.Unity {
return; // Root motion is only applied when component is enabled.
if (rigidBody2D != null) {
rigidBody2D.MovePosition(new Vector2(transform.position.x, transform.position.y)
Vector2 gravityAndVelocityMovement = Vector2.zero;
if (applyRigidbody2DGravity) {
float deltaTime = Time.fixedDeltaTime;
float deltaTimeSquared = (deltaTime * deltaTime);
rigidBody2D.velocity += rigidBody2D.gravityScale * Physics2D.gravity * deltaTime;
gravityAndVelocityMovement = 0.5f * rigidBody2D.gravityScale * Physics2D.gravity * deltaTimeSquared +
rigidBody2D.velocity * deltaTime;
}
rigidBody2D.MovePosition(gravityAndVelocityMovement + new Vector2(transform.position.x, transform.position.y)
+ rigidbodyDisplacement);
}
if (rigidBody != null) {
@ -143,8 +155,7 @@ namespace Spine.Unity {
if (index >= 0) {
this.rootMotionBoneIndex = index;
this.rootMotionBone = skeleton.bones.Items[index];
}
else {
} else {
Debug.Log("Bone named \"" + name + "\" could not be found.");
this.rootMotionBoneIndex = 0;
this.rootMotionBone = skeleton.RootBone;
@ -250,8 +261,7 @@ namespace Spine.Unity {
// to prevent stutter which would otherwise occur if we don't move every Update.
tempSkeletonDisplacement += skeletonDelta;
SetEffectiveBoneOffsetsTo(tempSkeletonDisplacement, parentBoneScale);
}
else {
} else {
transform.position += transform.TransformVector(skeletonDelta);
ClearEffectiveBoneOffsets(parentBoneScale);
}
@ -305,8 +315,7 @@ namespace Spine.Unity {
if (topLevelBone == rootMotionBone) {
if (transformPositionX) topLevelBone.x = displacementSkeletonSpace.x / skeleton.ScaleX;
if (transformPositionY) topLevelBone.y = displacementSkeletonSpace.y / skeleton.ScaleY;
}
else {
} else {
float offsetX = (initialOffset.x - rootMotionBone.x) * parentBoneScale.x;
float offsetY = (initialOffset.y - rootMotionBone.y) * parentBoneScale.y;
if (transformPositionX) topLevelBone.x = (displacementSkeletonSpace.x / skeleton.ScaleX) + offsetX;