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 strideynet · Dec 15, 2013 at 02:47 PM · c#camerarotation

C#. Rotation Help!

In my code the camera must stick to the Y Rotation of another object. The other object is known as target. target = theobject.transform. What code should I use to make the camera have the same Y rotation as theobject (target)? Looked everywhere and Eulers are confusing me!

Comment
Add comment · Show 4
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 strideynet · Dec 15, 2013 at 03:00 PM 0
Share

Hello. Do I need to give code?

avatar image EX_Darius · Dec 15, 2013 at 03:09 PM 0
Share

If you are not looking at the target but just need to get the rotation of the target simply put this line in your camera update script:

transform.rotation.y = target.transform.rotation.y;

avatar image aldonaletto EX_Darius · Dec 15, 2013 at 03:17 PM 1
Share

Don't assign values directly to transform.rotation like that! Rotation is a quaternion, a mysterious creature whose XYZW components have nothing to do with the nice angles we see in the Inspector's Rotation field (they are actually transform.eulerAngles). You should ins$$anonymous$$d copy transform.eulerAngles, but this wouldn't work fine either (see my answer)

avatar image LijuDeveloper · Dec 17, 2013 at 10:52 AM 0
Share

Please write your sample code

4 Replies

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

Answer by Visal · Dec 17, 2013 at 04:23 AM

Ok you want the rotation y of the camera to has the same value as target's rotation y value. In that case try this:

 public Transform target;
     void LateUpdate()
     {
         Vector3 camEular = transform.rotation.eulerAngles;
         transform.rotation = Quaternion.Euler(camEular.x, target.rotation.eulerAngles.y, camEular.z);
     }


Note: attach this script to Camera

Hope that could help :)

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
2

Answer by aldonaletto · Dec 15, 2013 at 03:09 PM

Euler probably won't help you: using a single transform.eulerAngles angle isn't reliable, since the 3 angles (XYZ) may change to weird (although correct) combinations. A more reliable solution is to copy the object's forward direction (camera script):

 transform.forward = target.forward;

Or make the camera look in the same direction while keeping its "head" up:

 transform.rotation = Quaternion.LookRotation(target.forward, Vector3.up);


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 Hoeloe · Dec 17, 2013 at 10:49 AM 0
Share

eulerAngles also has another problem (which is the reason Quaternions are used in the first place) in Gimbal Lock. It is possible to rotate with a Euler representation such that you lose a degree of freedom (that is, it becomes temporarily impossible to rotate in a certain direction). For this reason as well, it is best to avoid using Euler angles at all.

avatar image
0

Answer by Sindorej · Dec 15, 2013 at 03:06 PM

Vector3 rot = YOUROBJECT.transform.rotation.eulerAngles;

transform.rotation.eulerAngles = new Vector3(transform.rotation.eulerAngles.x,rot.y,transform.rotation.eulerAngles.z);

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 Bunny83 · Dec 15, 2013 at 03:13 PM 0
Share

Your second line won't work since a Quaternion is a struct and not a reference type.

Transform has an additional property for eulerAngles. This would work just fine.

Another way is this:

 transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, rot.y, transform.rotation.eulerAngles.z);

However changing eulerangles this way usually just causes problems. Without a more detailed explanation of the problem it's hard to give the "correct" answer

avatar image
0

Answer by hans_vesthardt98 · Dec 15, 2013 at 04:17 PM

I guess you can try something like this: (Untested)

 pubic Transform: cam;
 
 void Update(){
         Vector3 camP = cam.rotation;
         camP.y = target.transform.rotation.y;
         cam.rotation = camP;
 }
Comment
Add comment · Show 3 · 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 Bunny83 · Dec 15, 2013 at 10:31 PM 0
Share

Uhm first your code looks like a mixture of C#, UnityScript and Boo. At least it wouldn't compile in any of those languages.

Second, he wanted to align the rotation, not the position. I think you got the question wrong ;)

avatar image hans_vesthardt98 · Dec 16, 2013 at 12:04 AM 0
Share

Well, i have exactly that code on my game and it works. I meant to write rotation ins$$anonymous$$d of position, that is why i wrote untested. But it should work no problem ;)

avatar image hans_vesthardt98 · Dec 16, 2013 at 07:55 AM 0
Share

Edited the code now, i would recommend Strideynet to try this code, as it works for me.

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

23 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

Related Questions

Flip over an object (smooth transition) 3 Answers

Smooth Follow Camera Rotate on Z-Axis? 2 Answers

How to make Camera position Independent of its Rotation? 1 Answer

Camera to follow the player in the form of radius 1 Answer

Can't do Quaternion.AngleAxis twice. 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