Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Two pivot points for 2D Object

Discussion in 'Getting Started' started by madsen_ulrik, May 16, 2024.

  1. madsen_ulrik

    madsen_ulrik

    Joined:
    Jan 7, 2024
    Posts:
    2
    Hi, I am new to Unity.
    I have made 2 games following tutorials.

    Now I want to make a simple game of my own, but I have problem.

    When I right click on the tile shown here, it rotates around the yellow fish pivot = (0.5 , 0.75).
    upload_2024-5-16_11-31-3.png


    upload_2024-5-16_11-32-17.png

    When I left click I want the tile to rotate around the black fish, and I thought changing the pivot point to (0.5, 0.25) would do it, but it still rotates around the yellow fish. I can see in the inspector, that it changes.
    Maybe I have misunderstood, how pivot works?

    Any suggestions to how I make the tile rotate in 2 differnet ways?
    Thanks!


    Here is what I have:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using Unity.VisualScripting;
    5. using Unity.VisualScripting.Dependencies.Sqlite;
    6. using UnityEditor.Tilemaps;
    7. using UnityEngine;
    8. public class PieceMovement : MonoBehaviour
    9. {
    10.     private Camera _myCam;
    11.     private Collider2D _col;
    12.     private int _angle;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         _angle = 0;
    18.         _myCam = Camera.main;
    19.         _col = GetComponent<BoxCollider2D>();
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         Vector3 mousePos = _myCam.ScreenToWorldPoint(Input.mousePosition);
    26.         if (Input.GetMouseButtonDown(1) && _col == Physics2D.OverlapPoint(mousePos))
    27.         {
    28.             (gameObject.transform as RectTransform).pivot = new Vector2(0.5f, 0.75f);
    29.             transform.rotation = Quaternion.Euler(0, 0, _angle -= 90);
    30.         }
    31.         if (Input.GetMouseButtonDown(0) && _col == Physics2D.OverlapPoint(mousePos))
    32.         {
    33.             (gameObject.transform as RectTransform).pivot = new Vector2(0.5f, 0.25f);
    34.             transform.rotation = Quaternion.Euler(0, 0, _angle -= 90);
    35.          
    36.         }
    37.     }
    38. }
    39.  
     
    Last edited: May 17, 2024
  2. madsen_ulrik

    madsen_ulrik

    Joined:
    Jan 7, 2024
    Posts:
    2
    I have come up with a less elegant solution.

    I simply rotate around the yellow fish, and then change the position of the tile to where it is supposed to be, if it was rotated around the black fish.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,701
    A note for the future: when posting code, please use code-tags and not plain text.

    Thanks.