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 /
avatar image
0
Question by Ambassador_of_Infinity · Sep 16, 2019 at 05:19 PM · rotationrotatelookatlocalspacelook at

Look at around one axis in local space

I want a weapon to aim at a specific GameObject while its parts rotate around specific axes : alt text I need something like a transform.LookAt() in local space aroud specified axis. This :

 public Transform yRotator, xRotator;
 
 ...
 
 private void Update() // Look at target
 {
         Vector3 pos = target.position;
 
         yRotator.LookAt(new Vector3(pos.x, 0, pos.z));
 }

works for Y rotator... as long as spaceship the weapon is mounted on does not rotate. When it does, it all breaks, because Transform.LookAt() works in global space. This is also why X rotator is not included in this script - it does not work because it's a child of Y rotator (look at the image).

I have tried flatting target position converted to local space, but I can't make it work :

 public Transform yRotator, xRotator;
     
     ...
     
     private void Update() // Look at target
     {
         Vector3 pos = target.position;
 
         Vector3 rotY = yRotator.InverseTransformDirection(target.position);
         rotY.y = 0;
         yRotator.LookAt(yRotator.TransformDirection(rotY));
     }

UPDATE : Problem fixed :

 public Transform yRotator, xRotator, target;
         
         ...
         
         private void Update() // Look at target
         {      
             Vector3 rotY = yRotator.InverseTransformPoint(target.position);
             rotY.y = 0;
             yRotator.LookAt(yRotator.TransformPoint(rotY), yRotator.parent.up);

             Vector3 rotX = xRotator.InverseTransformPoint(target.position);
             rotX.x = 0;
             xRotator.LookAt(yRotator.TransformPoint(rotX), xRotator.parent.up);
         }

1.png (116.1 kB)
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

Answer by sbsmith · Sep 17, 2019 at 02:18 AM

You could always build it backwards. The gun is the parent and the mounting geometry are the children. You can point your gun at the target, then rotate the mounts so it looks like they're attached properly to the ship. Since the mounts don't have any children, you don't need to worry about them messing up the position of your gun.

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

148 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

Related Questions

LookAt inaccurate rotation issue (javascript) 1 Answer

Lock rotation axis? 4 Answers

Why LookRotation works, but LookAt doesnt 0 Answers

Rotation using Unity2D 3 Answers

LookAt() with X axis 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