From 32449a3b4da88058b43c03436a55d5368dc82278 Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Mon, 16 Aug 2021 18:14:52 +0200 Subject: [PATCH] [unity] SkeletonRootMotion component now offers a paramter to apply Rigidbody2D gravity. Defaults to `false` to keep existing behaviour. Closes #1941. --- .../SkeletonRootMotionBaseInspector.cs | 11 +++++++++ .../RootMotion/SkeletonRootMotionBase.cs | 23 +++++++++++++------ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Components/SkeletonRootMotionBaseInspector.cs b/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Components/SkeletonRootMotionBaseInspector.cs index 8314eb092..8d50511fa 100644 --- a/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Components/SkeletonRootMotionBaseInspector.cs +++ b/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Components/SkeletonRootMotionBaseInspector.cs @@ -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); } } diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/RootMotion/SkeletonRootMotionBase.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/RootMotion/SkeletonRootMotionBase.cs index a6222fb23..05833d037 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/RootMotion/SkeletonRootMotionBase.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/RootMotion/SkeletonRootMotionBase.cs @@ -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;