ArenaShooter/Assets/Scripts/PlayerShoot.cs
2021-06-21 23:08:41 +02:00

42 lines
No EOL
1.1 KiB
C#

using UnityEngine;
public class PlayerShoot : MonoBehaviour {
public float damage = 10f;
public float range = 100f;
public float fireRate = 2f;
public bool automatic = false;
public ParticleSystem particle;
public Camera camera;
private float nextTimeToFire = 0f;
private void Update() {
if (automatic) {
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire) {
shoot();
}
}
else {
if (Input.GetButtonDown("Fire1") && Time.time >= nextTimeToFire) {
shoot();
}
}
}
protected virtual void shoot() {
particle.Play();
RaycastHit hit;
if (Physics.Raycast(camera.transform.position, camera.transform.forward, out hit, range)) {
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null) {
target.takeDamage(damage, transform);
}
nextTimeToFire = Time.time + 1f / fireRate;
}
}
}