Search Unity

  1. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Rotate object 2d

Discussion in 'Scripting' started by thomaesjohansen, Aug 9, 2019.

  1. thomaesjohansen

    thomaesjohansen

    Joined:
    Aug 9, 2019
    Posts:
    14
    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):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class rotate : MonoBehaviour
    6. {
    7.     private void OnMouseDrag()
    8.     {
    9.         Vector3 mouse_pos = Input.mousePosition;
    10.         Vector3 wheel_pos = Camera.main.WorldToScreenPoint(this.transform.position);
    11.  
    12.         mouse_pos.x = mouse_pos.x - wheel_pos.x;
    13.         mouse_pos.y = mouse_pos.y - wheel_pos.y;
    14.  
    15.         float angle = Mathf.Atan2 (mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
    16.         this.transform.rotation = Quaternion.Euler (new Vector3(0, 0, angle));
    17.     }
    18. }
    UNITYprintscreen.jpg

    Edit:
     
    Last edited: Aug 9, 2019
  2. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
    If it's exactly the opposite direction, try just rotating by angle+180° instead of just angle.
     
  3. thomaesjohansen

    thomaesjohansen

    Joined:
    Aug 9, 2019
    Posts:
    14
    Hello,
    Thanks for the reply.
    Partition 3 was like an example. I need that no matter where I click the rotation starts from there.
     
  4. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
    Have you tried solution which I proposed?
     
  5. thomaesjohansen

    thomaesjohansen

    Joined:
    Aug 9, 2019
    Posts:
    14
    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.
     

    Attached Files:

  6. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
    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):
    1. this.transform.rotation = Quaternion.Euler (new Vector3(0, 0, Mathf.Abs(angle_at_press - angle_at_release)));
     
  7. thomaesjohansen

    thomaesjohansen

    Joined:
    Aug 9, 2019
    Posts:
    14
    Hi!
    For angle on mouse click:
    Code (CSharp):
    1. Vector3 angle_at_press = Camera.main.WorldToScreenPoint(Input.mousePosition);
    but how can I determine "angle_at_release"?
     
  8. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
    Oh, sorry. For you that is angle during drag. So you can reuse most of the stuff from your first post.

    Code (CSharp):
    1. private void OnMouseDrag()
    2.     {
    3.         Vector3 mouse_pos = Input.mousePosition;
    4.         Vector3 wheel_pos = Camera.main.WorldToScreenPoint(this.transform.position);
    5.         mouse_pos.x = mouse_pos.x - wheel_pos.x;
    6.         mouse_pos.y = mouse_pos.y - wheel_pos.y;
    7.         float angle = Mathf.Atan2 (mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
    8.         this.transform.rotation = Quaternion.Euler (new Vector3(0, 0, Math.abs(angle - (Mathf.Atan2 (angle_at_press.y, angle_at_press.x) * Mathf.Rad2Deg))));
    9.     }
     
    thomaesjohansen likes this.
  9. thomaesjohansen

    thomaesjohansen

    Joined:
    Aug 9, 2019
    Posts:
    14
    I have tried now but I get this error:
    The name `angle_at_press' does not exist in the current context.
     
  10. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
    You need to declare in context and assign it on mouse press. It won't exist otherwise.

    Code (CSharp):
    1.  
    2. Vector3 angle_at_press;
    3.  
    4. void Update ()
    5.  {
    6.  
    7.     if (Input.GetMouseButtonDown(0))
    8.     {
    9.         angle_at_press = Camera.main.WorldToScreenPoint(Input.mousePosition);
    10.     }
    11. }
    12.  
    13. private void OnMouseDrag()
    14.     {
    15.         Vector3 mouse_pos = Input.mousePosition;
    16.         Vector3 wheel_pos = Camera.main.WorldToScreenPoint(this.transform.position);
    17.         mouse_pos.x = mouse_pos.x - wheel_pos.x;
    18.         mouse_pos.y = mouse_pos.y - wheel_pos.y;
    19.         float angle = Mathf.Atan2 (mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
    20.         this.transform.rotation = Quaternion.Euler (new Vector3(0, 0, Math.abs(angle - (Mathf.Atan2 (angle_at_press.y, angle_at_press.x) * Mathf.Rad2Deg))));
    21.     }
     
  11. thomaesjohansen

    thomaesjohansen

    Joined:
    Aug 9, 2019
    Posts:
    14
    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. :)
     
unityunity