using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
public class main : MonoBehaviour
{
[SerializeField] GameObject gun,bullet;
private Vector3 mousePos, dir;
float rotateZ;
private void Update()
{
mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z));
dir = mousePos - gun.transform.position;
rotateZ = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
gun.transform.rotation = Quaternion.Euler(0,0,rotateZ);
if (Input.GetMouseButtonDown(0))
{
shoot();
}
}
void shoot()
{
// Silahın Z rotasyonunu bul.
float rotateZ = gun.transform.rotation.z;
// Merminin spawn pozisyonunu hesapla.
Vector3 spawnPosition = gun.transform.position + Vector3.forward * Mathf.Cos(rotateZ) * bulletSpeed + new Vector3(0, 0, 0.1f);
// Mermini spawnla.
Instantiate(bullet, spawnPosition, Quaternion.identity);
}
}