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 .hooligan · Jan 13, 2011 at 12:03 PM · rotation

Rotate on one axis

I was wondering how I can restrict the following code to rotate around the z axis only?

Quaternion targetRotation = Quaternion.LookRotation(target.position - turret.transform.position, Vector3.up);
turret.transform.rotation = Quaternion.Slerp(turret.transform.rotation, targetRotation, Time.deltaTime * 2.0f);

I'm working on a top down game so I only want it to rotate around one axis to face the player. Can it be done the way im trying above?

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

2 Replies

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

Answer by Statement · Jan 13, 2011 at 12:35 PM

You could try setting the look direction z to zero. I created a temporary variable to illustrate this easier.

var lookDirection = target.position - turret.transform.position; lookDirection.z = 0;

var targetRotation = Quaternion.LookRotation(lookDirection, Vector3.up); turret.transform.rotation = Quaternion.Slerp(turret.transform.rotation, targetRotation, Time.deltaTime * 2.0f);

I haven't tested this but it should be easy enough to test this for you with 2 more lines of code.

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 Statement · Jan 13, 2011 at 12:35 PM 0
Share

If you are getting dodgy results, try normalizing lookDirection after modifying z.

avatar image .hooligan · Jan 18, 2011 at 07:41 AM 0
Share

I fooled around with the code you posted and I'm still having trouble. I checked out my turrets orientation. The turrent sprite faces downward and the upwards y direction faces upward for it, so in the opposite direction.

I fooled around with setting each value individually the 0 for x, y, z.

I'm guessing I only want to rotate on the y axis so I tried setting the z and x to zero. But what happens is it rotates on the x as the player gets closer to in the top down view it appears to flip.

This is really hard to explain :(

avatar image .hooligan · Jan 18, 2011 at 08:22 AM 0
Share

When I try the following var lookDirection = target.position - turret.transform.position; Quaternion targetRotation = Quaternion.LookRotation(lookDirection, Vector3.down); turret.transform.rotation = Quaternion.Slerp(turret.transform.rotation, targetRotation, Time.deltaTime * 2.0f);

The turrets x forward tries to face the target. Which is the bottom of the sprite.

avatar image .hooligan · Jan 19, 2011 at 12:38 PM 0
Share

I managed to get working thanks for your help

avatar image Peter G · Mar 13, 2011 at 10:02 PM 1
Share

Sorry to comment on an old thread, but I noticed the top line is javascript and the 3rd is C#. (Unless you are using var type inference, which you and I understand, but many people would never figure out :) )

Show more comments
avatar image
0

Answer by Rally Raghavan · Mar 13, 2011 at 08:59 PM

Quaternion rotation = Quaternion.LookRotation(rotation, Vector3.up);

This line in the code shows some semicolon error. I do not understand what error it is. Error says, there should be a semicolon in the end.

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 Peter G · Mar 13, 2011 at 11:34 PM 0
Share

Remove "Quaternion" at the beginning of the line and replace it with "var".

avatar image Rally Raghavan · Mar 14, 2011 at 07:43 AM 0
Share

ya i tried it already. says some different error then. Assets/Codes/boatForce.js(17,67): BCE0023: No appropriate version of 'UnityEngine.Quaternion.LookRotation' for the argument list '(UnityEngine.Quaternion, UnityEngine.Vector3)' was found. This is the error that pops out.

avatar image Peter G · Mar 15, 2011 at 10:58 PM 0
Share

Quaternion.LookRotation(Vector3 dir, Vector3 up). You have to pass 2 Vector3's as an argument.

avatar image Rally Raghavan · Mar 16, 2011 at 06:52 PM 0
Share

thanks :) it works...

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

No one has followed this question yet.

Related Questions

Flip over an object (smooth transition) 3 Answers

Local Rotation of Child Object messed up 1 Answer

Instantiate an image, equally in a circle and rotate it away from the center? 2 Answers

Making an objects y Rotation match another objects Z 2 Answers

Extrapolating Quaternion Rotation 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