I have a working Animator Controller Script that will control the players primary Skeleton and the code is written to control the clones as well so I can use them for a Screen Wrap effect.
Unfortunately it will only animate the first Skeleton that the code executes on and I don't know why.
If I comment out the primary skeleton code line in Update the next one will animate. All of them should be animating exactly the same.
Please help! Thanks!
This section should be setting the animation for all the Skeletons but only the first ones ever animate.
//Code to determine toggle of MorphBall mode and Armor
if (input.isMorphBall)
{
ToggleSkeleton(ArmorAnimation, MorphBallAnimation);
ToggleSkeleton(ncArmorAnimation, ncMorphBallAnimation);
ToggleSkeleton(wcArmorAnimation, wcMorphBallAnimation);
ToggleSkeleton(scArmorAnimation, scMorphBallAnimation);
ToggleSkeleton(ecArmorAnimation, ecMorphBallAnimation);
MBallControl(MorphBallAnimation);
MBallControl(ncMorphBallAnimation);
MBallControl(wcMorphBallAnimation);
MBallControl(scMorphBallAnimation);
MBallControl(ecMorphBallAnimation);
}
else
{
ToggleSkeleton(MorphBallAnimation, ArmorAnimation);
ArmorControl(ArmorAnimation);
ToggleSkeleton(ncMorphBallAnimation, ncArmorAnimation);
ArmorControl(ncArmorAnimation);
ToggleSkeleton(wcMorphBallAnimation, wcArmorAnimation);
ArmorControl(wcArmorAnimation);
ToggleSkeleton(scMorphBallAnimation, scArmorAnimation);
ArmorControl(scArmorAnimation);
ToggleSkeleton(ecMorphBallAnimation, ecArmorAnimation);
ArmorControl(ecArmorAnimation);
}
Here is the full animator script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Spine.Unity;
public class SpineAnimator : MonoBehaviour
{
public SkeletonAnimation ArmorAnimation;
public SkeletonAnimation ncArmorAnimation;
public SkeletonAnimation wcArmorAnimation;
public SkeletonAnimation scArmorAnimation;
public SkeletonAnimation ecArmorAnimation;
public SkeletonAnimation MorphBallAnimation;
public SkeletonAnimation ncMorphBallAnimation;
public SkeletonAnimation wcMorphBallAnimation;
public SkeletonAnimation scMorphBallAnimation;
public SkeletonAnimation ecMorphBallAnimation;
public AnimationReferenceAsset idle, run, jump, fall, dash, ledgegrab, wallslide, rangeDown, rangeDownAngle, rangeStraight, rangeUpAngle, rangeUp;
public AnimationReferenceAsset mbidle, mbroll, mbportalin, mbportalout;
public PlayerInput input;
public string currentAnimation0;
public string currentAnimation1;
//Values set for animations
//public bool isMorphBall;
public bool isOnGround;
//public float xVelocity;
public bool isFacingRight;
//public bool rangeHeld;
public bool isWallGrab;
public bool isLedgeGrab;
public bool isJumping;
public bool isDashing;
public float yVelocity;
//public bool isTakingDamage;
//public bool isSpinDash;
//public bool isCrouching;
//public bool isCrouchingStill);
public enum AimingDirection
{
Up,
UpDiag,
Straight,
DownDiag,
Down,
Clear
}
public AimingDirection Aiming; //Direction player is Aiming
// Start is called before the first frame update
void Start()
{
input = GetComponent<PlayerInput>();
isFacingRight = true;
}
// Update is called once per frame
void Update()
{
//Flip whole Character code based on Directional input
if(input.horizontal > 0 && !isFacingRight)
{
Flip();
}
else if (input.horizontal < 0 && isFacingRight)
{
Flip();
}
//Code to determine toggle of MorphBall mode and Armor
if (input.isMorphBall)
{
ToggleSkeleton(ArmorAnimation, MorphBallAnimation);
ToggleSkeleton(ncArmorAnimation, ncMorphBallAnimation);
ToggleSkeleton(wcArmorAnimation, wcMorphBallAnimation);
ToggleSkeleton(scArmorAnimation, scMorphBallAnimation);
ToggleSkeleton(ecArmorAnimation, ecMorphBallAnimation);
MBallControl(MorphBallAnimation);
MBallControl(ncMorphBallAnimation);
MBallControl(wcMorphBallAnimation);
MBallControl(scMorphBallAnimation);
MBallControl(ecMorphBallAnimation);
}
else
{
ToggleSkeleton(MorphBallAnimation, ArmorAnimation);
ArmorControl(ArmorAnimation);
ToggleSkeleton(ncMorphBallAnimation, ncArmorAnimation);
ArmorControl(ncArmorAnimation);
ToggleSkeleton(wcMorphBallAnimation, wcArmorAnimation);
ArmorControl(wcArmorAnimation);
ToggleSkeleton(scMorphBallAnimation, scArmorAnimation);
ArmorControl(scArmorAnimation);
ToggleSkeleton(ecMorphBallAnimation, ecArmorAnimation);
ArmorControl(ecArmorAnimation);
}
}
//MorphBall Animation Control Code
public void MBallControl(SkeletonAnimation mbanim)
{
Debug.Log(mbanim.gameObject.name);
//Check for player horizontal movement
if (input.horizontal != 0) //Check if player is rolling
{
SetAnimation(mbanim, mbroll, true, 1, 0); //Set roll Animation on track 0
}
else if (input.horizontal == 0) //Check if player is idle
{
ClearTrack(mbanim, 0, 0); //Stop roll on Track 0
}
}
//Armor Mode Animation Control Code
public void ArmorControl(SkeletonAnimation armoranim)
{
//Track 0 Animations
//Dashing Animation Code
if (input.isDashing) //Check if Dashing
{
ClearTrack(armoranim, 1, 0); //Clear Track 1
SetAnimation(armoranim, dash, false, 1, 0); //Set Dash Animation on track 0
}
//Grounded Movement Animation Code
else if (isOnGround) //Check if player is grounded
{
if (input.horizontal != 0) //Check if player is running
{
SetAnimation(armoranim, run, true, 1, 0); //Set Run Animation on track 0
}
else if (input.horizontal == 0) //Check if player is idle
{
SetAnimation(armoranim, idle, true, 1, 0); //Set Idle Aimation on Track 0
}
}
//Ungrounded movement Animation Code
else if (!isOnGround) //Check if player is off ground
{
if (isLedgeGrab) //Check if player is grabbing a ledge
{
ClearTrack(armoranim, 1, 0); //Clear Track 1
SetAnimation(armoranim, ledgegrab, true, 1, 0); //Set Ledge Grab Animation on track 0
}
else if (isWallGrab)
{
ClearTrack(armoranim, 1, 0); //Clear Track 1
SetAnimation(armoranim, wallslide, false, 1, 0); //Set Wall Slide Animation
}
else if (input.vertical > 0) //Check if Jumping based on upward Vertical Movement
{
SetAnimation(armoranim, jump, false, 1, 0); //Set Jump Animation on track 0
}
else if (input.vertical <= 0) //Check if Falling based on downward Vertical Movement
{
SetAnimation(armoranim, fall, false, 1, 0); //Set Falling Animation on Track 0
}
}
//Track 1 Animations - Aiming
if (!isLedgeGrab)
{
switch (input.Aiming)
{
case PlayerInput.AimingDirection.Up:
SetAnimation(armoranim, rangeUp, false, 1, 1);
break;
case PlayerInput.AimingDirection.UpDiag:
SetAnimation(armoranim, rangeUpAngle, false, 1, 1);
break;
case PlayerInput.AimingDirection.Straight:
SetAnimation(armoranim, rangeStraight, false, 1, 1);
break;
case PlayerInput.AimingDirection.DownDiag:
SetAnimation(armoranim, rangeDownAngle, false, 1, 1);
break;
case PlayerInput.AimingDirection.Down:
SetAnimation(armoranim, rangeDown, false, 1, 1);
break;
case PlayerInput.AimingDirection.Clear:
ClearTrack(armoranim, 1, 0.5f);
break;
}
}
}
//Function to set next animation
public void SetAnimation(SkeletonAnimation anim, AnimationReferenceAsset animation, bool loop, float timeScale, int track)
{
if (track == 0 && animation.name.Equals(currentAnimation0))
{
return;
}
else if (track == 1 && animation.name.Equals(currentAnimation1))
{
return;
}
else if (track == 0)
{
currentAnimation0 = animation.name;
}else if (track == 1)
{
currentAnimation1 = animation.name;
}
anim.state.SetAnimation(track, animation, loop).TimeScale = timeScale;
}
//Clear Animation Track
public void ClearTrack(SkeletonAnimation anim, int track, float delay)
{
if (anim.state.GetCurrent(track) != null) //Check for Track 1 animation and clear if necessary
{
if(delay > 0)
{
anim.state.AddEmptyAnimation(track, 0.5f, delay);
}
else
{
anim.state.ClearTrack(track); //Clear Track 1
}
if (track == 0)
{
currentAnimation0 = null;
}
else if (track == 1)
{
currentAnimation1 = null;
}
}
}
//Change which Skeleton is active
public void ToggleSkeleton(SkeletonAnimation anim1, SkeletonAnimation anim2)
{
anim1.gameObject.SetActive(false);
anim2.gameObject.SetActive(true);
}
void Flip()
{
isFacingRight = !isFacingRight;
transform.Rotate(0f, 180f, 0f);
}
/*public void CheckCharacterState()
{
if (xVelocity != 0)
{
SetAnimation(run, true, 1f);
}else if (xVelocity == 0)
{
SetAnimation(idle, true, 1f);
}
}*/
}