- Home /
Rotate object from localRotation A to localRotation B at rate C.
Afternoon all!
This should be simple, yet I seem to be struggling with it. Scenario is I have a capsule character, a child RotationPivot empty inside him, and a WeaponPoint (child of RotationPivot) outside him.
All I want to do, is if(Input.GetKeyDown("mouse 0")), smoothly rotate RotationPivot from local (0,0,0) to local (0,-180,0) at a variable speed, and stop. Doing so would be simple script-based swing of the WeaponPoint across the front of the Capsule. Like I said, should be simple. :p
I've looked at Quaternion.RotateTowards and Vector3.RotateTowards, and seem to be having trouble getting them to work right. (Put simply, they're not moving anything at all.) Is there a better function to use for this, or am I simply inept? lol
Quaternion.Lerp looks like a good candidate, so I'm testing that now.
As I suspected, I got it working with Quaternion.Lerp acceptably. If there's a better way though, I'm all ears. :p
You should post the solution to your problem as an answer. That was others can see exactly what you did if they have the same issue and others can still post other solutions.
What I have 'works', but the question is specifically to ask if there's a better way. This one has a $$anonymous$$or issue that it slows down as it approaches rotation B.
Answer by William_Weaver · May 06, 2014 at 02:03 AM
Apologies for the vague answer. It was a year ago when I was brand new to scripting. ^_^; I've pulled up the full script I used after I got it working, and I can confidently say that I would do it all differently. My code was just... bad. So I won't confuse the subject by posting it all here. BUT! Here's the important part that made the pivot rotate:
function SwingWeapon()
{
swingEnd = Quaternion.Euler(0,180,0);
swingDuration = 0.35;
while(swingDuration > 0)
{
swingDuration -= Time.deltaTime;
swingPivot.localRotation = Quaternion.Lerp(swingPivot.localRotation, swingEnd, swingSpeed);
yield;
}
}
Using other helper functions to receive click input and call this function, the above made the pivot (a variable of type transform, assigned in the inspector to the empty transform mentioned in the original post) rotate from it's current rotation to the specified end rotation.
You'll want to look up Lerp and Quaternion.Euler, but it's my understanding the the mechanic I was working towards is usually handled by rigging and animation. This was a very rough workaround to make a simple character swing a weapon horizontally in front of it 180 degrees.
If you want a constant speed movement use Lerp, if you want it to be slower towards the start and the end use Slerp.
Can you please post a line example on how you achieved that? It is, on how to use Quaternin.Lerp to do that? I am looking for the exact same solution and would rather avoid having to post a duplicate question, in case you can provide the solution you had found. Thanks!
Answer by andyastro · Sep 19, 2015 at 08:55 PM
You should always post a detailed answer when you find one, in case other people have the same question and should not suffer all over again to find a proper solution. So, in case anyone else wonder how did you use Quaternion.Lerp to achieve what was asked in the OP, I send here an example I am currently employing:
//targetedlocalRotation should be a Quaternion:
go.transform.localRotation = Quaternion.Lerp(go.transform.localRotation, targetedolcaolRotation,speed);