- Home /
object facing wrong way, Java script Quaternion
so i got an issue with a turret in my game, i want it to look at the person and shoot, obviously. Well its looking to the left (from its prospective, right from mine) when it should be looking at me. Here is the update function i have (LookAtTarget is a transform var set as the player)
function Update() { if(LookAtTarget) { var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
var seconds : int = Time.time;
var oddeven = (seconds % 2);
if(oddeven)
{
Shoot(seconds);
}
}
so yeah need a way to constantly make its rotation 90 degrees more to look at me, please help
Answer by duck · Apr 01, 2010 at 09:52 AM
Well, the best way of fixing this would be to fix the orientation of your model's pivot, in your 3D app.
If that isn't possible, you can adjust the target quaternion by adding a line like this. I've changed your var name to "targetRotation" because it's a better description of what the variable contains.
function Update() { if(LookAtTarget) { // calculate rotation towards target var targetRotation = Quaternion.LookRotation(LookAtTarget.position - transform.position);
// adjust rotation by 90 degrees around y axis
targetRotation *= Quaternion.Euler(0,90,0);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * damp);
var seconds : int = Time.time;
var oddeven = (seconds % 2);
if(oddeven)
{
Shoot(seconds);
}
}
}
(and use -90 instead of 90 if it's rotated in the wrong direction!)
thanks, i went in and tried to change the rotation in blender, but for some reason it just changed the rotation in unity too, not the actual models rotation, so i ended up rebuilding the turret to face the right direction in blender (from front view straight up is forward...) because i was tired of waiting :P still thank you and i'll remember this for later :D