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 LeftyTwoGuns · Aug 16, 2013 at 01:30 AM · rotationquaternionlookrotationaxes

Confused with LookRotation in C#

I have a turret I would like to automatically look at specific targets, rotating only on the Z-axis, looking up. Most of the examples I see of LookRotation are in Javascript, which does Quaternions differently than C#. In JS you can list the axes seperately but you can't go that in C#. Example:

 public Transform target;
 
 void Update () {
         var newRotation = Quaternion.LookRotation(target - transform.position).eulerAngles;
         newRotation.x = 0;
         newRotation.y = 0;
         transform.rotation = Quaternion.Euler(newRotation);
     }

Isn't as simple in C#. Quaternions are pretty confusing to me so I was just wondering how to limit the rotation to just Z-axis in C#.

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
2
Best Answer

Answer by robertbu · Aug 16, 2013 at 01:46 AM

I believe this is what you are looking for:

 public Transform target;
  
 void Update () {
     Vector3 look = target.position - transform.position;
     look.z = 0;
     transform.rotation =  Quaternion.LookRotation(look);
 }
Comment
Add comment · Show 6 · 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 LeftyTwoGuns · Aug 16, 2013 at 02:06 AM 0
Share

That gives me the error of:

Operator '-' cannot be applied to operands of type UnityEngine.Transform' and UnityEngine.Vector3'

avatar image robertbu · Aug 16, 2013 at 02:32 AM 0
Share

I fixed the code in the answer.

avatar image LeftyTwoGuns · Aug 16, 2013 at 02:52 AM 0
Share

That was it, should have noticed that myself lol

It's rotating on the correct axis now but the cannon barrel itself is not pointing at the target. It's a simple object I made in Blender and I just applied the scrip to the barrel. What do I have to do to specify that I want the tip of the barrel itself to look at objects?

avatar image robertbu · Aug 16, 2013 at 03:08 AM 0
Share

Transform.LookAt() and Quaternion.LookRotation() assume that the front of the object is looking towards positive 'Z'. You can use an empty game object with the visible object as a child. The script goes on the empty game object, and you use a local rotation on the child to fix things. But by far the best fix is to go back to blender and change the default rotation so that the gun barrel is pointing to positive 'Z'.

avatar image LeftyTwoGuns · Aug 16, 2013 at 03:51 AM 0
Share

Now that seems to be a whole new bag of headaches lol. It seems as if all the problems I've been having with getting objects to rotate correctly has to do with object orientation in Blender vs. Unity. Guess I have some reading to do. Thanks a bunch!

Show more comments

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

16 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

Related Questions

Transform Z axis rotation from center of screen toward mouse cursor with Quaternion.LookRotation for a top down game. 2 Answers

to rotate along z axis 1 Answer

Quaternion.LookRotation only on Y axis 0 Answers

LookRotation but keeping oriented a certain way 0 Answers

Slerp to make the right/left side face another object 2 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