Making a bone point to a target?

Started by
-1 comments, last by Kaze 11 years ago

This is in unity but uses vectors and quaternions that should work the same in most libraries.

http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.html

http://docs.unity3d.com/Documentation/ScriptReference/Vector3.html

I want to make a bone look at a target point but have two problems.

1: unity uses z+ forward and blender uses x-

2: the look at should avoid excessive twisting of the bone.


public class BoneLookAt : MonoBehaviour {
	

	
	// Use this for initialization
	void Start () {
		//save rest data
		restRot = this.transform.localRotation;
		
			//filter input
		Foward.Normalize();
		FowardQuaternionInverse = Quaternion.Inverse( Quaternion.LookRotation(Foward));
		
		
		
		
	}
	public Vector3 Foward = new Vector3(-1,0,0);
	private Quaternion FowardQuaternionInverse;
	
	private Quaternion restRot;
	
	public Vector3 GetTargetPosition(){
		return this.transform.position +  (this.transform.parent.rotation * restRot) * Foward;
	}
	public Quaternion LookAt(Vector3 v){
		//,transform.InverseTransformDirection(Vector3.up)
		 return  Quaternion.LookRotation(v-this.transform.position) * FowardQuaternionInverse;
		
	}
	
	private Vector3 ang = new Vector3();

	void Update () {
		Debug.DrawLine(this.transform.position,this.transform.position + this.transform.rotation * Foward);
		
		
		
		
		//
		this.transform.rotation = LookAt(GetTargetPosition());
		

		
		
		
		
	}
}

This topic is closed to new replies.

Advertisement