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.
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.
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);
}