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
2
Question by ROM · Jul 10, 2010 at 04:47 PM · rotationtransformrotatelookattarget

Lock rotation axis?

So I'm rotating an object around a sphere using the below code:

transform.LookAt(target2);

The problem is that I don't want it to rotate along the Z axis, only the X and Y axis. How would I do this?

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

4 Replies

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

Answer by Deikkan · Jul 10, 2010 at 04:50 PM

What you want it to do is keep the position of the target's z axis but only transform the x and y axis, like below.

transform.LookAt(Vector3(transform.position.x, transform.position.y, target2.position.z));

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 ROM · Jul 10, 2010 at 05:02 PM 0
Share

Oddly it doesn't seem to work. Its for the camera and I don't want it to rotate along the Z axis but that doesn't work basically at all. It doesn't look at the center of the sphere or anything.

avatar image Deikkan · Jul 10, 2010 at 05:35 PM 0
Share

you could also try 'transform.eulerAngles.z = 0;' That will make sure that the z rotation is always 0

avatar image cncguy · Jul 10, 2010 at 11:59 PM 0
Share

@Deikkan - I think you want to look at Vector3(target.position.x, target.position.y, transform.position.z) to do what you are saying above. ie. project the target point into the horizontal plane containing the camera.

avatar image IMTRIGGERHAPPY9 · Aug 17, 2011 at 06:32 PM 0
Share

that transform.eulerAngles.z and x stuff works the best! i highly recommend you try that before you try the above

avatar image
0

Answer by chrono1081 · Jun 18, 2011 at 03:08 AM

Thank you for this response! Its been a huge help to me.

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
avatar image
0

Answer by Darren45 · Jul 26, 2013 at 08:09 AM

If it was me, I would use transform.RotateAround. transform.RotateAround (objectToRotateAround.transform.position, new Vector3 (1, 1, 0), turnSpeed * Time.deltaTime)

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
avatar image
0

Answer by vd853 · Mar 31, 2015 at 04:27 PM

I used this to rotate a tank turret where the "up/down" rotation is locked. It is basically pointing at the player which is an airplane.

gameObject.transform.LookAt(new Vector3 (GameObject.FindGameObjectWithTag("Player").transform.position.x, transform.position.y,GameObject.FindGameObjectWithTag("Player").transform.position.z));

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to prevent Z axis rotation ? 1 Answer

How to make an enemy look at you and follow you 0 Answers

Rotation Jumping values (0 to 180) 1 Answer

how to tilt boat on turning 2 Answers

Angle to Rotate Vehicle in Scrolling in Environment 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