- Home /
2D spaceship rotation
Hello. I'm writing a simple 2D game with spaceships in it. You control them by using a joystick or WASD, and they can fly up, down, left, and right (same viewpoint as Asteroids.) I have a little script that rotates the ship to face the directions you're moving it in, so if you're going down and to the right the ship will reflect that. My other concern in making is it was that at the end of the game, gravity gets applied and the ship off the screen - so it needs to apply whenever it's moving, not just when it's being controlled. Furthermore, I had an issue where the ship was upside down when I went to the right - so I made the script flip the sprite when it was going right. I wrote this:
function CalcDir(){
GetComponent("SpriteRenderer").flipX = false;
lookDirection = new Vector3(-GetComponent.<Rigidbody2D>().velocity.y, GetComponent.<Rigidbody2D>().velocity.x, 0);
transform.up = lookDirection;
Debug.Log(lookDirection);
if (lookDirection.y < 0){
GetComponent("SpriteRenderer").flipY = true;
}
else {
GetComponent("SpriteRenderer").flipY = false;
}
if(lookDirection.x == 0){
GetComponent("SpriteRenderer").flipX = true;
}
}
Note that I flip the X and Y values in lookDirection because the ship has to be facing sideways. So I got this working, but there's a weird bug. When I'm going perfectly right (no other velocity), I find that my sprite unflips and moves backwards. What's going on?