I’m building demo for a top-down shooter and I want the sprite to aim and rotate where the mouse is pointing. I’m having trouble getting the sprite to rotate correctly. It only goes 180 and then back to 0, not going a full 360. here’s the code:
onMouseMove(event: EventMouse) {
const mousePos = event.getUILocation();
// Calculate the angle between the mouse position and the center of the screen
const canvas = find("Canvas").getComponent(UITransform);
const center = new Vec2(canvas.width / 2, canvas.height / 2);
const direction = new Vec2(mousePos.x - center.x, mousePos.y - center.y);
const angle = Vec2.angle(direction, new Vec2(0, 1)) * 180 / Math.PI;
// Set the rotation of the line based on the angle
this.mainPlayer.setRotationFromEuler(new Vec3(0, 0, angle));
}
I also tried replacing 180 with 360 but it looks like its not allowing negative numbers. Does anyone have experience with this?