• 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 Scorpion2018 · Mar 05, 2020 at 09:37 PM · rotationrotaterotate objectcircle

Advance Unity 2d apply circle Rotation-z with mouse

Sorry I'm new to this, I am trying to rotate a circle from A to B with mouse.position, but I would like that the next time I receive a new mouse.position the circle rotates in z from the last position (B) to C, can someone help me ? Thanksalt text current code:

     Vector2 positionOnScreen = Camera.main.WorldToViewportPoint(transform.position);
 
         
         Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
        
        
         angle = AngleBetweenTwoPoints(positionOnScreen, mouseOnScreen);
 
         
 //apply Rotation
         transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle));
 
 
         float AngleBetweenTwoPoints(Vector3 a, Vector3 b)
         {
             return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
         }

atob.png (5.6 kB)
Comment
Add comment · Show 2
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 tormentoarmagedoom · Mar 06, 2020 at 12:58 PM 0
Share

CAn you explain what "next time I receive a new mouse.position the circle rotates in z" means?

In Z what? give reference

avatar image Scorpion2018 tormentoarmagedoom · Mar 06, 2020 at 01:55 PM 0
Share

Sorry i didn't explain well, so i'm trying to rotate the circle in transform.rotation.z, and i can with this code, the problem is that when i rotate the circle again(click with mouse again) it directs the object to the mouse position (recognizes the mouse position as direction to start position to the next position) ins$$anonymous$$d of rotating from last position to the mouse position.

here is the video: https://takeafile.com/?f=rixukecagi

1 Reply

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

Answer by tormentoarmagedoom · Mar 06, 2020 at 02:08 PM

Hello.

You just need to apply your last rotation every time. So every time you rotate, need to store its final rotation for the next time:

  transform.rotation =  LastRotation + Quaternion.Euler(new Vector3(0f, 0f, angle));
  LastRotation = transform.rotation;

Bye !

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 Scorpion2018 · Mar 06, 2020 at 02:38 PM 0
Share

Thanks by the answer, but is giving error...

alt text

error.png (25.0 kB)
avatar image tormentoarmagedoom Scorpion2018 · Mar 07, 2020 at 08:44 PM 0
Share

$$anonymous$$y answer was not code, was just n "structure. You need to SU$$anonymous$$ Euler angles!

 LastRotation = transform.eulerAngles

avatar image Scorpion2018 tormentoarmagedoom · Mar 11, 2020 at 01:58 PM 0
Share

It works!! thanks a lot!!

transform.rotation = Quaternion.AngleAxis(angle - startAngle, Vector3.forward);

avatar image TreyH · Mar 06, 2020 at 04:11 PM 0
Share

I think you meant to multiply the quaternions, but the order would be off for that too

avatar image Scorpion2018 TreyH · Mar 06, 2020 at 05:37 PM 0
Share

I tried, but thanks.

avatar image Scorpion2018 · Mar 06, 2020 at 05:39 PM 0
Share

I think its not that easy....

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

163 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 avatar image avatar image avatar image

Related Questions

Have an object continuously rotate towards another 1 Answer

What´s wrong with my rotation function? 0 Answers

How can I rotate an object without moving it up or down? 0 Answers

managing rotation of Turret on two axis 1 Answer

rotate object around another object 1:1 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges