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 Malkalypse · Mar 26, 2018 at 05:10 AM · camerarotationtransform

Get rotation based on closest 90 degrees to camera transform?

I'm trying to set a Vector3 rotation value based on camera orientation. I found 2 snippets of script that I think can help, but I'm not sure how to make them work in context. The first snippet sets the rotation directly based on the camera transform:

 Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
 targetDirection = Camera.main.transform.TransformDirection(targetDirection);
 targetDirection.y = 0.0f;


The other rotates a game object to the nearest 90 degree axis:

 Vector3 vec = transform.eulerAngles;
 vec.x = Mathf.Round(vec.x / 90) * 90;
 vec.y = Mathf.Round(vec.y / 90) * 90;
 vec.z = Mathf.Round(vec.z / 90) * 90;
 transform.eulerAngles = vec;


What I want is to get a Vector3 rotation value based on the 90 degree axis that the camera transformation rounds to. Any advice on this?

My first instinct was to do something like the following (using an empty placeholder game object as "coordSys")

 Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
 
 Vector3 vec = Camera.main.transform.eulerAngles;
 vec.x = Mathf.Round(vec.x / 90) * 90;
 vec.y = Mathf.Round(vec.y / 90) * 90;
 vec.z = Mathf.Round(vec.z / 90) * 90;
 
 coordSys.eulerAngles = new Vector3(x, y, z);
 
 targetDirection = coordSys.transform.TransformDirection(targetDirection);
 targetDirection.y = 0.0f;


This works for some camera angles, but other times it does not

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

Answer by Ramlock · Mar 26, 2018 at 05:43 AM

Is this what you want? I didn't really understand what you're trying to achieve.

  Vector3 vec = Camera.main.transform.eulerAngles;
  vec.x = Mathf.Round(vec.x / 90) * 90;
  vec.y = Mathf.Round(vec.y / 90) * 90;
  vec.z = Mathf.Round(vec.z / 90) * 90;
  Camera.main.transform.eulerAngles = vec;
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 Malkalypse · Mar 26, 2018 at 03:44 PM

No, I am not trying to rotate the camera. I am trying to establish a coordinate system BASED on the camera but at a multiple of 90 degrees in world space.

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

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

Related Questions

Keep camera at certain distance from character when rotating 3 Answers

Camera rotation does not work properly 2 Answers

Moving Camera on player stop then move and rotate to separate point? 0 Answers

2D Camera Rotation Along Level Boundaries 1 Answer

Quaternion Slerp Sanity Check 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