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 Karsnen_2 · Nov 17, 2014 at 09:00 PM · c#rotationquaternionlogic

How to make a plane face a static but rotating camera?

Hello,

SCENARIO:

I have a plane with has UI Elements on it. It is a child to an empty GameObject (say targetGO). Now I wish to rotate targetGO towards the Main camera and make sure it faces it always. The camera is completely static but rotates around it's position.

PROBLEM:\

It works fine, but for some reason the plane is not perfect. Hence I believe that there is something fundamentally wrong in the logic. I use this piece of code. This class is a component of targetGO.

CODE :

http://pastebin.com/TWLurbga

 private void lookAtCamera()
 {
         transform.LookAt((this.transform.position + cameraMain.rotation * Vector3.up),
                          (cameraMain.rotation * Vector3.up));
 }

// EOF

REQUEST :

Is there a problem with the logic, if not what is the best route to approach it. I am sure this has been done many times but I wish to know the correct flawless logic.

Thank you and highly appreciate your time.


Karsnen

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

3 Replies

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

Answer by robertbu · Nov 17, 2014 at 09:08 PM

I'm not sure of your exact goal here, but the easiest way to make sure a 'plane' is facing the camera is to use a Unity's built-in Quad (not a Plane). Then you can just assign the rotation:

   transform.rotation = cameraMain.rotation;

I'm assuming 'cameraMain' is the transform of the main camera.

Comment
Add comment · Show 7 · 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 FairGamesProductions · Nov 17, 2014 at 09:14 PM 0
Share

I'm sorry, but no. All this would do is assign the same rotation to the gameObject as the camera. That will NOT make it face the camera. It may actually make it face AWAY from the camera...

avatar image robertbu · Nov 17, 2014 at 09:17 PM 0
Share

@FairGamesProductions - you are wrong. Unity's Quad has the visible triangles on the backside. So if the rotation of the camera and the rotation of the Quad matches, and the Quad is within in the Camera's view Frustum, then the Quad will be visible. In addition, since the rotation matches, the orientation will always match. The rotation I believe the OP is trying for can be done with a Plane, but the code is a lot uglier.

avatar image FairGamesProductions · Nov 17, 2014 at 09:58 PM 0
Share

@robertbu No... YOU are wrong. If you give the built-in plane, the exact rotation of the camera, it will NOT be facing the camera AND there is a good chance that the backward facing polygon will be facing the camera, so the polygon will, if effect, be invisible without the right shader...

avatar image robertbu · Nov 17, 2014 at 10:07 PM 0
Share

No, read my answer. I recommend a built-in Quad. In addition, I just dropped in a built-in plane into a project, added a material and this script using your line of code:

 #pragma strict
   
 function Update () {
     transform.LookAt(Camera.main.transform.position);
 }

As expected, it does not work. Nor does it work for a Quad. But the line of code I supplied works just fine with a Quad.

avatar image FairGamesProductions · Nov 17, 2014 at 10:49 PM 0
Share

I agreed with you (see lower) about the visible polygons on the quad. But simply taking the rotation of the camera and applying it to the quad will fail eventually. Don't forget, he said that the camera is stationary, BUT it can rotate. LookAt is the only surefire way of doing it.

Show more comments
avatar image
1

Answer by FairGamesProductions · Nov 17, 2014 at 09:12 PM

If I understand your question correctly, you only need this to have the gameObject look at the camera:

 transform.LookAt(cameraMain.transform.position);

I'm not sure why you have all the rest there... Also, this should be under the Update function, otherwise it will only happen once when the lookAtCamera function is called. You want this to happen all the time in order to keep the gameObject pointed at the camera at all times.

EDIT: As robert has pointed out, the built-in quads have their visible polygons on the negative Z axis (which is really weird, IMHO). So an extra line of code is needed:

 function Update ()
     {
     gameObject.transform.LookAt(Target.transform.position);
     gameObject.transform.rotation.eulerAngles.y += 180;
     }

This will correct the Y rotation so the visible polygons are facing the camera.

Comment
Add comment · Show 4 · 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 robertbu · Nov 17, 2014 at 09:34 PM 0
Share

This will not work with a built-in plane, nor will it work with a Quad. In addition, it does nothing to assure the orientation. That is if the camera is rotated arbitrarily, the object will face the camera, but the orientation may be anything.

avatar image FairGamesProductions · Nov 17, 2014 at 09:56 PM 0
Share

That is just not true...

LookAt will point the positive Z axis of the object at the point you are targeting... It has NOTHING to do with the rotation of the camera... And FYI, I use this with the built-in quads in my game and it works PERFECTLY.

avatar image robertbu · Nov 17, 2014 at 10:13 PM 0
Share

But a built-in plane, the visible triangles are facing positive 'y'. A Quad has the visible triangles on the back side. Neither of these objects has the visible side on the positive 'z' size. And the positive 'z' side is the side that LookAt() will face towards it supplied Vector3. Test it.

avatar image FairGamesProductions · Nov 17, 2014 at 10:43 PM 0
Share

O$$anonymous$$. You're right. I got a little confused. I was thinking of C4D for some reason. But it's a simple fix:

 function Update ()
     {
     gameObject.transform.LookAt(Target.transform.position);
     gameObject.transform.rotation.eulerAngles.y += 180;
     }

This will now work for built-in quads.

avatar image
0

Answer by philwinkel · Nov 17, 2014 at 09:15 PM

I think what you're trying to accomplish is a "Camera-facing billboard", here is some information: http://wiki.unity3d.com/index.php?title=CameraFacingBillboard

As FairGamesProductions suggested, you can use transform.LookAt, or there are some other samples on that wiki link depending on your requirements.

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

28 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

Related Questions

Flip over an object (smooth transition) 3 Answers

[Solved] Help on a rotation issue for a Top-Down view? 1 Answer

rotate rigid body based on transform.velocity? 2 Answers

i want to get my to cube to rotate 90 degrees on the x-axis as it jumps 0 Answers

Getting the rotation of an object does not return correct rotation 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