Sign in to follow this  
Kaze

Making a bone point to a target?

Recommended Posts

Kaze    948

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());
		

		
		
		
		
	}
}

Edited by Kaze

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this