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 Frede1012 · Dec 05, 2013 at 07:25 PM · c#2dspriterotatez axis

Sprite "face" second sprite only on Z axis

Hello everyone. Im trying to make my sprite "face" another sprite (rotate on the Z axis to face it ingame).

And i tried using LookAt, but then it rotates on X & Y axis.

I need help with figuring out how to rotate on the Z axis only and face the other sprite. (The game is "top-down")

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

Answer by robertbu · Dec 05, 2013 at 07:31 PM

I'm having to guess a bit. I'm assuming the camera is at negative 'z' looking towards positive 'z'. I'm guessing that the up of the object is the side you want to face towards your target. If so, then this should work:

 transform.up = target.position - transform.position;

If not this will work:

 tranform.rotation = Quaternion.FromToRotation(transform.up, target.position - transform.position) * transform.rotation;
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 Frede1012 · Dec 05, 2013 at 07:52 PM 0
Share

The problem is don't have any rotation code :S I tried your code, and it does like all the code i have already tried. It rotates on every axis except Z..

avatar image robertbu · Dec 05, 2013 at 08:18 PM 0
Share

I'm not sure of your setup, and folks use 'axis' in different ways, and Unity may report a different rotation than the one you expect. As a first try, change the middle line to:

 pos.x = transform.position.x;

or

 pos.y = transform.position.y;

If that does not work, post a screen shot of your object with axes painted in and post the code you use for rotation (i.e. the code with the LookAt()). In addition, we need to know the natural orientation of your object (how it looks with rotation (0,0,0).

avatar image Frede1012 · Dec 05, 2013 at 08:25 PM 0
Share

It still doesnt work. Here is a screenshot: http://imgur.com/6itYja3 And my code is just an object which has a GameObject called target (which it has to face)

avatar image robertbu · Dec 05, 2013 at 09:31 PM 1
Share

I'm having to guess a bit. I'm assu$$anonymous$$g the camera is at negative 'z' looking towards positive 'z'. I'm guessing that the up of the object is the side you want to face towards your target. If so, then this should work:

 transform.up = target.position - transform.position;

If not this will work:

 tranform.rotation = Quaternion.FromToRotation(transform.up, target.position - transform.position) * transform.rotation;
avatar image Frede1012 · Dec 06, 2013 at 11:33 AM 0
Share

Thanks! Edit your answer and i will accept it ;-)

Show more comments
avatar image
1

Answer by Tomer-Barkan · Dec 05, 2013 at 10:02 PM

Just set transform.up so that it points on your target (I'm assuming the sprite is facing upwards when it's not rotated)

 public void LookAt2D(Transform target) {
     transform.up = target.position = transform.position;
 }

You can't use LookAt() in 2D, because LookAt() causes the transform's forward vector to face the target, but in 2D games your sprite faces up by default, not forward, so setting the forward vector to look at the target will do no good.

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

17 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

Related Questions

Sprite image not changing 0 Answers

Way to have 2D Sprites lit by Lights? 3 Answers

Distribute terrain in zones 3 Answers

Rotate a 2D sprite without rotating the transform 5 Answers

Strange artifacts in builds 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