67 lines
1.8 KiB
C#
67 lines
1.8 KiB
C#
using Palmmedia.ReportGenerator.Core.CodeAnalysis;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
public class GunController : MonoBehaviour
|
|
{
|
|
Camera cam;
|
|
public LayerMask mouseHitMask;
|
|
public Rigidbody bulletPrefab;
|
|
public Rigidbody bulletInstance;
|
|
public float bulletForce = 10;
|
|
public float forceYOffset = 0;
|
|
public bool isMissle = false;
|
|
public float missleSpeed = 10;
|
|
public float missleRotateSpeed = 5;
|
|
|
|
private void Awake()
|
|
{
|
|
cam = Camera.main;
|
|
bulletInstance.useGravity = false;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!Input.GetMouseButtonDown(0))
|
|
return;
|
|
var ray = cam.ScreenPointToRay(Input.mousePosition);
|
|
ray.direction += Vector3.up * forceYOffset;
|
|
var hitPoint = this.transform.position + ray.direction * 1000;
|
|
ShootOnce(hitPoint);
|
|
}
|
|
private void ShootOnce(Vector3 hitPoint)
|
|
{
|
|
var dir = hitPoint - this.transform.position;
|
|
|
|
if (bulletInstance == null)
|
|
return;
|
|
|
|
|
|
bulletInstance.useGravity = true;
|
|
if (isMissle)
|
|
{
|
|
bulletInstance.transform.forward = dir;
|
|
var missle = bulletInstance.gameObject.AddComponent<Missle>();
|
|
missle.targetTrans = GameObject.FindObjectOfType<Target>().transform;
|
|
missle.moveSpeed = missleSpeed;
|
|
missle.rotateSpeed = missleRotateSpeed;
|
|
}
|
|
else
|
|
{
|
|
bulletInstance.AddForce(dir.normalized * bulletForce);
|
|
}
|
|
bulletInstance = null;
|
|
}
|
|
private async void Shoot(Vector3 hitPoint)
|
|
{
|
|
ShootOnce(hitPoint);
|
|
|
|
await Task.Delay(500);
|
|
if (bulletInstance == null)
|
|
{
|
|
bulletInstance = GameObject.Instantiate(bulletPrefab, this.transform);
|
|
bulletInstance.useGravity = false;
|
|
}
|
|
}
|
|
}
|