ソースを参照

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

Harald Csaszar 4 年 前
コミット
32449a3b4d

+ 11 - 0
spine-unity/Assets/Spine/Editor/spine-unity/Editor/Components/SkeletonRootMotionBaseInspector.cs

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

+ 16 - 7
spine-unity/Assets/Spine/Runtime/spine-unity/Components/RootMotion/SkeletonRootMotionBase.cs

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