• 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 NoGo86 · Aug 23, 2012 at 05:27 AM · cameratransformobjectmaincamera

How to get the constant Y rotation of the camera

Question.... So I am trying to figure out how to change my movement controls depending on the Y rotation of the Camera.

This would be the code for just one of the 4 sides, but it seams to only check the Cameras Y rot at the start of the program instead of always checking it. I can not seam to put Camera.mainCamera.transform.rotation.y in a var.

This is in my Update function on the Prefab I am controlling.

 if ((Camera.mainCamera.transform.rotation.y < 137) && (Camera.mainCamera.transform.rotation.y > 51))
 {
     //Front Between 137-50 camera rot.y GP Movement
     if ((transform.position.z > 388.5) && (Input.GetKeyDown (KeyCode.D))) transform.Translate (Vector3(0,0,-33.5));      
     if ((transform.position.z < 392.5) && (Input.GetKeyDown (KeyCode.A))) transform.Translate (Vector3(0,0,33.5));
     if ((transform.position.x > -84) && (Input.GetKeyDown (KeyCode.S))) transform.Translate (Vector3(-33.5,0,0));
     if ((transform.position.x < 51) && (Input.GetKeyDown (KeyCode.W))) transform.Translate (Vector3(33.5,0,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

1 Reply

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

Answer by Khada · Aug 23, 2012 at 09:47 AM

This should work fine:

 float fYRot = Camera.main.transform.eulerAngles.y;

If your code is only running once at the start, are you sure it's in an update loop and not in a Start / Awake method?

Comment
Add comment · Show 4 · 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 NoGo86 · Aug 23, 2012 at 01:43 PM 0
Share

That is C# right not javascript? in javascript its "var fyRot : float = Camera.mainCamera.transform.rotation.y" right? When I do that it says can not put camera.maincamera.transform.rotation.y in a var. And yes it is in the update function for sure. To me my code should check if Camera rot Y is <137 And >50 and if it is the controls should work if not then nothing happens. but that is not happening. It will only let me use the movement keys if the Camera.mainCameras Y rotation is already betwen the two HardcodeVars at the start of the game.

avatar image Khada · Aug 23, 2012 at 01:59 PM 0
Share

Yes, it's C# (I don't use java). Is it really "Camera.mainCamera" in java (rather than "Camera.main" in c#). Seems odd to have a different na$$anonymous$$g convention. You're using an API with debugging capabilities?

OH! I just realised you're using "transform.rotation.y"! Don't do that, use "transform.eulerAngles.y"

avatar image NoGo86 · Aug 23, 2012 at 06:20 PM 0
Share

You rock $$anonymous$$hada! All I needed was the eulerAngles.y ins$$anonymous$$d of rotation.y

avatar image Khada · Aug 24, 2012 at 07:42 AM 0
Share

Awesome :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

How do I move the camera to another object in the scene on mouse down? 1 Answer

Camera Orbiting Character: How to make an object move with another with no rotation 1 Answer

Push object in direction of camera 0 Answers

LookAt only when camera position changes 2 Answers

Object doesn't stick on Camera right Corner?? (Basic Question) 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