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 Qiut · Aug 23, 2012 at 10:50 AM · 2drotationmovementtransformquaternion

Rotating an object towards target on a single axis

I need to rotate an 2d sprite towards it's target - another sprite, only using the Z axis. The target can only move on the X and Y axis, I need the object to always face it with it's front.

I tried using Quaternion.LookRotation , converting it to eulerAngles and setting z and y to 0:

 function Update () {
     var relativePos = target.position - transform.position;
     var rotation = Quaternion.LookRotation(relativePos).eulerAngles;
     transform.rotation = Quaternion.Euler(0,0,rotation.z);
 
 }



but for some reason it doesn't rotate on the Z axis at all (rotation.z always stays at zero) no matter where the target is.

I also tried this:

    var relative : Vector3  = transform.InverseTransformPoint(target.position);

    var angle : float = Mathf.Atan2(relative.x, relative.y) * Mathf.Rad2Deg;

    var rotation = Quaternion.Euler (0, 0, angle);

    transform.rotation = rotation;



The object does rotate on the Z axis now, but instead of turning towards the target it rotates to random directions.

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
5

Answer by brunopava · Aug 23, 2012 at 03:55 PM

c#

 // fast rotation
 float rotSpeed = 360f; 
 
 // distance between target and the actual rotating object
 Vector3 D = target.position - transform.position;  
 
 
 // calculate the Quaternion for the rotation
 Quaternion rot = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(D), rotSpeed * Time.deltaTime);
 
 //Apply the rotation 
 transform.rotation = rot; 
 
 // put 0 on the axys you do not want for the rotation object to rotate
 transform.eulerAngles = new Vector3(0, 0,transform.eulerAngles.z); 



java

 // fast rotation
 var rotSpeed : float = 360; 
 
 // distance between target and the actual rotating object
 var D : Vector3 = target.position - transform.position;  
 
 
 // calculate the Quaternion for the rotation
 var rot : Quaternion = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(D), rotSpeed * Time.deltaTime);
 
 //Apply the rotation 
 transform.rotation = rot; 
 
 // put 0 on the axys you do not want for the rotation object to rotate
 transform.eulerAngles = new Vector3(0, 0,transform.eulerAngles.z); 
 // not quite sure if the "new" is needed on java
Comment
Add comment · Show 2 · 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 Muuskii · Aug 23, 2012 at 04:15 PM 0
Share

I just tried that using a capsule and a cube. It did absolutely nothing.

avatar image Qiut · Aug 23, 2012 at 04:33 PM 0
Share

I don't get why but I still can't manage to make it work. Despite that everything seems to be correct, it still doesn't rotate, Z in inspector either stays at 0 or is something like -5.617408e-06 or other random very small number.

avatar image
0

Answer by Muuskii · Aug 23, 2012 at 02:39 PM

Well, just looking at the reference for Quaternion.LookRotation we can see that it:

  LookRotation (Vector3 forward, Vector3 upwards)

creates a rotation that looks along forward with the the head upwards along upwards

I don't see where you define "upwards" in your code so I can only assume that you're letting that slip through JS's ability to handle that sort of stuff. But have you tried replacing "upwards" with Vector3.Forward? If you do, all the other stuff you have mashed in there won't matter.

LookRotation is really supposed to do what you want without any hand holding at all really.

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 Qiut · Aug 23, 2012 at 03:34 PM 0
Share

I did try setting upwards to Vector3.Forward, it didn't work, rotation.z staid at zero. I need the remaining code, because I only want the sprite to rotate on the Z axis.

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

10 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

Related Questions

Help with rotating a sprite with mouse. 2 Answers

How do i rotate based on direction character is moving in 1 Answer

Rotating an object to target on the Z axis 1 Answer

The best way for working with rotation in 2d game 1 Answer

Character rotation and move to mouse click point with speed 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