- Home /
Quaternion LookRotation
Hi guys - I'm making a tower defence game, yet I've come across a problem.
I have a Trigger Sphere Collider on my turrent, and when the enemy object comes in range, the tower follows and shoots. However, for some reason, the tower rotates in the X axis, which shouldn't happen - it should only rotate on the Y.
Any clues?
function Update() {
if (myTarget) {
if (Time.time >= nextMoveTime) {
CalculateAimPosition(myTarget.position);
turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime * turnSpeed);
}
if (Time.time >= nextFireTime) {
FireProjectile();
}
}
}
function CalculateAimPosition(targetPos : Vector3) {
var aimPoint = Vector3(targetPos.x + aimError, targetPos.y + aimError, targetPos.z + aimError);
desiredRotation = Quaternion.LookRotation(aimPoint);
}
function CalculateAimError() {
aimError = Random.Range(-errorAmount, errorAmount);
}
Answer by robertbu · May 23, 2013 at 02:52 PM
You say "shouldn't", but I don't see any code here that is intended to make that happen. LookRotation() will look at whatever position you specify. The typical way to solve this problem is to bring the y value of the target down to the level of the turret. But you have a second problem here. LookRotation() is looking for a zero based vector, not a position in world space. The code you have here will only work if your turret is at the origin.
So to fix the two problems you would have something like this:
targetPos = targetPos - transform.position;
var aimPoint = Vector3(targetPos.x + aimError, transform.position.y, targetPos.z + aimError);
Yea, with your fix, it has the turret ai$$anonymous$$g down, and unable to move back up.
Could it have something to do with the import settings, or the model itself? When I import, I have to change the rotation initially (which I don't understand, but always seems to be the case with 3DS $$anonymous$$ax models).
The Y rotation is fine, but the X also changes, however goes 'flat' (like it is supposed to) when the enemy is directly in the same spot X/Z - position in Y, the top of the dome/turrent moves towards the enemy, flipping vertically (so -Y it goes upside down).
Yes, initial rotation will make a huge difference. You model needs to have the turret facing positive 'Z' when the rotation is (0,0,0). You can compensate for this in Unity by making the turret a child of an empty game object. And rotating the visible turret to face positive 'Z' when the child has rotation of (0,0,0). The rotation script would then go on the empty parent game object.
Well that's what I did originally, but playing around with it now, I've noticed that the up direction has become +Z
I may play around with 3DS $$anonymous$$ax and try to re-import it.
EDIT: tried re-importing, same thing - tried empty game object, still rotates down (so on the X axis)
Answer by Classic-Niall · May 23, 2013 at 04:50 PM
Okay, figured it out.
Instead of Lerd, it is Slerp... and that's it - one letter difference :-D
Your answer
Follow this Question
Related Questions
how to aim an object at another object 1 Answer
Snap a direction vector 1 Answer
How to convert quaternion to vector3 with specific multiplication order? 5 Answers
Help Rotating a Player 0 Answers
How to add 2 Quaternions. 2 Answers