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 MarioGraceZ22 · Jan 09, 2015 at 11:32 AM · cameratransformbuttontransform.rotate

Why does transform.rotate() do this?

This is the issue: I am using JavaScript and I am trying to make my camera go to different positions when pressing various buttons. It works fine with buttons 1 and 2, but when pressing button 3 it makes the camera begin to turn incessantly with no apparent goal, practically going crazy. Here is the code, if you could help me:

 #pragma strict
     var screenUp : boolean = false;
     var cameraPosition : int = 0;
     var storePosition : int;
     var showCameraGUI : boolean = false;
     var cameraWindow : Rect = Rect(300,100,400,300);
 
 function OnGUI(){
     if(GUI.Button(Rect(100,100,100,100), "Screen")){
         if(screenUp == false){
             cameraPosition = storePosition;
             screenUp = true;
         }
         else if(screenUp == true){
             storePosition = cameraPosition;
             screenUp = false;
         }
     }
     if(showCameraGUI){
         GUI.backgroundColor = Color.clear;
         cameraWindow = GUI.Window(0, cameraWindow, CameraFunction, "Camera");
     }
 }
 
 function Update () {
     if(screenUp){
         showCameraGUI = true;
     }
     else{
         showCameraGUI = false;
         cameraPosition = 0;
     }
     if(cameraPosition == 0){
         Camera.main.transform.position.x = -0.5886186;
         Camera.main.transform.position.y = 0.2869148;
         Camera.main.transform.position.z = -4.973199;
         Camera.main.transform.rotation.x = 0;
         Camera.main.transform.rotation.y = 0;
         Camera.main.transform.rotation.z = 0; 
     }
     else if(cameraPosition == 1){
         Camera.main.transform.position.x = 3.509071;
         Camera.main.transform.position.y = 0.2869148;
         Camera.main.transform.position.z = -4.973199;
         Camera.main.transform.Rotate(0,0,0);
     }
     else if(cameraPosition == 2){
         Camera.main.transform.position.x = -4.109085;
         Camera.main.transform.position.y = 0.2869148;
         Camera.main.transform.position.z = -4.973199;
         Camera.main.transform.Rotate(0,0,0);
     }
     else if(cameraPosition == 3){
         Camera.main.transform.position.x = -3.526589;
         Camera.main.transform.position.y = 3.941321;
         Camera.main.transform.position.z = 0.6447246;
         Camera.main.transform.Rotate(15,90,0);
     }
 }
 
 function CameraFunction (windowID : int){
     GUI.backgroundColor = Color.cyan;
     if(GUI.Button(Rect(100,100,80,50), "Camera 1")){
         cameraPosition = 1;
     }
     if(GUI.Button(Rect(100,170,80,50), "Camera 2")){
         cameraPosition = 2;
     }
         if(GUI.Button(Rect(200,100,80,50), "Camera 3")){
         cameraPosition = 3;
     }
 }

Comment
Add comment · Show 3
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 Graham-Dunnett ♦♦ · Jan 09, 2015 at 11:33 AM 0
Share

You know that "spastically" isn't an accepted word? I didn't reject your question, believing that you'd quickly edit your question.

avatar image MarioGraceZ22 · Jan 09, 2015 at 02:30 PM 0
Share

So, you would have rejected my WHOLE question due to one word?

avatar image MarioGraceZ22 · Jan 11, 2015 at 09:23 PM 0
Share

Is there any way I could achieve my desired result, which is having it rotate to a certain degree instantly, by using transform.rotation?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Graham-Dunnett · Jan 09, 2015 at 11:34 AM

Presumably line 57 gets called every frame.

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 kburkhart84 · Jan 11, 2015 at 11:34 PM 0
Share

The above answer is the actual cause, though I'll explain what is happening. The script is in the update function, and so is getting called each frame. The function "Rotate" simply rotates from whatever angle it currently is at. Notice in the other if statements you only put 0 as the arguments, so it didn't make any change at all. It works relative to whatever the angle is at the moment, not an actual "Set" of the rotation. But then in the first if statement you directly set the x/y/z of the rotation, which I believe is what you want. So, you should do the same in the other if statement.

The other thing is that you only want to set the position and rotation once, not directly in the update function usually. Once you set the position, unless you or the physics engine does something, it won't change on you. So when you are checking the input, and setting the variable for the position, just do the actual position/rotation change there. That way, the code only gets ran once, not in every frame in the update function.

avatar image MarioGraceZ22 · Jan 12, 2015 at 04:28 AM 0
Share

I did use transform.rotation for all of them at the start, hence the first one doing that, but it gave me an undesired result, setting the camera's rotation to a completely different angle from what I had asked. I did assume the Update function to be the issue with transform.rotate, but I didn't know how to fix it. If you could enlighten me on how I could get transform.rotation to work properly, I would greatly appreciate it.

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

transform.Rotate For an Certain Amount of Time 1 Answer

How to spawn objects on a restricted sphere surface relative to camera? 1 Answer

getting the position of player for the minimap to follow 1 Answer

Setting a cube to be exactly size of intersecting camera view plane 1 Answer

GUI.Button child of the camera . 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