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 Anymeese · Mar 10, 2014 at 08:04 PM · rotationparentthird-person3rd person controller3rd person camera

How to get JUST the y rotation of an object?

I'm making a 3rd person game with the controls set up so that:

If the player holds down forward on the left stick (i.e. runs forward) and rotates the camera left/right, he'll run the way the camera's facing.

HOWEVER, my problem is that he is also running the way the camera is facing, rotated around the x/z axis too, so he's turning and facing the sky rather than just running on the x,z plane.

I'm not familiar with matricies, quaternions, or eulr's, so is there a way I can make my character ONLY rotate around the y axis (so that he only runs on the x,z plane), not all 3?

the code I'm using is: "transform.rotation = cameraParent.transform.rotation;", I'm just not sure how to split it up so that it only changes the rotation around the y axis since transform.rotation.y only returns a value and can not be set by calling that

Comment
Add comment · Show 1
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 robertbu · Mar 11, 2014 at 04:42 AM 0
Share

Lots of ways to solve. Here is an approach using Vectors rather than angles:

 var dir = cameraParent.transform.forward;
 dir.y = 0.0;
 transform.rotation = Quaternion.LookRotation(dir);

3 Replies

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

Answer by highpockets · Mar 10, 2014 at 09:18 PM

You could just get the euler y angle of the camera:

 float yRotation = cameraParent.transform.eulerAngles.y;

Then assign it to the transform:

 transform.eulerAngles = new Vector3( transform.eulerAngles.x, yRotation, transform.eulerAngles.z );
Comment
Add comment · Show 2 · 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 Anymeese · Mar 11, 2014 at 04:38 AM 0
Share

This did it! Thanks very much!!

avatar image zaher_it · Jun 02, 2016 at 09:13 AM 0
Share

This was very helpful for me. Thanks

avatar image
1

Answer by goibon · Mar 10, 2014 at 09:14 PM

You only wish to access

 cameraParent.transform.rotation.y

but you can't modify that property by itself, so you need to store it in a temporary variable and then apply the rotation like this:

 Vector3 newRotation = transform.rotation;
 newRotation.y = cameraParent.transform.rotation.y;
 transform.rotation = newRotation;
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 Anymeese · Mar 11, 2014 at 04:35 AM 0
Share

The problem is that transform.rotation is a Quaternion, NOT a Vector 3, so this won't work

avatar image
1

Answer by nicolasjr · Mar 10, 2014 at 09:13 PM

Should be something like:

 transform.rotation.eulerAngles = new Vector3(transform.rotation.eulerAngles.x, newRotationValue, 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 Anymeese · Mar 11, 2014 at 04:38 AM 0
Share

This was close to the solution, but gave an error message because it's supposed to be transform.eulerAngles, no "rotation"

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

How to make the character move and rotate where the camera is facing (3rd person) 0 Answers

transform.rotation and localRotation automatically reset after rotating. 1 Answer

3rd person controller , movement issues 1 Answer

Using gamepad triggers to control 3rd person camera 2 Answers

How to move player in direction where the camera is aiming ? 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