Hello there! I have a simple mechanic I'm trying to achieve. In my game, there are butterflies that follow the characters wherever they go. And I want to achieve the "following feeling" rather than having them fixed over the characters.
I could simply create a MoveTowards script and have the transfrom follow the character, but I want the butterfly to follow the bone itself. Because in some animations, the character can move away from the center.
TL;DR So, I've been trying to modify the BoneFollower script into making it use Vector2.MoveTowards. Rather than having it fixed over the bone.
How can I do that?
Here's what I attempted:
public void LateUpdate () {
if (!valid) {
Initialize();
return;
}
#if UNITY_EDITOR
if (!Application.isPlaying)
skeletonTransformIsParent = Transform.ReferenceEquals(skeletonTransform, transform.parent);
#endif
if (bone == null) {
if (string.IsNullOrEmpty(boneName)) return;
bone = skeletonRenderer.skeleton.FindBone(boneName);
if (!SetBone(boneName)) return;
}
Transform thisTransform = this.transform;
if (skeletonTransformIsParent) {
// Recommended setup: Use local transform properties if Spine GameObject is the immediate parent
thisTransform.localPosition = new Vector3(followXYPosition ? bone.worldX : thisTransform.localPosition.x,
followXYPosition ? bone.worldY : thisTransform.localPosition.y,
followZPosition ? 0f : thisTransform.localPosition.z);
if (followBoneRotation) {
float halfRotation = Mathf.Atan2(bone.c, bone.a) * 0.5f;
if (followLocalScale && bone.scaleX < 0) // Negate rotation from negative scaleX. Don't use negative determinant. local scaleY doesn't factor into used rotation.
halfRotation += Mathf.PI * 0.5f;
var q = default(Quaternion);
q.z = Mathf.Sin(halfRotation);
q.w = Mathf.Cos(halfRotation);
thisTransform.localRotation = q;
}
} else {
// For special cases: Use transform world properties if transform relationship is complicated
Vector3 targetWorldPosition = skeletonTransform.TransformPoint(new Vector3(bone.worldX, bone.worldY, 0f));
if (!followZPosition) targetWorldPosition.z = thisTransform.position.z;
if (!followXYPosition) {
targetWorldPosition.x = thisTransform.position.x;
targetWorldPosition.y = thisTransform.position.y;
}
float boneWorldRotation = bone.WorldRotationX;
Transform transformParent = thisTransform.parent;
if (transformParent != null) {
Matrix4x4 m = transformParent.localToWorldMatrix;
if (m.m00 * m.m11 - m.m01 * m.m10 < 0) // Determinant2D is negative
boneWorldRotation = -boneWorldRotation;
}
if (followBoneRotation) {
Vector3 worldRotation = skeletonTransform.rotation.eulerAngles;
if (followLocalScale && bone.scaleX < 0) boneWorldRotation += 180f;
thisTransform.SetPositionAndRotation(targetWorldPosition, Quaternion.Euler(worldRotation.x, worldRotation.y, worldRotation.z + boneWorldRotation));
} else {
thisTransform.position = Vector2.MoveTowards(thisTransform.position, new Vector2(targetWorldPosition.x, targetWorldPosition.y), 7f ) ;
}
}
Vector3 localScale = followLocalScale ? new Vector3(bone.scaleX, bone.scaleY, 1f) : new Vector3(1f, 1f, 1f);
if (followSkeletonFlip) localScale.y *= Mathf.Sign(bone.skeleton.ScaleX * bone.skeleton.ScaleY);
thisTransform.localScale = localScale;
}
It still doesn't work. On the parameters of the script, I only have "FollowXYPosition" set to true.
This is the line I edited in the script:
[b] thisTransform.position = Vector2.MoveTowards(thisTransform.position, new Vector2(targetWorldPosition.x, targetWorldPosition.y), 7f )[/b]
Can you please help? Thank you