Reputation: 11
I have a beer bottle positioned at top of a glass at 90%. I want to rotate it around its Pivot which is at the top. In order to do so i'm trying to find the angle between the the mouseposition(mp) and the bottle and rotate ti by it.
The center of rotation is the current position of the GameObject since the Pivot of the sprite is at the top. I tried to find two Vectors one being the vector from mp to center of rotation and the other one being the position of the bottle. Then i used: gameObject.transform.Rotate(Vector3.Forward, Vector3.Angle(v2,v1)).
The result its not what i expected of course. I'm new to this game math, i'd appreciate an explanation.
(Its an android game and i intend to drag the bottle up and down from 90 to 180 degrees).
Upvotes: 1
Views: 1303
Reputation: 1556
MrApnea's link says that the dot product is the key operation needed to calculate the angle (θ) between the two object's positions.
θ = Math.Acos (Vector3.Dot (mouseClick, startPosition) / mouseClick.magnitude * startPosition.magnitude)
Reference: The "Geometric Definition" illustration in Wikipedia's Dot Product page
Upvotes: 0
Reputation: 1946
I hope that i understand you question correctly. But if you want to find the angle between the mouse point you can use the two lines you have drawn on the picture and just calculate the angle between them.
Check out this answer:
Calculating the angle between two lines without having to calculate the slope? (Java)
Upvotes: 1