Just in case someone want to follow bones to the mouse position, here is the code
private SkeletonAnimation SkeletonAnim;
private Bone HeadBone;
private Bone GunBone;
void Start () {
GroundCheck = transform.Find("GroundCheck");
SkeletonAnim = GetComponent<SkeletonAnimation>();
if(SkeletonAnim) {
// get bones
HeadBone = SkeletonAnim.skeleton.FindBone("head");
GunBone = SkeletonAnim.skeleton.FindBone("gun");
SkeletonAnim.UpdateBones += UpdateBones;
}
}
void UpdateBones(SkeletonAnimation skeletonAnimation) {
// could be public
const float LowerRotationBound = -60.0f;
const float UpperRotationBound = 60.0f;
// temp variables
float tempRot;
Vector3 tempVec;
// head bone rotation
tempVec = Camera.main.WorldToScreenPoint(new Vector3(HeadBone.WorldX+transform.position.x, HeadBone.WorldY+transform.position.y, 0));
tempVec = Input.mousePosition - tempVec;
tempRot = Mathf.Atan2(tempVec.y, tempVec.x*transform.localScale.x) * Mathf.Rad2Deg;
HeadBone.Rotation = Mathf.Clamp(tempRot, LowerRotationBound, UpperRotationBound);
// gun bone rotation
tempVec = Camera.main.WorldToScreenPoint(new Vector3(GunBone.WorldX+transform.position.x, GunBone.WorldY+transform.position.y, 0));
tempVec = Input.mousePosition - tempVec;
tempRot = Mathf.Atan2(tempVec.y, tempVec.x*transform.localScale.x) * Mathf.Rad2Deg;
GunBone.Rotation = Mathf.Clamp(tempRot, LowerRotationBound, UpperRotationBound)-GunBone.parent.WorldRotation;
}
The transform.localScale.x Multiplication is because I am using that Flip to setup the facing of the character when moving left and right:
void Flip() {
// change facing flag and scale transform
FacingRight = !FacingRight;
Vector3 scale = transform.localScale;
scale.x*=-1;
transform.localScale = scale;
}
Hope that helps someone!
BR, Marc