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
1
Question by Reaper1121 · May 03, 2015 at 05:03 PM · c#rotationrotation detection

Camera rotation stop if x is 50

Hello. I am creating a game and i want to stop the camera rotation to x more if reached 50 x. I had this in testing game that is working:

 void Update () {
     
         if(Input.GetAxis ("Mouse ScrollWheel") > 0f )
         transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
 
         if(Input.GetAxis ("Mouse ScrollWheel") < 0f )
         transform.Translate (Vector3.back * moveSpeed * Time.deltaTime);
 
         if (transform.position.y > 14)
             transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
 
         if (transform.position.y < 2)
             transform.Translate (Vector3.back * moveSpeed * Time.deltaTime);


The code is checking y position not rotation if the height is more than 14 the camera stopped did not move more higher same with the bottom if reached height 2 camera stopped did not go more than 2 down. But now with rotation i changed the same code to check for rotation x the same way but it does not work but it should. Here is my rotation check:

 using UnityEngine;
 using System.Collections;
 
 public class CameraControl : MonoBehaviour {
 
     public float cameraspeed = 30f;
     
     // Update is called once per frame
     void Update () {
     
         if(Input.GetKey(KeyCode.UpArrow))
             transform.Rotate (Vector3.left * cameraspeed * Time.deltaTime);
 
         if(Input.GetKey(KeyCode.DownArrow))
             transform.Rotate (Vector3.right * cameraspeed * Time.deltaTime);
     
         if (transform.rotation.x > 50)
             transform.Rotate (Vector3.left * cameraspeed * Time.deltaTime);
    }
 }

Soo i hope someone can find the problem and tell me how to fix this rotation x checker. Thanks.

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
0
Best Answer

Answer by Addyarb · May 03, 2015 at 06:01 PM

Change line 17 to:

 if (transform.rotation.x > .4229)

Unity internally measures angles in quaternion angles, which are way different than the angles you see in the transform/inspector (known as Euler angles).

Alternatively, you can convert them for your convenience.

Like so:

 float angle = transform.rotation.eulerAngles.x; // define our angle up here..
 
         if(Input.GetKey(KeyCode.UpArrow))
             transform.Rotate (Vector3.left * cameraspeed * Time.deltaTime);
         
         if (Input.GetKey (KeyCode.DownArrow)) {
             transform.Rotate (Vector3.right * cameraspeed * Time.deltaTime);
         }
         if (angle > 50)
             transform.Rotate (Vector3.left * cameraspeed * Time.deltaTime);
         }



Comment
Add comment · Show 7 · 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 Reaper1121 · May 03, 2015 at 06:08 PM 0
Share

I have a error using the float angle. This is my updated code:

 using UnityEngine;
 using System.Collections;
 
 public class CameraControl : $$anonymous$$onoBehaviour {
 
     public float cameraspeed = 30f;
 
     float angle = transform.rotation.eulerAngles.x;
     
     // Update is called once per frame
     void Update () {
     
         if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow))
             transform.Rotate (Vector3.left * cameraspeed * Time.deltaTime);
 
         if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.DownArrow))
             transform.Rotate (Vector3.right * cameraspeed * Time.deltaTime);
     
         if (angle > 50)
             transform.Rotate (Vector3.left * cameraspeed * Time.deltaTime);
    }
 }

And here is the error: Assets/Cookie Clicker/Scripts/Camera/CameraControl.cs(8,26): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform' How do i fix it?

avatar image Reaper1121 · May 03, 2015 at 06:12 PM 0
Share

I tried if (transform.rotation.x > .4229) i still can rotate left more than 50. If you need i can take a video or a screenshot

avatar image Zinatulin · May 03, 2015 at 06:21 PM 0
Share

On line 17 try this: if(transform.eulerAngles.x > 50) { //Rest of the code }

avatar image Reaper1121 · May 03, 2015 at 06:26 PM 0
Share

Yes it worked thanks. :)

avatar image Reaper1121 · May 03, 2015 at 06:39 PM 0
Share

Hey if you can read this comment maybe you know how to fix if i move the camera up if it reach 0 it goes down from 380 how do i make it dont do that and if i reach 0 from going up go to -1... ?

Show more comments
avatar image
2

Answer by Zinatulin · May 04, 2015 at 12:12 PM

I think you should try this on line 17:

 if(transform.eulerAngles.x > 50)
 {
      transform.rotate(Vector3.left * cameraspeed * 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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Distribute terrain in zones 3 Answers

Know if an object has been rotating 180 or 360 degrees? 0 Answers

Multiple Cars not working 1 Answer

Grid-based game 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