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 guru20 · Jul 18, 2017 at 07:19 PM · cameravrcanvascamera rotatecamera rotation

Get camera rotation constrained to left/right (ie. yaw)?

I'm trying to "project" a GUI/HUD screen (canvas in world-space) in front of the user in a VR game, but would want the canvas to be consistently upright/vertical (ie. I wouldn't want to project it at an angle if head is tilted sideways; nor would I want it to appear on ceiling or floor if user is looking up or down)

In other words, I need just the normal/projection forward from the left/right turn of the head, ignoring the other rotations (ie. I need just yaw, while ignoring pitch and roll of the camera)

What is the best way to get this angle (and a transformation forward some distance from that angle)?

I'm assuming I can convert the rotation from Quaternion to Euler to check the rotation angle, but then how do I place the Transform "forward" from that Euler angle accordingly?

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

Answer by rajannath · Dec 20, 2017 at 02:13 AM

using UnityEngine; using System.Collections;

public class swiperotation :TouchLogic { public float rotateSpeed = 100.0f;

 private float pitch = 0.0f,
 yaw = 0.0f;

 void onTouchMoveAnywhere () 
 {
     pitch -= Input.GetTouch (0).deltaPosition.y = * rotateSpeed * invertPitch * Time.deltaTime;
     yaw += Input.GetTouch (0).deltaPosition.x = * rotateSpeed * invertPitch * Time.deltaTime;

     //do the rotations of our camera
     this.transform.eulerAngles = new Vector3 (pitch, yaw, 0.0f);
 }

}

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 DreadKyller · Dec 20, 2017 at 02:25 AM 0
Share

From what I read, this answer is opposite what they want. The OP wants to get the yaw of the current camera rotation, not to rotate the camera based on yaw and pitch.

avatar image
0

Answer by DreadKyller · Dec 20, 2017 at 02:45 AM

If this is what you want, all you really need to do, is take the standard euler angles and use only the Y axis, this would give you your yaw.

 float angle = transform.rotation.eulerAngles.y;

Now that we know this angle, we can calculate the direction:

 Vector3 direction = new Vector3(
     Mathf.Cos(angle * Mathf.Deg2Rad),
     0f,
     Mathf.Sin(angle * Mathf.Deg2Rad)
 );

By default this will already be a unit vector because of the use of sine and cosine, so no need to normalize.

Now, to find the normal you need to render the UI, just invert the direction:

 Vector3 normal = Vector3.zero - direction;

And to find the location to place the center of the UI:

 Vector3 position = direction * distance;

Where distance is the number of units you want it far away.

If this renders on the side instead of in front of you, then you need to flip the sin and cos. If the ui renders behind you at all times, then you need to add 180 to the angle or negate the x and z values of the direction vector (preferable). If the position of the UI moves based on if you're facing on Z or X axis, then one of the two (either x or z) of the direction need to be negated.

EDIT!

Alternatively to the sin and cos method, you can also take the forward vector and set it's y to 0, then normalize:

 Vector3 direction = yourCamera.transform.forward;
 direction.y = 0;
 direction.Normalize();

This may be faster (no trig, but still a quare root so...) and should still give the same results.

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

127 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

Related Questions

third person camera 2 Answers

VR camera position instant flick 0 Answers

I'm trying to create an FPS script but something I'm not getting faser is what the rotation limiters are for x please someone help me 0 Answers

camera rotation help in bolt script 0 Answers

FPS cam for Roll -a-Ball like game 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