diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonAnimation.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonAnimation.cs index b8268e5b4..d1f3cf5d3 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonAnimation.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonAnimation.cs @@ -244,12 +244,19 @@ namespace Spine.Unity { skeleton.Update(deltaTime); if (Application.isPlaying) { - Vector2 position = new Vector2(transform.position.x, transform.position.y); - Vector2 positionDelta = position - lastPosition; - positionDelta.x /= transform.lossyScale.x; - positionDelta.y /= transform.lossyScale.y; - skeleton.PhysicsTranslate(positionDelta.x, positionDelta.y); - lastPosition = position; + if (applyTranslationToPhysics) { + Vector2 position = new Vector2(transform.position.x, transform.position.y); + Vector2 positionDelta = position - lastPosition; + positionDelta.x /= transform.lossyScale.x; + positionDelta.y /= transform.lossyScale.y; + skeleton.PhysicsTranslate(positionDelta.x, positionDelta.y); + lastPosition = position; + } + if (applyRotationToPhysics) { + float rotation = this.transform.rotation.eulerAngles.z; + skeleton.PhysicsRotate(0, 0, rotation - lastRotation); + lastRotation = rotation; + } } if (updateMode == UpdateMode.OnlyAnimationStatus) { diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs index 826e4d62a..1c7ff9d38 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs @@ -382,12 +382,19 @@ namespace Spine.Unity { skeleton.Update(deltaTime); if (Application.isPlaying) { - Vector2 position = new Vector2(transform.position.x, transform.position.y); - Vector2 positionDelta = (position - lastPosition) / meshScale; - positionDelta.x /= transform.lossyScale.x; - positionDelta.y /= transform.lossyScale.y; - skeleton.PhysicsTranslate(positionDelta.x, positionDelta.y); - lastPosition = position; + if (applyTranslationToPhysics) { + Vector2 position = new Vector2(transform.position.x, transform.position.y); + Vector2 positionDelta = (position - lastPosition) / meshScale; + positionDelta.x /= transform.lossyScale.x; + positionDelta.y /= transform.lossyScale.y; + skeleton.PhysicsTranslate(positionDelta.x, positionDelta.y); + lastPosition = position; + } + if (applyRotationToPhysics) { + float rotation = this.transform.rotation.eulerAngles.z; + skeleton.PhysicsRotate(0, 0, rotation - lastRotation); + lastRotation = rotation; + } } if (updateMode == UpdateMode.OnlyAnimationStatus) { @@ -528,8 +535,22 @@ namespace Spine.Unity { } } - /// Used for applying Transform translation to skeleton physics. + /// When enabled, Transform translation is applied to skeleton PhysicsConstraints. + public bool applyTranslationToPhysics = true; + /// When enabled, Transform rotation is applied to skeleton PhysicsConstraints. + public bool applyRotationToPhysics = true; + + /// Used for applying Transform translation to skeleton PhysicsConstraints. protected Vector2 lastPosition; + /// Used for applying Transform rotation to skeleton PhysicsConstraints. + protected float lastRotation; + + public void ResetLastPosition () { lastPosition = this.transform.position; } + public void ResetLastRotation () { lastRotation = this.transform.rotation.eulerAngles.z; } + public void ResetLastPositionAndRotation () { + lastPosition = this.transform.position; + lastRotation = this.transform.rotation.eulerAngles.z; + } [SerializeField] protected Spine.Unity.MeshGenerator meshGenerator = new MeshGenerator(); public Spine.Unity.MeshGenerator MeshGenerator { get { return this.meshGenerator; } } @@ -690,6 +711,8 @@ namespace Spine.Unity { } public void Initialize (bool overwrite) { + ResetLastPositionAndRotation(); + if (this.IsValid && !overwrite) return; #if UNITY_EDITOR if (BuildUtilities.IsInSkeletonAssetBuildPreProcessing) diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonMecanim.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonMecanim.cs index ac87f089f..219dbf80e 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonMecanim.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonMecanim.cs @@ -127,12 +127,19 @@ namespace Spine.Unity { skeleton.Update(deltaTime); if (Application.isPlaying) { - Vector2 position = new Vector2(transform.position.x, transform.position.y); - Vector2 positionDelta = position - lastPosition; - positionDelta.x /= transform.lossyScale.x; - positionDelta.y /= transform.lossyScale.y; - skeleton.PhysicsTranslate(positionDelta.x, positionDelta.y); - lastPosition = position; + if (applyTranslationToPhysics) { + Vector2 position = new Vector2(transform.position.x, transform.position.y); + Vector2 positionDelta = position - lastPosition; + positionDelta.x /= transform.lossyScale.x; + positionDelta.y /= transform.lossyScale.y; + skeleton.PhysicsTranslate(positionDelta.x, positionDelta.y); + lastPosition = position; + } + if (applyRotationToPhysics) { + float rotation = this.transform.rotation.eulerAngles.z; + skeleton.PhysicsRotate(0, 0, rotation - lastRotation); + lastRotation = rotation; + } } ApplyAnimation(); diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonRenderer.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonRenderer.cs index dd05b9438..f85ca5e3e 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonRenderer.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonRenderer.cs @@ -286,9 +286,25 @@ namespace Spine.Unity { return skeleton; } } + #endregion - /// Used for applying Transform translation to skeleton physics. + #region Physics + /// When enabled, Transform translation is applied to skeleton PhysicsConstraints. + public bool applyTranslationToPhysics = true; + /// When enabled, Transform rotation is applied to skeleton PhysicsConstraints. + public bool applyRotationToPhysics = true; + + /// Used for applying Transform translation to skeleton PhysicsConstraints. protected Vector2 lastPosition; + /// Used for applying Transform rotation to skeleton PhysicsConstraints. + protected float lastRotation; + + public void ResetLastPosition () { lastPosition = this.transform.position; } + public void ResetLastRotation () { lastRotation = this.transform.rotation.eulerAngles.z; } + public void ResetLastPositionAndRotation () { + lastPosition = this.transform.position; + lastRotation = this.transform.rotation.eulerAngles.z; + } #endregion public delegate void SkeletonRendererDelegate (SkeletonRenderer skeletonRenderer); @@ -380,6 +396,8 @@ namespace Spine.Unity { /// Initialize this component. Attempts to load the SkeletonData and creates the internal Skeleton object and buffers. /// If set to true, it will overwrite internal objects if they were already generated. Otherwise, the initialized component will ignore subsequent calls to initialize. public virtual void Initialize (bool overwrite, bool quiet = false) { + ResetLastPositionAndRotation(); + if (valid && !overwrite) return; #if UNITY_EDITOR @@ -427,7 +445,6 @@ namespace Spine.Unity { UpdateMode updateModeSaved = updateMode; updateMode = UpdateMode.FullUpdate; UpdateWorldTransform(Skeleton.Physics.Update); - lastPosition = this.transform.position; LateUpdate(); updateMode = updateModeSaved; diff --git a/spine-unity/Assets/Spine/package.json b/spine-unity/Assets/Spine/package.json index 12f964eae..0c4ebeddf 100644 --- a/spine-unity/Assets/Spine/package.json +++ b/spine-unity/Assets/Spine/package.json @@ -2,7 +2,7 @@ "name": "com.esotericsoftware.spine.spine-unity", "displayName": "spine-unity Runtime", "description": "This plugin provides the spine-unity runtime core.", - "version": "4.2.43", + "version": "4.2.44", "unity": "2018.3", "author": { "name": "Esoteric Software",