Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by JoeBrar · Mar 29, 2018 at 08:12 PM · rotationquaternioneulerangleseulerlookrotation

How to make a 2D sprite rotate towards a specific point while facing perpendicular to the camera ?

So I am creating a 2D project.I want my 2D sprite worm to rotate in the direction of a specific point(for eg the football sprite) in my game plane . I tried using LookRotation but the problem is the worm is not facing perpendicular to the camera so it is not visible in 2D view . My game plane is X-Y plane . When I run it , it looks like this - alt text

Notice that the worm is not visible . Here is the 3D view -

alt text

This is the code I used :

         Vector3 relativePos = target.transform.position - worm.transform.position;
         Quaternion rotation=Quaternion.LookRotation (relativePos);   
         worm.transform.rotation = rotation;

Also , changing the upward direction ,like setting it to vector3.forward , is not helping . Please help , I am stuck here ! I will be grateful to you !

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
1

Answer by Scribe · Mar 30, 2018 at 07:09 PM

This is a definitely a good time to use Quaternion.LookRotation, unfortunately the way you want to specify the directions is a bit convoluted because of the orientation that you want.

The inmortant bit to notice is that you want the forward direction (the worms blue axis) to oint at the camera, which I have assumed is in the direction -Vector3.forward (on the penultimate line), and the up direction is not straight up, we have to calculate oour up direction by knowing it is perpendicular (at right angles to) the relativePos vector that you calculated. The following is untested but it should give you something to go on, this might help understanding

 Vector3 relativePos = target.transform.position - worm.transform.position;
 
 // get the desired up vector by finding the vector perpendicular to relativePos
 // and make sure we have a positive y value
 // the following works because if y is going to be negative then Mathf.Sign is -1 and so inverts the vector
 // otherwise it does nothing
 Vector3 desiredUp = new Vector3(relativePos.y, -relativePos.x, 0) * Mathf.Sign(-relativePos.x);
 
 Quaternion rotation = Quaternion.LookRotation (-Vector3.forward, desiredUp);   
 worm.transform.rotation = rotation;

Hope that helps!

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 JoeBrar · Mar 31, 2018 at 12:58 AM 0
Share

Thanks for your help , although I already found the solution by another method .

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

160 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 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 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 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 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 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 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 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 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 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

Something seriously weird is going on with my transform.eulerAngles 0 Answers

Problem with Quaternion and Euler Angles 0 Answers

Draw a ray from a gamobject and keep direction of the ray relative to the gameobjects rotation. 1 Answer

Rotating an Object (To Face Another Object) Only on X and Y Axis 3 Answers

Quaternion Rotation - AngleAxis/EulerAngle 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