Although the code seems right, for some reason in the game the sprite doesnt visibly rotate towards the cursor, it doesnt rotate at all
Texture playerTexture;
Sprite playerSprite;
Vector3 playerPos = new Vector3();
public void create () {
batch = new SpriteBatch();
playerTexture = new Texture("playerAiming.png");
playerSprite = new Sprite(playerTexture);
playerSprite.setOrigin(playerSprite.getWidth() / 2, playerSprite.getHeight() / 2);
}
public void render () {
float speed = playerSpeed * Gdx.graphics.getDeltaTime();
float mouseX = Gdx.input.getX();
float mouseY = Gdx.input.getY();
float playerX = playerPos.x + playerSprite.getWidth() / 2;
float playerY = playerPos.y + playerSprite.getHeight() / 2;
float angle = MathUtils.atan2(mouseY - playerY, mouseX - playerX) * MathUtils.radiansToDegrees;
playerSprite.setRotation(angle); //Doesnt work for some reason
ScreenUtils.clear(1, 0, 0, 1);
batch.begin();
batch.draw(playerSprite, playerPos.x, playerPos.y);
batch.end();
//Movement codes and others...
}
The variables are working just fine(i sysod them), i tried using straight up Texture and also wouldnt work
New contributor
Jeipi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.