Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Dan-Gilbert · Jan 16 at 07:16 PM · script.lookrotation

LookAt rotate around X

I know this has been asked and answered a million times , mostly relating to y rotation for turrets etc. but.....

I am trying to make a Cube (1) Rotate around its X Axis only. To look at another Cube object (creating a Flap ). Local Z is facing forward correctly ,Y is Up. Using below simple code, Cube (1) snaps to look at World Z

  targetPostition = new Vector3 ( transform.position.x, target.position.y, target.position.z );
             transform.LookAt(targetPostition);
 

If I set it to :

  ... new Vector3 ( target.position.x, **transform.position.y**, target.position.z );
 

Cube(1) stays facing forward in local position and rotates perfectly around Y only, but I need to rotate around X.

I have been through tons of answers here and elsewhere , have also tried using Quaternion.LookRotation , but again with .x restricted to transform.position . Cube(1) looks towards World Z only.

Im well and truly stuck here, any suggestions greatly appreciated

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

1 Reply

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

Answer by Hellium · Jan 16 at 07:53 PM

 private void Update()
 {
     targetPosition = transform.position + (Vector3.ProjectOnPlane(Target.position - transform.position, transform.right));
     transform.LookAt(targetPosition);
 }

This will keep the rotation of the object on its Y and Z intact while rotating the object along its X axis.

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 Dan-Gilbert · Jan 16 at 08:44 PM 0
Share

NOTE: targetPosition apologies for my mis spelling !

Thank you very much for replying This only seems to work if the target cube is moved into a position so that Cube(1) is again looking in World Z direction.

alt text

ProjectOnPlane () has fried my brain , especially after spending all day frustrated with what i though would be a simple process.

i just want to rotate the long cube like a wing flap around x only ( will be making target a grabbable object in XR Interaction.

If there is a more suitable solution please let me know .

Thanks everyone ))

lookat.jpg (204.0 kB)
avatar image Hellium Dan-Gilbert · Jan 16 at 09:37 PM 0
Share

I've updated the script and fixed an issue when the object wasn't at the origin, I hope it will fix the issue you're having.

The LookAt method is meant to align the Z axis towards the target position.

avatar image Dan-Gilbert · Jan 16 at 09:50 PM 0
Share

Fantastic. That works perfectly. I hate to just paste stuff in without knowing how it works , but now i can get over this hurdle and onto the next one.
Thank you ever so much for your help

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

168 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 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 pieces of model to look at player 1 Answer

UnityEngine.Object is null but System.Object is NOT null! 3 Answers

5.3 Scripts Appear Ghosted in The Inspector? 1 Answer

PDF417 Barcode reader for iOS and Android 0 Answers

Unity project referencing another Unity project? 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