How to rotate fluently/smoothly?
Hello!
At the moment I am making a game like Legend of Grimrock or the old type of games. I am wondering how I can do the fluent roating? At the moment I rotate like this:
public void RightTurn()
{
this.transform.Rotate(Vector3.up, 90);
this.transform.forward = this.transform.forward.Round(0);
this.Turned();
}
private void Turned()
{
this.canWaitIntersection = false;
this.waitingIntersection = false;
this.OnPlayerTurned();
}
private void OnPlayerTurned()
{
Action<Vector3> handler = this.PlayerTurned;
if (handler != null)
handler(this.player.transform.forward);
}
Any ideas and/or suggestions? :) Thanks in advance!
Answer by mj321 · Aug 29, 2016 at 09:58 PM
Here's a script that rotates an object by 90 degrees if you press space. The rotation itself happens inside a CoRoutine. I'm still inexperienced in Unity and i suspect there's an easier way to do it. But that's how i'd do it today :)
using UnityEngine;
using System.Collections;
public class Rotator : MonoBehaviour
{
void Update ()
{
if( Input.GetKeyDown(KeyCode.Space) )
{
StartCoroutine( Rotate(Vector3.up, 90, 1.0f) );
}
}
IEnumerator Rotate( Vector3 axis, float angle, float duration = 1.0f)
{
Quaternion from = transform.rotation;
Quaternion to = transform.rotation;
to *= Quaternion.Euler( axis * angle );
float elapsed = 0.0f;
while( elapsed < duration )
{
transform.rotation = Quaternion.Slerp(from, to, elapsed / duration );
elapsed += Time.deltaTime;
yield return null;
}
transform.rotation = to;
}
}
Hey man i used your code and it works awesome just like what i was looking for. Can i use it in my commercial game? Your code is awesome it's working really good and smooth!!! Thank you so much!
Thanks so much! I have 1 point to give and it's yours now!
Thank you it works fine! :)
The only problem I have now is, that it recognizese treasures some how in the wrong angle. So if I am walking to wards the treasure and turn right to face it, nothing happens, if I turn right again than it recognizes the treasure. I am not sure why because the camera is rotating right and is facing in the right direction? I forget to mention my CheckFacingDirection(); method:
private void CheckFacingDirection()
{
RaycastHit hitInfo;
if (Physics.Raycast(this.transform.position, this.transform.forward, out hitInfo, 1))
{
GameObject hitObject = hitInfo.collider.gameObject;
TreasureChest tChest = hitObject.GetComponent<TreasureChest>();
if (tChest != null && !tChest.IsOpened)
{
this.interactionPanel.gameObject.SetActive(true);
this.interactionPanel.UpdateContent(tChest);
return;
}
}
this.interactionPanel.gameObject.SetActive(false);
}
The Coroutine lets the rotation happen in the background and Turned() gets called immediately. Try moving the Turned() call to the end of the Coroutine (After transform.rotation = to; )
New to Unity as well and ended up using this script as well but I've noticed a small issue. It would seem that if you were to hit the button twice it undoes the last animation. For instance, if you hit it once, wait until it's at 10 degrees and then hit it once more it ends up resting at 100 degrees rather than 90 degrees or 180 degrees. I've decided to try using this script to make a simple camera dolly of sorts by attaching it to an empty that the camera is tied to. It rotates at 90 degree angles around the character when you press an input. I've also got a separate input to rotate the object 90 degrees in the opposite direction. It works if you give it time to complete the rotation but if you double-tap the buttons or hit both inputs then the camera gets a little confused. If the player were to mash the two then it just bugs out completely. Do you happen to know if there is a way to force it to fully execute the rotation before accepting another input? Or if not, is it possible to force the input onto a stack of sorts (Hitting it twice will execute the first then the second, etc.). Thanks for the help, sorry for the bother.
I had just realized, just by adding StopAllCoroutines(); to the end of the script it would do exactly that. Pardon the question earlier.
The code works fine...thanks for that...But how to make the rotation whilst moving forward and also after rotation forward movement should be based on continuous holding of mouse button (Input.mouseButton(0))...Help me, thanks in advance :-)
So, axis = Vector3.up and angle is 180. Somehow the X of my object also rotates, but I only need it to rotate around Y axis. Any help? Thanks!
Your answer
Follow this Question
Related Questions
Move an object by rotation 0 Answers
Hard coded rotation of child results in weird arc rotation when parent is rotated? 0 Answers
How to access y rotation of an object as a variable or value 1 Answer
Problems with rotating the player on the Y axis C# 0 Answers
MouseOrbitImproved slanted orbit 0 Answers