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 /
This question was closed Nov 24, 2017 at 03:03 AM by Namaarie for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Namaarie · Oct 31, 2017 at 04:24 AM · rotationrotaterotatearoundlocalrotationz axis

Rotate object around local Z axis

Hi! So, I'm trying to rotate a gameobject around the local Z axis to face another gameobject, and have the X and Y axis stay the same. But it always turns out weird. I have tried

         Vector3 a = new Vector3 (target.position.x, target.position.y, target.position.z);
         Vector3 b = new Vector3 (transform.position.x, transform.position.y, transform.position.z);
         Vector3 direction = a - b;
         direction.Normalize ();
         Quaternion rotation = Quaternion.Euler(45f, -45f, Mathf.Atan2(direction.x, direction.y) * 57.29578f - 180f);
         transform.rotation = rotation;

But, it only accounts for the X position of the two gameobjects. So when you're above the object, it will face down and when you're below the object, it will face up. I have also tried

         Vector3 a = new Vector3 (target.position.x, target.position.y, target.position.z);
         Vector3 b = new Vector3 (transform.position.x, transform.position.y, transform.position.z);
         Vector3 direction = a - b;
         Quaternion rotation = Quaternion.LookRotation (direction);
         transform.rotation = rotation;

But, it rotates the object on all 3 axis. And if I change it to

        transform.rotation.z = rotation.z;

It returns a

"Cannot modify a value type return value of 'UnityEngine.Transform.rotation'. Consider storing the value in a temporary variable"

error. I have also tried various other things, such as

        float angleRad = Mathf.Atan2 (target.transform.position.y - transform.position.y, target.position.x - transform.position.x);
         float angleDeg = (180 / Mathf.PI) * angleRad;
         transform.rotation = Quaternion.Euler (45f, -45f, angleDeg);

And

         transform.Rotate (new Vector3(45f, -45f, Quaternion.Euler(direction).z),Space.Self);

But they all have their own issues. So if anyone can help me solve this problem, that would be much appreciated. Thanks!

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 MezFo · Oct 31, 2017 at 04:55 AM 0
Share

I think you need to use transfrom.localRotation ins$$anonymous$$d of transform.rotation.

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by nihohit1 · Oct 31, 2017 at 06:43 AM

Try using your own x & y coordinates while rotating:

 Vector3 a = new Vector3 (transform.position.x, transform.position.y, target.position.z);
          Vector3 b = new Vector3 (transform.position.x, transform.position.y, transform.position.z);
          Vector3 direction = a - b;
          Quaternion rotation = Quaternion.LookRotation (direction);
          transform.rotation = 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 ihsan07 · Feb 10, 2018 at 08:25 AM 1
Share

This won't work. Because in "a" and "b" vectors, x and y angles are the same. So in direction it will always be 0 and "transform rotation" will only return (0,0,0) or (0,0,180). Correct me if I have done something wrong.

Follow this Question

Answers Answers and Comments

104 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

Related Questions

rotate around character 1 Answer

Character Rotation Now Matching GameObject's Local Rotation 0 Answers

Quaternion, eulerAngles, localEulerAngles Driving me mad 1 Answer

rotate around character 2 Answers

Transform.RotateAround over specific length of time 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