Hey guys, so I’m working on a canon that rotates in the opposite direction of the mouse position. To try to make a drag system to shoot stuff (like catapaults). I’ve tried using transform.LookAt and Quaternion.LookRotation but they keep rotating the canon in it’s local z axis when i get to half of a full rotation (180 degrees). I even tried using a gameobject as a target but it still does the same thing
alt text
Vector3 targPos = new Vector3(test.transform.position.x, test.transform.position.y, canon.transform.position.z); canon.transform.LookAt(targPos);
Here is it with the mouse position as target
void Update()
{
mousePos = Input.mousePosition;
mousePos.z = zDis - cam.transform.position.z;
mouseLookAt = PosInvert(canon.transform.position,cam.ScreenToWorldPoint(mousePos));
canon.transform.LookAt(mouseLookAt);
}
Vector3 PosInvert(Vector3 origin, Vector3 pos)
{
float oX = origin.x;
float oY = origin.y;
float pX = pos.x;
float pY = pos.y;
float x = pX - oX;
x = oX - x;
float y = pY - oY;
y = oY - y;
return new Vector3(x, y, pos.z);
}
If someone could help me with the code using the mouse position, as a target i would be very grateful