Weird rotation problem
Posted
by
Phil
on Game Development
See other posts from Game Development
or by Phil
Published on 2011-02-06T18:51:06Z
Indexed on
2011/02/06
23:36 UTC
Read the original article
Hit count: 307
I'm creating a simple tank game. No matter what I do, the turret keeps facing the target with it's side. I just can't figure out how to turn it 90 degrees in Y once so it faces it correctly. I've checked the pivot in Maya and it doesn't matter how I change it.
This is the code I use to calculate how to face the target:
void LookAt()
{
var forwardA = transform.forward;
var forwardB = (toLookAt.transform.position - transform.position);
var angleA = Mathf.Atan2(forwardA.x, forwardA.z) * Mathf.Rad2Deg;
var angleB = Mathf.Atan2(forwardB.x, forwardB.z) * Mathf.Rad2Deg;
var angleDiff = Mathf.DeltaAngle(angleA, angleB);
//print(angleDiff.ToString());
if (angleDiff > 20) {
//Rotate to
transform.Rotate(new Vector3(0, (-turretSpeed * Time.deltaTime),0));
//transform.rotation = new Quaternion(transform.rotation.x, transform.rotation.y + adjustment, transform.rotation.z, transform.rotation.w);
}
else if (angleDiff < 20) {
transform.Rotate(new Vector3(0, (turretSpeed * Time.deltaTime),0));
//transform.rotation = new Quaternion(transform.rotation.x, transform.rotation.y + adjustment, transform.rotation.z, transform.rotation.w);
}
else {
}
}
I'm using Unity3d and would appreciate any help I can get! Thanks!
© Game Development or respective owner