- Home /
Simple smooth rotation problem
Hi everyone,
I am having a simple issue here I can't seem to get around whatever I try. Here's the code:
if (Input.GetKeyDown("w"))
{
aileronR.transform.Rotate(35, 0, 0);
What this does, when I keep the "w" pressed the gameObject rotates to 35 degrees on its X axis. What I actually look for is to get to the full 35 degrees angle in time; so if I press the "w" key the gameObject should rotate smoothly; I don't mind if on keyUp it snaps back to 0 0 0. I tried looking at Time.DeltaTime but nothing worked. However,when I tried this part of the script separately I achieved the exact end result; Here's the script that works.
var aileronRrotation = 15;
var aileronR : GameObject;
function Update () {
if (Input.GetKey ("a")) {
aileronR.transform.Rotate (aileronRrotation * Time.deltaTime, 0, 0);
}
if (Input.GetKeyUp ("a")) {
aileronR.transform.rotation = Quaternion.identity;
}
}
It could have something to do with the KeyUp/Key down part. I'll try this later today.
Thanks in advance!
Answer by CallToAdventure · Nov 30, 2010 at 11:36 AM
This is not really an answer since the problem is not entirely solved. But this is the code that works when my plane is not flying; I attached a simple rotate animation to the plane just to see if the controls will rotate locally and they work fine; However, when I put the code in my plane script (or use it as a separate controls script) the rotation is local to .. something else; meaning, they keep the pivot but they rotate - i think - relative to camera.
this is the code.
var smooth = 2.0;
var tiltAngle = 30.0;
var aileronR : GameObject;
var aileronL : GameObject;
var elevator : GameObject;
var rudder : GameObject;
function Update () {
var tiltAroundX = Input.GetAxis("Vertical") * tiltAngle; var aileronRtarget = Quaternion.Euler (tiltAroundX, 0, 0); var aileronLtarget = Quaternion.Euler (tiltAroundX, 0, 0); var elevatorTarget = Quaternion.Euler (tiltAroundX, 0, 0);
aileronR.transform.localRotation = Quaternion.Slerp(transform.localRotation, aileronRtarget, Time.deltaTime * smooth);;
aileronL.transform.localRotation = Quaternion.Slerp(transform.localRotation, aileronLtarget, Time.deltaTime * smooth);;
elevator.transform.localRotation = Quaternion.Slerp(transform.localRotation, elevatorTarget, Time.deltaTime * smooth);;
var tiltAroundY = Input.GetAxis("Horizontal") * tiltAngle; var rudderTarget = Quaternion.Euler ( 0, tiltAroundY, 0);
rudder.transform.localRotation = Quaternion.Slerp(transform.localRotation, rudderTarget, Time.deltaTime * smooth);; }
Another issue I have with this code is that I cannot use 2 axis; example - when I turn lean right, left aileron should be rotating up and the right aileron should be rotating downwards. I am just not sure where the problem is. Any help is appreciated.
Answer by Maarten · Nov 25, 2010 at 05:53 PM
Hi, please try the following code (C#, but isnt hard to convert)
public class Test : MonoBehaviour
{ public GameObject aileronR; public float aileronRoration = 15; public float speed = 5;
private float currentRotation = 0; private bool rotationActive = false;
void Update() { if (Input.GetKeyDown("w")) { rotationActive = true; currentRotation = aileronR.transform.eulerAngles.x; } if (rotationActive) { currentRotation = Mathf.Lerp(currentRotation, aileronRoration, (Time.deltaTime * speed)); aileronR.transform.eulerAngles = new Vector3(currentRotation, aileronR.transform.eulerAngles.y, aileronR.transform.eulerAngles.z); if (Mathf.Floor(currentRotation) == aileronRoration) { rotationActive = false; } } }
}
I made a new c# script named Test.cs and copied your code. public class Test : $$anonymous$$onoBehaviour gives me an error. You got me there, while I can understand some javaScript, C# is totally new to me. I am trying to learn though, that's why I am asking around :) Thanks.
Please add the following above the public class:
using UnityEngine;
Answer by Maarten · Nov 25, 2010 at 01:48 PM
Use a Vector3.Lerp(CurrentRotation,WantedRotation,(Speed * Time.deltaTime)
I am not sure how to add the speed variable; my code looks like this:
aileronR.transform.Rotate(Vector3.Lerp (0, -35, Speed * Time.deltaTime, 0, 0));
If you're running out of patience with me, well, that's because I am an artist, not a programmer :)
Thanks in advance.
Answer by CallToAdventure · Nov 25, 2010 at 06:50 PM
Works awesome, the feeling is a lot better. Before that it kind of snapped in place :)
I'll write the entire controls script in c# for now and see how it works. Do I need currentRotation and rotationActive variables for each aileron and the other controls? Thanks again !
Yeah you do. The currentRotation is to remember the "state" of the rotation. And the rotationActive is needed to see if it should rotate.
Your answer
![](https://koobas.hobune.stream/wayback/20220613084507im_/https://answers.unity.com/themes/thub/images/avi.jpg)