Hello, When I try to rotate this wheel if I click on partition 3, partition 1 automatically goes instead of 3 and then rotates as I want. Basically I would like to stay 3 under the cursor. Partition 3 was like an example. I need that no matter where I click the rotation starts from there. Any help, please? Sorry for my bad english. My code: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class rotate : MonoBehaviour { private void OnMouseDrag() { Vector3 mouse_pos = Input.mousePosition; Vector3 wheel_pos = Camera.main.WorldToScreenPoint(this.transform.position); mouse_pos.x = mouse_pos.x - wheel_pos.x; mouse_pos.y = mouse_pos.y - wheel_pos.y; float angle = Mathf.Atan2 (mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg; this.transform.rotation = Quaternion.Euler (new Vector3(0, 0, angle)); } } Edit:
Hello, Thanks for the reply. Partition 3 was like an example. I need that no matter where I click the rotation starts from there.
Yes, I tried but it didn't solve my problem. In attach is a simulation for the first situation. If I put "angle + 180 °" instead of partition 1 is partition 3, but it's still not what I need. Thanks anyway.
Ah. I see. You are just setting angle. Try getting angle on mouse click and then put the difference between angles into the rotation function. Code (CSharp): this.transform.rotation = Quaternion.Euler (new Vector3(0, 0, Mathf.Abs(angle_at_press - angle_at_release)));
Hi! For angle on mouse click: Code (CSharp): Vector3 angle_at_press = Camera.main.WorldToScreenPoint(Input.mousePosition); but how can I determine "angle_at_release"?
Oh, sorry. For you that is angle during drag. So you can reuse most of the stuff from your first post. Code (CSharp): private void OnMouseDrag() { Vector3 mouse_pos = Input.mousePosition; Vector3 wheel_pos = Camera.main.WorldToScreenPoint(this.transform.position); mouse_pos.x = mouse_pos.x - wheel_pos.x; mouse_pos.y = mouse_pos.y - wheel_pos.y; float angle = Mathf.Atan2 (mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg; this.transform.rotation = Quaternion.Euler (new Vector3(0, 0, Math.abs(angle - (Mathf.Atan2 (angle_at_press.y, angle_at_press.x) * Mathf.Rad2Deg)))); }
I have tried now but I get this error: The name `angle_at_press' does not exist in the current context.
You need to declare in context and assign it on mouse press. It won't exist otherwise. Code (CSharp): Vector3 angle_at_press; void Update () { if (Input.GetMouseButtonDown(0)) { angle_at_press = Camera.main.WorldToScreenPoint(Input.mousePosition); } } private void OnMouseDrag() { Vector3 mouse_pos = Input.mousePosition; Vector3 wheel_pos = Camera.main.WorldToScreenPoint(this.transform.position); mouse_pos.x = mouse_pos.x - wheel_pos.x; mouse_pos.y = mouse_pos.y - wheel_pos.y; float angle = Mathf.Atan2 (mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg; this.transform.rotation = Quaternion.Euler (new Vector3(0, 0, Math.abs(angle - (Mathf.Atan2 (angle_at_press.y, angle_at_press.x) * Mathf.Rad2Deg)))); }
Thank you very much for the answer. Neither this script doesn't work as I wanted. I think I'll give up with that. I'm a beginner and I should probably start with some easier things. Thanks again for your help and for your time.