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 VictoryX · Feb 27, 2013 at 04:30 PM · cameratransformpositionrotatejava

Changeing Camera Position

Hello, again. I've been trying to get a camera to jump to certain positions when the player hits the arrows keys. Basically if the player were to continuously hit the right arrow it would cycle through all the different camera positions. For some reason though when the player hits the right arrow nothing happens. I'm assuming that the code is being executed, but it is all happening at once so it cycles through all the positions and ends up back at the front position. Not really sure how to get it to stop on each position and wait for player input. My Code below.

 var CameraFpos = true;
 var CameraBpos = false;
 var CameraRpos = false;
 var CameraLpos = false;     
 
  
 function FixedUpdate() {
 
 //Camera Right Spin from CameraFpos
     if (CameraFpos && Input.GetButton("Right"))
     {
     
         Camera.main.transform.position.x = 65.757;
         Camera.main.transform.position.z = 22.58412;
         Camera.main.transform.rotation = Quaternion.Euler(0,270,0);
         CameraFpos = false;
         CameraRpos = true;
     }
     
 //Camera Right Spin from CameraRpos
     if (CameraRpos && Input.GetButton("Right"))
     {
     
         Camera.main.transform.position.x = 18.0817;
         Camera.main.transform.position.z = 69.14645;
         Camera.main.transform.rotation = Quaternion.Euler(0,180,0);
         CameraRpos = false;
         CameraBpos = true;
     }
     
 //Camera Right Spin from CameraBpos
     if (CameraBpos && Input.GetButton("Right"))
     {
     
         Camera.main.transform.position.x = -32.8335;
         Camera.main.transform.position.z = 22.04136;
         Camera.main.transform.rotation = Quaternion.Euler(0,90,0);
         CameraBpos = false;
         CameraLpos = true;
     }
     
 //Camera Right Spin from CameraLpos
     if (CameraLpos && Input.GetButton("Right"))
     {
     
         Camera.main.transform.position.x = 18.9608;
         Camera.main.transform.position.z = -25.7975;
         Camera.main.transform.rotation = Quaternion.Euler(0,0,0);
         CameraLpos = false;
         CameraFpos = true;
     }
 }
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
2
Best Answer

Answer by AlucardJay · Feb 27, 2013 at 05:55 PM

Look at how your logic is organized.

if (CameraFpos && Input.GetButton("Right")) is true, then CameraRpos is set to true;

so then the next conditional becomes true : if (CameraRpos && Input.GetButton("Right"))

Even if you use Input.GetButtonDown, this is still recognized as it is still the same frame. So basically every if conditional in your script becomes true. The quick way to fix this is to use else if

Another consideration is you're checking for an input in every conditional. How about just check for the button first, if no button then nothing else needs to execute.

Finally, why is this all in FixedUpdate ? This is meant for physics steps, and as it runs at a different framerate to Update (and sometimes slower than Update depending on the time step or computing power). Also you should put as little code as possible in FixedUpdate. My advice for any camera movement is to apply it in LateUpdate, then everything else moves first, then the camera moves, this reduces shuddering/stuttering.

Here's another structure :

 if ( Input.GetButtonDown( "Right" ) )
 {
     if ( CameraFpos )
     {
         //
     }
     else if ( CameraRpos )
     {
         //
     }
     else if ( CameraBpos )
     {
         //
     }
     else if ( CameraLpos )
     {
         //
     }
 }



now having said all that, I would recommend you use an enum for your camera state, and a switch-case depending on that state. Search enum, switch (switch case), and state engine (state machine).

http://answers.unity3d.com/questions/41108/switch-case-with-enum.html

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 VictoryX · Feb 27, 2013 at 11:03 PM 0
Share

Worked great, thanks for the input, highly appreciated.

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera rotation around player while following. 6 Answers

Change position of camera on scene load? 1 Answer

When switching camera position... 1 Answer

Aircraft wing position 0 Answers

Scripting Issue With Top Down Mouse Movement 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