Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Making a bone point to a target?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 Kaze   Members   -  Reputation: 948

Like
0Likes
Like

Posted 13 April 2013 - 04:33 PM

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, 13 April 2013 - 04:33 PM.


Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS