Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Peysbubby · Jul 30, 2015 at 06:30 PM · c#rotation

How to rotate my object correctly?

I realize this question has been asked and answered a lot, however everything I can find either uses an outdated version of unity and the scripts don't work, the scripts use javaScript (I use c#), or it just plain doesn't work for what I want.

I have a green oval sprite with a slice out of it and a smaller green oval sprite without anything missing (see Fig. 1). I want the missing piece of the larger oval to always face the smaller oval no matter where the smaller oval is. (This is a 2d game and nothing should move along the "z" axis)

(Fig. 1)

alt text

This is the script I currently have:

 public Transform player;
     Vector3 target;
     
     void Update()
     {
         target = new Vector3(player.position.x, player.position.y, this.transform.position.z);
         transform.LookAt(target);
     }


However this does not work. It always rotates the larger oval 90 degrees along the "y" axis so the larger oval faces the smaller one like this:

alt text

I have used just about everything I can find and everything either doesn't work or ends in the same result.

Please explain to me why this is happening and how I can fix this.

thanks for your help in advance.

untitled.png (34.9 kB)
untitled2.png (52.0 kB)
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
Best Answer

Answer by _Gkxd · Jul 30, 2015 at 07:14 PM

LookAt rotates the forward vector (+z axis) to point at your the target. For a 2D game, this isn't what you want.

You can use Vector3.Angle instead, which gives you an angle between two vectors.

Here is some pseudocode for what the relevant code might look like:

 float angle = Vector3.Angle(Vector3.right, target.position - transform.position);

 transform.eulerAngles = new Vector3(0, 0, angle);

The above code assumes that target.position and transform.position always have a z value of 0.

Edit:

The above code doesn't actually work, since Vector3.Angle can't return angles greater than 180. The correct way would be to use Mathf.Atan2 instead.

 Vector3 difference = target.position - transform.position;
 float angle = Mathf.Atan2(difference.y, difference.x);

 // Do the same thing with angle as above
Comment
Add comment · Show 19 · 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 Peysbubby · Jul 30, 2015 at 07:37 PM 0
Share

Hi, this works beautifully for the top half of the object but the second I cross this line:alt text

It starts moving the opposite direction of where it should be following the second I cross that line.

I tried to edit the code some but nothing I did fixed the issue. Would you happen to know what is causing this?

3.png (9.6 kB)
avatar image _Gkxd · Jul 30, 2015 at 07:49 PM 0
Share

What do you mean by the opposite direction?

avatar image Peysbubby · Jul 30, 2015 at 08:29 PM 0
Share

alt text

this is with the small oval moving from right to left.

untitled3.png (81.6 kB)
avatar image _Gkxd · Jul 30, 2015 at 08:45 PM 0
Share

That is a bit strange... maybe I got the angle flipped. Does changing angle to -angle fix anything?

If not, could you post screenshots with the red/green/blue arrows in the scene view? That would help me understand what's happening a bit better.

avatar image Peysbubby · Jul 30, 2015 at 09:02 PM 0
Share

Changing angle to -angle fixes the bottom half however now it is doing it to the top half. $$anonymous$$y computer just started acting up and I cant seem to get it to take screenshots right now for some reason (this isn't the first time its done this)(its a somewhat old computer) it might take me a little bit to get the screenshots that you asked for to you.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How do I make the player rotate by following the mouse when I click? 0 Answers

Is it possible to add a point to the score, everytime the Player rotates 180°? 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