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 markTheDesigner · Feb 22, 2013 at 10:30 AM · quaternionlookatquaternion.lookrotation

How to LookAt a position along local axis only?

Hi, I've read a few posts asking the same questions and tried the code but it doesn't do what I want.

I'll just explain what I want first.

I have a sphere and I want to place turrets on the surface of the sphere (say, a planet). So, I instantiate a turret on a facet of the sphere using the facet normal to orientate it. I want this normal to be my Up axis (local Y of the turret). Then, I want the turret to face objects that are orbiting the sphere but only by rotating along its local Y axis only.

I've tried LookAt but this won't let me rotate around only the local Y. I'm guessing I need to use Quaternion rotation but I'm not sure where to start.

A hint, explanation, pseudocode or javascript would be appreciated.

Thanks

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
2

Answer by Owen-Reynolds · Feb 22, 2013 at 05:16 PM

There are some tricks in UnityAnswers for an object on regular tilted ground, always <90 degrees (because Terrain can't flip upside down.) But I think they'd break for upside-down towers.

The usual solution for a "flat" lookAt is to flatten the position before looking -- don't look at the bird, look at where it would be on the ground. But that only works if you want to spin around the real Y axis. To do it in your own personal Y, use the local-coordinate trick. Bring it to your point of view, flatten it there, then bring it back to real coords. Not specifically tested:

 Vector3 localPos = transform.InverseTransformDirection(targPos-transform.position);

localPos is now the numbers if you were the camera at (0,0,0). For example, localPos.x>0 means the target is to your right. Next flatten it:

 localPos.y = 0;

This flattens it out, but the numbers are still from your point of view. If we watched it move, you'd see it go straight down, but anyone else would see it move diagonally to your personal "ground plane." Finally bring it back to real xyz:

 Vector3 lookPos = transform.position + transform.TransformDirection(localPos);

Then use your facet normal for the UP in lookAt.

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 azevedco · Feb 26, 2019 at 04:08 PM 0
Share

Just what I was looking for, thanks a ton!

avatar image
0

Answer by markTheDesigner · Feb 23, 2013 at 04:07 PM

Hi, thanks for that, that is what I was thinking.

I tried it a few times but couldn't get it right but I'll try your method.

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 markTheDesigner · Feb 23, 2013 at 07:46 PM 0
Share

Worked perfectly. Thanks :)

avatar image
0

Answer by Osttik · Oct 07, 2020 at 09:44 AM

I have similar problem, and i think i will help (my problem, when i writing this text, isn't solved, but it helps to rotate without using some angle) https://www.youtube.com/watch?v=dp3lZUDij6Y&feature=emb_title

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

12 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

Related Questions

How Make an Vector Follow an Quaternion? 1 Answer

smooth look at with offset 0 Answers

Using MouseLook script with LookAt function 2 Answers

Get slerp to work just as LookAt(,Vector3.right) does 1 Answer

How to use LookAt() to make a gameobject look at another gameobject slowly? 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