Unity 2D Вид зверху + обертання + постріл

 

Герой має бути всередині пустого об’єкту
Герой дивиться вправо (якщо ні – покласти його в пустий об’єкт і повернути на 90)
Куля дивиться вправо

public GameObject playerImage;
public GameObject bullet;
public GameObject bulletPosition;
public float playerSpeed = 6.0f;
public float bulletSpeed = 400.0f;
public UnityEngine.UI.Text t1;
Rigidbody2D rb;
Vector2 movement;
char direction = 0;

void Start()
{
  rb = GetComponent<Rigidbody2D>();
  t1.text = "Bullets: " + bullets;
}

void Update()
{
  movement.x = Input.GetAxisRaw("Horizontal");
  movement.y = Input.GetAxisRaw("Vertical");

  if (Input.GetKeyDown(KeyCode.Space))
  {
    GameObject go = Instantiate(bullet,
    bulletPosition.transform.position,
    Quaternion.Euler(0, 0, 0));
    Destroy(go, 5f);

    if (movement.y > 0)
    {
      go.transform.rotation = Quaternion.Euler(0, 0, 90);
      go.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 5) * bulletSpeed);
    }
    else if (movement.y < 0)
    {
      go.transform.rotation = Quaternion.Euler(0, 0, -90);
      go.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, -5) * bulletSpeed);
    }
    else if (movement.x > 0)
    {
      go.transform.rotation = Quaternion.Euler(0, 0, 0);
      go.GetComponent<Rigidbody2D>().AddForce(new Vector2(5, 0) * bulletSpeed);
    }
    else if (movement.x < 0)
    {
      go.transform.rotation = Quaternion.Euler(0, 0, 180);
      go.GetComponent<Rigidbody2D>().AddForce(new Vector2(-5, 0) * bulletSpeed);
    }
    else
    {
      if (direction == 0)
      {
        go.transform.rotation = Quaternion.Euler(0, 0, 0);
        go.GetComponent<Rigidbody2D>().AddForce(new Vector2(5, 0) * bulletSpeed);
      }
      else if (direction == 1)
      {
        go.transform.rotation = Quaternion.Euler(0, 0, 180);
        go.GetComponent<Rigidbody2D>().AddForce(new Vector2(-5, 0) * bulletSpeed);
      }
      else if (direction == 2)
      {
        go.transform.rotation = Quaternion.Euler(0, 0, 90);
        go.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 5) * bulletSpeed);
      }
      else if (direction == 3)
      {
        go.transform.rotation = Quaternion.Euler(0, 0, -90);
        go.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, -5) * bulletSpeed);
      }
    }
  }

  if (movement.y > 0)
  {
    direction = 2;
    playerImage.transform.rotation = Quaternion.Euler(0, 0, 90);
  }
  else if (movement.y < 0)
  {
    direction = 3;
    playerImage.transform.rotation = Quaternion.Euler(0, 0, -90);
  }
  else if (movement.x > 0)
  {
    direction = 0;
    playerImage.transform.rotation = Quaternion.Euler(0, 0, 0);
  }
  else if (movement.x < 0)
  {
    direction = 1;
    playerImage.transform.rotation = Quaternion.Euler(0, 0, 180);
  }
}

void FixedUpdate()
{
  rb.MovePosition(rb.position + movement * playerSpeed * Time.fixedDeltaTime);
}