• 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 Alireza-pir · Aug 17, 2014 at 02:27 PM · camerarotation

set rotation Of An object same to the Another except in one axis

Hi All, im trying to write a code for camera that follows the player, i set the rotation and position Of the camera same as the player by this code:

 transform.position = shazde.transform.position;
     transform.rotation = shazde.transform.rotation;


but there Is one problem, when I flip the player to walk backward, my camera flips too, the question Is this, How can i make the rotation Of An object same Az another one, just in some axis that i want not in all axis. or at least how can i freeze the camera rotation in one axis?

Comment
Add comment · Show 2
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 · Aug 17, 2014 at 02:49 PM 0
Share

What axis are you flipping the camera on? What kind of object is the camera following?

avatar image Alireza-pir · Aug 17, 2014 at 04:28 PM 0
Share

im not flipping the camera, Im Flipng the player, I parentet An Empety Game Object callet "CameraTarget" with player by the codes i mentioned, I mean by: transform.position = shazde.transform.position; transform.rotation = shazde.transform.rotation;

(shazde is the Player Game Object witch is a mesh)

then i wrote A code for camera to follow the CameraTarget GameObject, but the problem is, when i rotate the player in y Axis for 180, the CameraTarget And so the Camera Itself rotate too, i just want the camera not rotate in y, was it clear?

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Cherno · Aug 18, 2014 at 01:19 AM

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


This will update the camera's y rotation to be the same as the player's y rotation.

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 Tsilliev · Jul 04, 2017 at 10:37 AM 0
Share

It doesnt recognize eulerAngles: Assets/LockRotZ.cs(17,55): error CS1061: Type UnityEngine.GameObject' does not contain a definition for eulerAngles' and no extension method eulerAngles' of type UnityEngine.GameObject' could be found. Are you missing an assembly reference?

avatar image
-1

Answer by efeguclu · Jul 04, 2017 at 11:31 AM

use Quaternion.Euler..

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

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 Tsilliev · Jul 04, 2017 at 02:41 PM 0
Share

This is what worked for me and no other combination of quanternions, euelers, localtransforms etc etc, I checked 10 threads. This one worked for me: EmptyObject.transform.localEulerAngles = new Vector3(0.0f,transform.localEulerAngles.y, 0.0f);

avatar image efeguclu Tsilliev · Jul 04, 2017 at 02:49 PM 0
Share

so is my answer correct?

avatar image Tsilliev efeguclu · Jul 04, 2017 at 02:51 PM 0
Share

No, sorry. Another guy told me that some methods are in java and thats why the different results and gibberish in unity.

avatar image
0

Answer by Tsilliev · Jul 04, 2017 at 02:45 PM

I found a solution for myself from checking all around google, this is what worked for me in CSHARP:

 EmptyObject.transform.localEulerAngles = new Vector3(0.0f,transform.localEulerAngles.y, 0.0f);

The script is attached to the camera, my empty object emulates 1:1 the Y rotation from the camera, all other combinations that I have seen from the internet copy it in the ratio of 1:100 or other gibberish.

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.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotation seems to pass through condition checks 1 Answer

How to make my player look in the direction of its movement? 3 Answers

How do I set a child object to not rotate if the parent WILL be rotating? 1 Answer

How to make "annotation" which always face the camera? See the pictures. 1 Answer

allows users to view 360º content without a VR headset 0 Answers


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