[unity] Automatically applying Transform rotation changes to skeleton physics constraints at skeleton components. See commit 05e37fc.

This commit is contained in:
Harald Csaszar 2024-01-22 18:25:44 +01:00
parent 05e37fc738
commit 9602a715d9
5 changed files with 76 additions and 22 deletions

View File

@ -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) {

View File

@ -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 {
}
}
/// <summary>Used for applying Transform translation to skeleton physics.</summary>
/// <summary>When enabled, Transform translation is applied to skeleton PhysicsConstraints.</summary>
public bool applyTranslationToPhysics = true;
/// <summary>When enabled, Transform rotation is applied to skeleton PhysicsConstraints.</summary>
public bool applyRotationToPhysics = true;
/// <summary>Used for applying Transform translation to skeleton PhysicsConstraints.</summary>
protected Vector2 lastPosition;
/// <summary>Used for applying Transform rotation to skeleton PhysicsConstraints.</summary>
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)

View File

@ -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();

View File

@ -286,9 +286,25 @@ namespace Spine.Unity {
return skeleton;
}
}
#endregion
/// <summary>Used for applying Transform translation to skeleton physics.</summary>
#region Physics
/// <summary>When enabled, Transform translation is applied to skeleton PhysicsConstraints.</summary>
public bool applyTranslationToPhysics = true;
/// <summary>When enabled, Transform rotation is applied to skeleton PhysicsConstraints.</summary>
public bool applyRotationToPhysics = true;
/// <summary>Used for applying Transform translation to skeleton PhysicsConstraints.</summary>
protected Vector2 lastPosition;
/// <summary>Used for applying Transform rotation to skeleton PhysicsConstraints.</summary>
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.</summary>
/// <param name="overwrite">If set to <c>true</c>, it will overwrite internal objects if they were already generated. Otherwise, the initialized component will ignore subsequent calls to initialize.</param>
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;

View File

@ -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",