Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CallToAdventure · Nov 25, 2010 at 11:31 AM · transformrotatesmooth

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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; } } }

}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CallToAdventure · Nov 25, 2010 at 06:19 PM 0
Share

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.

avatar image Maarten · Nov 25, 2010 at 06:27 PM 0
Share

Please add the following above the public class:

using UnityEngine;

avatar image
0

Answer by Maarten · Nov 25, 2010 at 01:48 PM

Use a Vector3.Lerp(CurrentRotation,WantedRotation,(Speed * Time.deltaTime)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CallToAdventure · Nov 25, 2010 at 05:27 PM 0
Share

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.

avatar image
0

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 !

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Maarten · Nov 25, 2010 at 06:51 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

smooth fps movements using joystick 1 Answer

Rotate z and y together? 1 Answer

How to rotate without a target vector 1 Answer

Rotating a character smoothly 0 Answers

Orientation Vs. Rotation: How do I specify more than 360deg rotation for a quaternion, in the editor? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges