Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Arthur 1 · May 20, 2011 at 03:26 AM · camerarotationinputdeltatime

Camera Rotation Script

Hey UnityAnswers,

I'm looking for some advice on some code I have. Basically I have 4 cubes, each being a different colour (Red, Blue, Green and Yellow). In the center is an empty gameObject, with the mainCamera parented. When the gameObject rotates, so does the mainCamera. Right now I have input controls to rotate the empty gameObject either left or right, showing each of the cubes.

Here is what my scene looks like just for visualization : http://imgur.com/79ibA

What I'm trying to get is a smooth curve between 90 degree intervals. I don't want it to change like a light switch. I'm not a good programmer, but I'm thinking it involves either for loops, time.Deltatime or transform.Rotate. Let know if you can help me out. The code I'm working on is below.

     var goodDegs = 30; // degrees per second
     var badDegs = -30; // degrees per second
 
 
 function Update () {
     if (Input.GetKeyDown("left")){
         for (var n = 0; n < 90; n++)
         {
             transform.Rotate(0, goodDegs * Time.deltaTime, 0);
         }
     }
     if (Input.GetKeyDown("right")){
         for (var m = 0; m < 90; m++)
         {
             transform.Rotate(0, badDegs * Time.deltaTime, 0);
         }
     }
 }

or an older version:

 var rotationPositive = 90;
 var rotationNegative = -90;
 
 
 function Update () {
     if (Input.GetKeyDown("left")) 
         {
             transform.Rotate(Time.deltaTime, rotationPositive, 0);
 
         }
     if (Input.GetKeyDown("right"))
         {
             transform.Rotate(Time.deltaTime, rotationNegative, 0);
             
         }
     }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DaveA · May 20, 2011 at 04:13 AM

Have a look at this: http://unity3d.com/support/documentation/ScriptReference/Mathf.MoveTowardsAngle.html

You'll want to set a 'target rotation degrees' OnKeyDown, rather than using a for loop. Anything you do in one frame will all happen in that frame, so you need to spread it across frames. Time.deltaTime is your friend.

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
0

Answer by maaz0011 · Sep 21, 2017 at 11:34 AM

public class swipebutton : MonoBehaviour { public Camera cam;

 void Update()
 {
    
     cam.transform.Rotate(Vector3.up, 20.0f * Time.deltaTime);
 }


}

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Need help on the 3ds max style camera control 0 Answers

Quaternion Slerp Sanity Check 1 Answer

Orthographic Camera rotation on world y-axis but also rotates z-axis slightly? 0 Answers

how to clamp y axis with Quaternion.Euler in unity 1 Answer

How to change object direction when Camera Rotates 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