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 Sprakle · Jul 04, 2013 at 11:09 PM · rotationlookatbillboard

Lookat on local axis

I have an object that must face another on it's local z axis only. Only the local z euler rotation will be modified.

I've seen a few other similar questions, but each answer had a small problem. Here are the three main (incorrect) methods:

Method 1:

 Vector3 curRot = transform.localEulerAngles;
 curRot.y = 90;
 curRot.z = 0;

 Vector3 dirToCamera = (camera.position - transform.position).normalized;
 float xDir = Quaternion.LookRotation(dirToCamera).eulerAngles.x;
 curRot.x = xDir;
 
 transform.localEulerAngles = curRot;

This doesn't work because xDir is not signed, causing the rotation to only work from one side. On the other, the rotation will be inverted.

Method 2:

 // look at camera
 Vector3 camRot = camera.position;
 Vector3 curRot = transform.position;
 Vector3 look = new Vector3(curRot.x, camRot.y, camRot.z);
 transform.LookAt(look);
 
 // allign to local 0
 Vector3 rot = transform.localEulerAngles;
 rot.y = 0;
 transform.localEulerAngles = rot;

I'm not exactly sure why this doesn't work, but it has the same result as Method 1. (Inverted rotation on one side)

Method 3:

 Vector3 lookAtPos = camera.position;
 Vector3 lookAtPosLocalised = transform.InverseTransformPoint(camera.position);
 
 lookAtPosLocalised.x = 0;
 
 lookAtPos = transform.TransformPoint(lookAtPosLocalised);
 transform.LookAt(lookAtPos);

This works perfectly at first, but after a while it becomes "desynced" with the parent rotation. I don't know what triggers it.

Can anyone help?

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 Sjiggle · Jul 04, 2013 at 11:29 PM

I think your first answer might be the best. It only requires a signed angle. Resources for this can be found at this link.

Once you have the signed angle you can use

 var finalAngle = (angle + 360) % 360;

this makes sure the signed angle (which means - for left, + for right rotation) becomes a 0-360 rotation, instead of -180 to 180.

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 Sprakle · Jul 04, 2013 at 11:47 PM 0
Share

I tried this:

 Vector3 curRot = transform.localEulerAngles;
 curRot.y = 90;
 curRot.z = 0;
 
 float xDir = AngleSigned(camera.position, transform.position, transform.forward);
 curRot.x = (xDir + 360) % 360; ;
 
 transform.localEulerAngles = curRot;

But the rotation is very erratic at certain angles.

avatar image
0

Answer by fafase · Jul 04, 2013 at 11:34 PM

Try this, not sure I got your point

 Transform other;

 float angle = other.localEulerAngles.z;
 transform.rotation = Quaternion.Euler(0, (angle+180.0f),0);
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

16 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

Related Questions

Is the trail emitted by the Trail Renderer "billboard"? 1 Answer

Changing The Camera Position & Offsetting LookAt 2 Answers

Camera following/looking at aircraft 1 Answer

Object Look At Mouse 2 Answers

2D LookAt not working as intended 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