- Home /
Two rotations at the same time, on the same axis.
I need help regarding two rotations at the same time, on the same axis. One vector is approaching 90 degrees. When I press up, it should smoothly approach the angle 45 degrees above it, such that HOLDING up will still be going down. Here is a rough sketch of what I mean: (black = vector without up, red = vector approaching 45 above black)
Answer by andrew-lukasik · Aug 05, 2021 at 09:31 AM
Ok. As far as I can understand you are probably asking how to combine rotations
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TwoRotations : MonoBehaviour
{
Quaternion _main = Quaternion.identity;
Quaternion _auxiliary = Quaternion.identity;
Coroutine _coroutine = null;
void Update ()
{
// animate main rotation every frame;
_main = _main * Quaternion.AngleAxis( 10f*Time.deltaTime , transform.up );
// animate auxiliary rotation on player input:
if( Input.GetKey(KeyCode.UpArrow) )
{
if( _coroutine!=null ) StopCoroutine( _coroutine );
_coroutine = StartCoroutine(
AnimateAuxiliaryRotation( _auxiliary , Quaternion.Euler(0,45f,0) , 1.5f )
);
}
if( Input.GetKey(KeyCode.DownArrow) )
{
if( _coroutine!=null ) StopCoroutine( _coroutine );
_coroutine = StartCoroutine(
AnimateAuxiliaryRotation( _auxiliary , Quaternion.Euler(0,0,0) , 1.5f )
);
}
// combine rotations:
transform.rotation = _main * _auxiliary;
}
IEnumerator AnimateAuxiliaryRotation ( Quaternion src , Quaternion dst , float duration )
{
float time = 0;
do
{
_auxiliary = Quaternion.Slerp( src , dst , time/duration );
time += Time.deltaTime;
yield return null;
}
while( time < duration );
}
}
Your answer
Follow this Question
Related Questions
Problem about Rotating the Character 1 Answer
How to add rotation to current rotation over time? 2 Answers
Rotation jumping to a different rotation after rotating a certain amount 0 Answers
Rotate child object between specific Y-rotation values 1 Answer
How do you smoothly transition/Lerp into a new rotation? 3 Answers