Đoạn code này dùng để điều khiển vị trí ngón tay con boss búng đúng vào đầu hero như trong hình.
Khi mà SkillStart thì điều khiển:
skeletonAnimation.UpdateLocal += SkeletonAnimation_UpdateLocal;
Khi SkillEnd thì remove điều khiển, và reset bone:
skeletonAnimation.UpdateLocal -= SkeletonAnimation_UpdateLocal;
bone.SetToSetupPose();
Full code:
using Spine.Unity;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using zStickgun;
public class EnemySkill : MonoBehaviour
{
internal EnemyControl mEnemyControl;
[SerializeField] internal string mSoundName;
[SpineBone] public string boneName;
private HeroControl target;
private Spine.Bone bone;
private Vector3 targetPosition;
// Start is called before the first frame update
void Start()
{
targetPosition = Vector3.zero;
}
// Update is called once per frame
void Update()
{
}
public virtual void Init(EnemyControl pEnemyControl)
{
mEnemyControl = pEnemyControl;
SkeletonAnimation skeletonAnimation = mEnemyControl.Model;
bone = skeletonAnimation.Skeleton.FindBone(boneName);
}
void SkeletonAnimation_UpdateLocal(ISkeletonAnimation animated)
{
var localPositon = mEnemyControl.Model.transform.InverseTransformPoint(targetPosition);
bone.SetPositionSkeletonSpace(localPositon);
}
public virtual void SkillStart()
{
SkeletonAnimation skeletonAnimation = mEnemyControl.Model;
skeletonAnimation.UpdateLocal += SkeletonAnimation_UpdateLocal;
if (mEnemyControl.Target != null && mEnemyControl.Target.IsAlive())
{
target = mEnemyControl.Target;
targetPosition = target.transform.position + new Vector3(-0.5f, 1.8f, 0f);
}
}
public virtual void Skill()
{
if (!mSoundName.Equals("")) SoundManager.Instance.PlaySFX(mSoundName, true);
if (target != null && target.IsAlive())
{
var heroButton = target.HeroButton;
heroButton.LeaveHero();
GameplayController.Instance.SpawnHeroInOrderForWorldBoss(heroButton);
target = null;
}
}
public virtual void SkillEnd()
{
targetPosition = Vector3.zero;
SkeletonAnimation skeletonAnimation = mEnemyControl.Model;
skeletonAnimation.UpdateLocal -= SkeletonAnimation_UpdateLocal;
bone.SetToSetupPose();
}
}
Link tham khảo:
How to get a bone transform from a SkeletonMecanim?
Spine Unity – Making the arm follow the mouse
Xem thêm bài:
Điều khiển mắt trong spine