I apologize for the seeming doublepost but I realized that I may have been getting off topic in my last post which was specific to moving an IK target using the joystick.
I am trying to figure out how to get bones in Unity to look at an IK target. Currently the bone either does nothing when I apply code to it, or if I set it's override to the IK target, itself, it bends and skews either in the wrong way, or teleports to the IK target position (or its inverse). So either things are totally warped, inverted and warped, or teleported to the either the IK target or it's inverse position. I've been trying lots of different things and nothing has been working very well, or having no apparent affect at all.
Currently I'm trying this code from this post:
Spine Unity - Making the arm follow the mouse
This doesn't actually seem to make my animation do anything aside from preventing my shooting arm from transitioning from it's idle state into the shooting state. Anyone else run into that issue trying to implement this/their own methods? How did you fix it?
And lastly, my current code looks like this:
[SpineBone]
public string _boneName;
Spine.Bone bone;
public Vector3 targetPos;
public Transform IK_Target;
SkeletonAnimation skeletonAnimation;
Weapon_Select rotater;
public float Tester;
private void Start()
{
rotater = GetComponentInParent<Weapon_Select>();
SkeletonAnimation skeletonAnimation = GetComponent<SkeletonAnimation>();
this.bone = skeletonAnimation.Skeleton.FindBone(_boneName);
skeletonAnimation.UpdateLocal += SkeletonAnimation_UpdateLocal;
}
void SkeletonAnimation_UpdateLocal(ISkeletonAnimation animated)
{
var bonePos = bone.GetWorldPosition(this.transform);
var direction = targetPos - bonePos;
float rotation = DirectionToRotation(direction, this.transform);
float parentRotation = bone.Parent.WorldRotationX;
bone.RotateWorld(rotation - parentRotation);
}
static float DirectionToRotation(Vector3 direction, Transform transform)
{
var localDirection = transform.InverseTransformDirection(direction);
return Mathf.Atan2(localDirection.y, localDirection.x) * Mathf.Rad2Deg;
}
private void LateUpdate()
{
skeletonAnimation.UpdateLocal += SkeletonAnimation_UpdateLocal;
}
Any help is appreciated!