Вид зверху + обертання + постріл
Герой має бути всередині пустого об'єкту
Герой дивиться вправо (якщо ні - покласти пустий об'єкт і повернути на 90)
Куля дивиться вправо
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
public GameObject obj;
public GameObject bullet;
public GameObject bulPos;
public float moveSpeed = 6.0f;
public float bulletSpeed = 100.0f;
Rigidbody2D rb;
Vector2 movement;
int direction = 0;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
if (Input.GetKeyDown(KeyCode.Space))
{
GameObject go = Instantiate(bullet, bulPos.transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
Destroy(go, 5f);
if (movement.y > 0)
{
go.transform.rotation = Quaternion.Euler(0, 0, 90);
go.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 5) * bulletSpeed);
}
if (movement.y < 0)
{
go.transform.rotation = Quaternion.Euler(0, 0, -90);
go.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, -5) * bulletSpeed);
}
if (movement.x > 0)
{
go.transform.rotation = Quaternion.Euler(0, 0, 0);
go.GetComponent<Rigidbody2D>().AddForce(new Vector2(5, 0) * bulletSpeed);
}
if (movement.x < 0)
{
go.transform.rotation = Quaternion.Euler(0, 0, 180);
go.GetComponent<Rigidbody2D>().AddForce(new Vector2(-5, 0) * bulletSpeed);
}
if (movement.x == 0 && movement.y == 0)
{
if (direction==0)
{
go.transform.rotation = Quaternion.Euler(0, 0, 0);
go.GetComponent<Rigidbody2D>().AddForce(new Vector2(5, 0) * bulletSpeed);
}
if (direction == 1)
{
go.transform.rotation = Quaternion.Euler(0, 0, 180);
go.GetComponent<Rigidbody2D>().AddForce(new Vector2(-5, 0) * bulletSpeed);
}
if (direction == 2)
{
go.transform.rotation = Quaternion.Euler(0, 0, 90);
go.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 5) * bulletSpeed);
}
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;
obj.transform.rotation = Quaternion.Euler(0, 0, 90);
}
if (movement.y < 0)
{
direction = 3;
obj.transform.rotation = Quaternion.Euler(0, 0, -90);
}
if (movement.x > 0)
{
direction = 0;
obj.transform.rotation = Quaternion.Euler(0, 0, 0);
}
if (movement.x < 0)
{
direction = 1;
obj.transform.rotation = Quaternion.Euler(0, 0, 180);
}
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}