2023-04-03 17:02:11 +02:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
2023-04-10 04:30:54 +02:00
|
|
|
using static UnityEngine.UI.Image;
|
2023-04-03 17:02:11 +02:00
|
|
|
|
|
|
|
public class PistolComponent : MonoBehaviour
|
|
|
|
{
|
|
|
|
[SerializeField]
|
|
|
|
private Light targetingLight;
|
|
|
|
|
|
|
|
private bool IsEnabled = false;
|
|
|
|
|
|
|
|
GameObject targetObject;
|
2023-04-10 04:30:54 +02:00
|
|
|
|
2023-04-03 17:02:11 +02:00
|
|
|
[SerializeField]
|
|
|
|
GameObject targetObjectPrefab;
|
2023-04-04 01:23:20 +02:00
|
|
|
[SerializeField]
|
|
|
|
private GameObject projectilePrefab;
|
|
|
|
[SerializeField]
|
|
|
|
private Transform bulletSpawnPoint;
|
|
|
|
[SerializeField]
|
|
|
|
private float firePower = 20f;
|
|
|
|
[SerializeField]
|
|
|
|
private float maxProjectileDuration = 5f;
|
|
|
|
|
2023-04-10 04:30:54 +02:00
|
|
|
|
|
|
|
|
2023-04-04 01:23:20 +02:00
|
|
|
//private Dictionary<int,float> projectiles = new Dictionary<int, float>();
|
2023-04-03 17:02:11 +02:00
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2023-04-10 04:30:54 +02:00
|
|
|
}
|
|
|
|
private float CalculateDrop(Vector3 origin,Vector3 destination,Vector3 force)
|
|
|
|
{
|
|
|
|
// Calculate the initial velocity required to reach the destination.
|
|
|
|
Vector3 displacement = destination - origin;
|
|
|
|
float time = Mathf.Sqrt(2f * displacement.magnitude / Physics.gravity.magnitude);
|
|
|
|
Vector3 velocity = (displacement - 0.5f * Physics.gravity * time * time) / time + force;
|
|
|
|
|
|
|
|
// Calculate the height the object will reach during its flight.
|
|
|
|
float maxHeight = origin.y + velocity.y * time - 0.5f * Physics.gravity.y * time * time;
|
|
|
|
|
|
|
|
// Calculate the distance the object will drop during its flight.
|
|
|
|
float dropDistance = maxHeight - destination.y;
|
|
|
|
|
|
|
|
return dropDistance;
|
2023-04-03 17:02:11 +02:00
|
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
|
|
{
|
|
|
|
if (this.IsEnabled)
|
|
|
|
{
|
|
|
|
Ray ray = new Ray(transform.position, transform.up);
|
|
|
|
RaycastHit hit;
|
|
|
|
if (Physics.Raycast(ray, out hit, 50))
|
|
|
|
{
|
|
|
|
targetObject.gameObject.transform.position = hit.point;
|
2023-04-10 04:30:54 +02:00
|
|
|
float drop = CalculateDrop(this.bulletSpawnPoint.position, hit.point, this.transform.up * this.firePower);
|
|
|
|
print(drop);
|
|
|
|
|
2023-04-03 17:02:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-04 01:23:20 +02:00
|
|
|
|
|
|
|
|
2023-04-03 17:02:11 +02:00
|
|
|
|
2023-04-04 01:23:20 +02:00
|
|
|
}
|
|
|
|
public void Fire()
|
|
|
|
{
|
|
|
|
GameObject projectile = Instantiate(projectilePrefab, this.bulletSpawnPoint);
|
|
|
|
projectile.transform.localPosition = Vector3.zero;
|
|
|
|
projectile.transform.localEulerAngles = Vector3.zero;
|
|
|
|
projectile.transform.localScale = Vector3.one;
|
|
|
|
Rigidbody pRigid = projectile.GetComponent<Rigidbody>();
|
|
|
|
pRigid.AddForce(pRigid.transform.up*this.firePower, ForceMode.Impulse);
|
|
|
|
projectile.transform.parent = null;
|
2023-04-03 17:02:11 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
public void Enable()
|
|
|
|
{
|
|
|
|
IsEnabled = true;
|
|
|
|
if (targetObject == null)
|
|
|
|
{
|
|
|
|
targetObject = Instantiate(targetObjectPrefab);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public void Disable()
|
|
|
|
{
|
|
|
|
IsEnabled = false;
|
|
|
|
if (targetObject != null)
|
|
|
|
{
|
|
|
|
Destroy(targetObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void LightToggle() {
|
|
|
|
this.targetingLight.gameObject.SetActive(!this.targetingLight.gameObject.activeSelf);
|
|
|
|
}
|
|
|
|
public bool IsLightOn { get { return this.targetingLight.gameObject.activeSelf; } }
|
|
|
|
|
|
|
|
}
|