Rotate object 2d

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. :frowning:

My code:

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:

If it’s exactly the opposite direction, try just rotating by angle+180° instead of just angle.

Hello,
Thanks for the reply.
Partition 3 was like an example. I need that no matter where I click the rotation starts from there.

Have you tried solution which I proposed?

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.

4839428--464816--simulation.png

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.

this.transform.rotation = Quaternion.Euler (new Vector3(0, 0, Mathf.Abs(angle_at_press - angle_at_release)));

Hi!
For angle on mouse click:

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.

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))));
    }
1 Like

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.

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. :slight_smile: