Rotating GameObject (Sprite2D) without moving end of sprite

Hello,

so the problem is that I got an object looking at the mouse cursor. It is supposed rotate towards the object like shown in the following picture.

56261-skjermbilde2.png

The green object is connected to the red object. When looking at the cursor, the green object should rotate towards the cursor, but I would like the end of the object to still stick on the red object.

The code I have produces the following scenario.

56262-skjermbilde3.png

As you can see the green object is rotating but the end of it moves away from the red object. I know this makes sence as the X and Y position coordinates is not changed when rotating.

Is there a good way to do this in Unity? I was trying Hinge Joint 2D but it did not seem to work at all. What I did there was to add a rigidbody to the red object at the position where I would like the green object to be attached and then added a rigidbody to the green object and a Hinge Joint. And then placed the anchor and the connected anchor at the middle of the red object. This produced the same problem output as mentioned above.

Thanks in advance.

Here is the rotating script, if it helps:

 public float sensitivityY = 15F;
    public float minimumY = -40F;
    public float maximumY = 0F;
    float rotationY = 0F;
    Quaternion originalRotation;

    void Start()
    {
        originalRotation = transform.rotation;
    }

    void Update()
    {
        
            rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
            rotationY = ClampAngle(rotationY, minimumY, maximumY);
            Quaternion yQuaternion = Quaternion.AngleAxis(-rotationY, Vector3.forward);
            transform.localRotation = originalRotation * yQuaternion;
        
    }
  
    public static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360F)
            angle += 360F;
        if (angle > 360F)
            angle -= 360F;
        return Mathf.Clamp(angle, min, max);
    }

If you find the sprite for the “green object” in your assets folder under the inspector there should be a pivot option; in the case of your example, if you set the green objects pivot to “right” it will behave as you wanted it to. If your sprite is part of a spritesheet then you can adjust each pivot point individually under the sprite editor.

Write this code in the update() function:
this.transform.RotateAround(RedPoint.trasnform.position, Vector3.forward, float angle);

-RedPoint.trasnform.position - this is the red object’s vector3 position; This is the point the green object is going to rotate around.

-Vector3.forward - rotate in the z axis. The axis the object will be rotating

-float angle - What angle the green object will have <— this will change every frame based on where the mouse is.

All you have to do is put the red object on the desired point of the green object and based on the position of the mouse or whatever you put in for float angle value, the green object will rotate accordingly.

I hope this gave you a little bit of an idea of how to solve your problem.