- Home /
Rotating an object around Z axis using Lerp
I have spent a few hours going through different pieces of code and have been unable to find anything that limits the rotation to just one axis.
This is what I have so far... explanation to follow.
void Update(){
if(gotTarget)
{
i += (speed * Time.deltaTime);
transform.rotation = Quaternion.Slerp(start, target, i);
if (transform.rotation == target)
{
i = 0f;
gotTarget = false;
fireMissile();
}
}
}
public void giveTarget(Vector3 pointIn){ if(!gotTarget)
{
gotTarget = true; start = transform.rotation;
Ray r = Camera.main.ScreenPointToRay(pointIn);
point = r.origin;
point.z = transform.position.z;
target = Quaternion.LookRotation(point - transform.position);
}
}
the pointIn is mousePosition. transform.rotation == target is not working 100% at the moment.
the idea is when the object (a turret) is given an input, it will rotate around the Z to face the input position with its top (of a cylinder) smoothly using Lerp. when it reaches its final rotation, it will fire, then be ready for the next input.
however, the turret seems to be rotating through all angles in a peculiar way until.
i have uploaded a version to dropbox for the rotation to be seen. and you may see it not fire everytime as an if statement doesn't always work. https://dl.dropbox.com/u/34478950/Game/test/WebPlayer/WebPlayer.html
any help appreciated
Answer by suttsc · Jul 19, 2012 at 10:29 AM
I found a solution to my problem on this forum page. http://forum.unity3d.com/threads/36377-transform.LookAt-or-Quaternion.LookRotation-on-1-axis-only Just mentioning for other people searching for an answer to a similar problem.
Answer by whydoidoit · Jul 18, 2012 at 04:58 PM
Just use a Mathf.Lerp and only apply it to the Z axis:
var r = transform.eulerAngles;
transform.rotation = Quaternion.Euler(r.x, r.y, Mathf.LerpAngle(start, target, i));
start and target are quaternions so invalid arguments in $$anonymous$$athf.Lerp/LerpAngle I'll give it a go using start and target as floats from the z's i can obtain. edit: Nope that just buggered it up
You probably want to Slerp then...
http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Slerp.html
Already tried slerp, unless you meant ins$$anonymous$$d of $$anonymous$$athf.Lerp, but it returns a Quaternion which would fit.
Your answer
Follow this Question
Related Questions
Creating a multiple part turret what locks onto certain axis. 4 Answers
Rotation Lerp 2 Answers
Quaternion LookRotation/Slerp Axis Lock 3 Answers
90 Degree stopping rotation on y-axis issue 0 Answers
How to Increase Height of Pendulum? 1 Answer