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 MC HALO · Oct 22, 2011 at 02:12 AM · quaternion

How can i rotate a camera on its x or z axis using a Quaternion

Hello guys i would like to ask how can i make may camera rotate 60 degrees on its x axis when i press the Left Arrow key on the keyboard. I would be thankful if someone could help me out thank you very much in advance :)

Also one important thing how do i make it so that when i let go of the key the camera returns back to its normal position here is a script i did for gun movement but it did not work out when i tried the same for the camera rotation :)

         public var MoveAmount : float = 1;
         
                 public var MoveSpeed : float = 2;
                 
                         public  var GUN: GameObject;
                             public  var CAM: GameObject;
                         
                         
                             public var MoveOnX : float;
                             
                               public var MoveOnY: float;
                               
                                       public var DefaultPos : Vector3;
                                       
                                       public var NewGunPos : Vector3;
                                       
                                       public var ONOff : boolean = false;
                                       
                                       
         
 function Awake(){
 
 
   DefaultPos = transform.localPosition;
   ONOff = true;
  
 }
 
 
 
 function LateUpdate () {
 
 
    if(ONOff == true){
      
             MoveOnX = Input.GetAxis( "Mouse X") * Time.deltaTime * MoveAmount;
             MoveOnY = Input.GetAxis( "Mouse Y") * Time.deltaTime * MoveAmount;
             NewGunPos = new Vector3 ( DefaultPos.x+ MoveOnX, DefaultPos.y + MoveOnY, DefaultPos.z);
             GUN.transform.localPosition = Vector3.Lerp(GUN.transform.localPosition , NewGunPos, MoveSpeed * Time.deltaTime);
             
 
 }
 
 else{
 
    ONOff = false;
    
  GUN.transform.localPosition = Vector3.Lerp(GUN.transform.localPosition, DefaultPos , MoveSpeed *Time.deltaTime);
 
 
 }
 
 }
 
 
 
 
 
 
 
     
 
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Oct 22, 2011 at 11:15 AM

That's a good job for a coroutine. Call the coroutine RotateCam when the key is pressed, and it will do the full cycle: rotate 60 degrees around X in duration seconds, wait until key released, then return to the initial position in the same time.

var duration: float = 1.2; // duration in seconds private var orig: Quaternion; private var dest: Quaternion; private var rotateCamFlag = false;

function RotateCam(){ if (rotateCamFlag) return; // aborts if already rotating rotateCamFlag = true; // flag indicates it's rotating var start = transform.rotation; // save the start rotation... var dest = Quaternion.Euler(60, 0, 0) * start; // and calculate the destination var t: float = 0; while (t = 0){ t -= Time.deltaTime/duration; // t go back to 0 a little step transform.rotation = Quaternion.Slerp(start, dest, t); // rotate this little yield; // free Unity until next frame } rotateCamFlag = false; // rotation ended } To do the magic, just call RotateCam when left arrow pressed:

   if (Input.GetKeyDown("left")) RotateCam();

NOTE: This function always rotate 60 degrees, wait key up and rotates back, even if you release the key too soon.

EDITED: I tested the script above and it worked fine, but you can have the same effect without coroutines with the script below (values adapted to match the video that you linked):

private var duration: float = 0.6; // time in seconds private var orig: Quaternion; private var dest: Quaternion; private var t: float = 0;

function Update(){ // t > 0 when cycle running; avoid new cycle when t > 0 if (Input.GetKeyDown("left") && t

EDITED2: Well, this is a 3rd version - this time using Mouse X and returning to 0 rotation when the mouse stops. It seems that that's what your script does, but in this version the rotation can be changed from left to turn right and vice versa if the mouse is moved.

var duration: float = 0.2; // time in seconds var angle: float = 12; // angle to turn left or right var limit: float = 0.3; // set the sensitivity var speed : float = 100; // isn't used in this code

private var orig: Quaternion; private var dest: Quaternion; private var dir: float;

function Start(){ orig = transform.localRotation; }

function Update(){ // filter the mouse input with Lerp dir = Mathf.Lerp(dir, Input.GetAxis("Mouse X"), 5*Time.deltaTime); dest = orig; // define the dest rotation according to the dir variable if (dir > limit) dest = Quaternion.Euler(0, 0, -angle); if (dir < -limit) dest = Quaternion.Euler(0, 0, angle); transform.localRotation = Quaternion.RotateTowards(transform.localRotation, dest, angle*Time.deltaTime/duration); }

Comment
Add comment · Show 14 · 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 MC HALO · Oct 23, 2011 at 10:47 AM 0
Share

it does not work i attached the script to the players camera and when i call it from another script there is nothing going on ?

avatar image aldonaletto · Oct 23, 2011 at 12:53 PM 1
Share

It should work. It should be part of the camera script, or a new script attached to the camera. $$anonymous$$aybe you must change transform.rotation to transform.localRotation. Anyway, I'll test this script and return asap.

avatar image aldonaletto · Oct 23, 2011 at 02:38 PM 1
Share

The script worked fine in my tests, but I edited my answer and added an alternative that doesn't use coroutines. You can attach this script to your camera, and it will work in a way similar to the video linked. NOTE: If you have another camera control script it can fight with this one for the camera control, and one of them will not work! Disable any other camera script to test this one.

avatar image aldonaletto · Oct 24, 2011 at 05:44 AM 1
Share

t = $$anonymous$$athf.Clamp(t, 0, 1); clamps the value of t to the range 0..1, needed for the Slerp function. In this script, the variable t is constantly incremented (when left pressed) or decremented (left released), and if not clamped it could reach too high or too low values, taking a long time to effectively turn the camera.

avatar image aldonaletto · Nov 06, 2011 at 05:03 AM 1
Share

Yes, the dir variable holds a smoothed version of Input.GetAxis("$$anonymous$$ouse X"). Each Update, this script reads Input.GetAxis and "filter" its value in the variable dir using Lerp; the dir value stays at 0 if mouse stopped, and goes to -1 or +1 when mouse moving. Right after reading the mouse axis, the script uses the smoothed dir value to select the dest rotation: orig if dir near to 0, -angle if > limit and angle if < -limit. Anyway, RotateTowards is called each Update to move in the dest direction - if it's orig, the object returns or gets stopped at the original rotation, else it rotates to the desired angle.
Finally, the value 5 multiplied by Time.deltaTime defines the smoothing effect of the Lerp filter; if you increase it too much, the rotation may become jerky; if you reduce too much, the controls will become too slow.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to prioritise quaternions over transforms? 0 Answers

Fixing the rotation around a particular axis using Quaternions? 2 Answers

Quaternion.Lerp is not working ,causing weird rotations 0 Answers

How do I make a 2D sprite smoothly rotate upright? 1 Answer

rotations smooth in scene view but not game view 0 Answers


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