2023-04-03 17:02:11 +02:00
|
|
|
using UnityEngine;
|
2023-05-29 06:16:05 +02:00
|
|
|
using UnityEngine.VFX;
|
2023-04-03 17:02:11 +02:00
|
|
|
|
|
|
|
public class PistolComponent : MonoBehaviour
|
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
public enum AimMode
|
|
|
|
{
|
|
|
|
GUN,
|
|
|
|
MODIFIED,
|
|
|
|
CAMERA
|
|
|
|
}
|
2023-05-29 06:16:05 +02:00
|
|
|
|
|
|
|
public AimMode aimMode = AimMode.CAMERA;
|
2023-06-01 17:03:48 +02:00
|
|
|
|
|
|
|
[SerializeField] private Light targetingLight;
|
|
|
|
|
|
|
|
[SerializeField] private GameObject targetObjectPrefab;
|
|
|
|
|
|
|
|
[SerializeField] public GameObject projectilePrefab;
|
|
|
|
|
|
|
|
[SerializeField] public string projectileName;
|
|
|
|
|
|
|
|
[SerializeField] private Transform bulletSpawnPoint;
|
|
|
|
|
|
|
|
[SerializeField] private float firePower = 20f;
|
|
|
|
|
|
|
|
[SerializeField] private float maxProjectileDuration = 5f;
|
|
|
|
|
|
|
|
[SerializeField] private float maxTargetObjDistance = 15f;
|
|
|
|
|
|
|
|
[SerializeField] private VisualEffect shootEffect;
|
|
|
|
|
|
|
|
[SerializeField] private Light shootLight;
|
|
|
|
|
|
|
|
[SerializeField] private float shootLightDuration = 0.1f;
|
|
|
|
|
|
|
|
[SerializeField] private LayerMask ignoreLayers;
|
|
|
|
|
|
|
|
private bool hasCloseTarget;
|
|
|
|
|
|
|
|
|
|
|
|
private bool IsEnabled;
|
|
|
|
|
|
|
|
private GameObject targetObject;
|
|
|
|
private float timeSinceLightDuration;
|
|
|
|
public bool IsLightOn => targetingLight.gameObject.activeSelf;
|
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
|
2023-06-01 17:03:48 +02:00
|
|
|
private void Start()
|
2023-04-03 17:02:11 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
2023-06-01 17:03:48 +02:00
|
|
|
private void Update()
|
2023-04-03 17:02:11 +02:00
|
|
|
{
|
2023-05-29 06:16:05 +02:00
|
|
|
timeSinceLightDuration += Time.deltaTime;
|
2023-04-10 04:30:54 +02:00
|
|
|
}
|
|
|
|
|
2023-04-03 17:02:11 +02:00
|
|
|
private void FixedUpdate()
|
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
if (shootLight.gameObject.activeSelf && timeSinceLightDuration > shootLightDuration)
|
2023-05-29 06:16:05 +02:00
|
|
|
shootLight.gameObject.SetActive(false);
|
2023-06-01 20:25:46 +02:00
|
|
|
if (aimMode == AimMode.CAMERA) targetObject.gameObject.transform.position = Player.PlayerAim.active.targetPosition;
|
2023-06-01 17:03:48 +02:00
|
|
|
if (IsEnabled && aimMode != AimMode.CAMERA)
|
2023-04-03 17:02:11 +02:00
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
var ray = new Ray(transform.position, transform.up);
|
2023-04-03 17:02:11 +02:00
|
|
|
RaycastHit hit;
|
2023-06-01 17:03:48 +02:00
|
|
|
if (Physics.Raycast(ray, out hit, 50, ignoreLayers))
|
2023-04-03 17:02:11 +02:00
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
var hitDist = Vector3.Distance(hit.point, transform.position);
|
2023-05-17 06:15:44 +02:00
|
|
|
if (hitDist < maxTargetObjDistance)
|
|
|
|
{
|
|
|
|
targetObject.gameObject.transform.position = hit.point;
|
2023-06-01 17:03:48 +02:00
|
|
|
|
|
|
|
targetObject.gameObject.GetComponent<MeshRenderer>().materials[0]
|
|
|
|
.SetColor("_EmissiveColor", new Color(255, 0, 0));
|
2023-05-17 06:15:44 +02:00
|
|
|
//Track if we have a close target
|
2023-06-01 17:03:48 +02:00
|
|
|
hasCloseTarget = true;
|
2023-05-17 06:15:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
targetObject.gameObject.transform.position =
|
|
|
|
transform.position + ray.direction * maxTargetObjDistance;
|
|
|
|
|
|
|
|
targetObject.gameObject.GetComponent<MeshRenderer>().materials[0]
|
|
|
|
.SetColor("_EmissiveColor", new Color(255, 255, 255));
|
2023-05-17 06:15:44 +02:00
|
|
|
//Track if we have a close target
|
|
|
|
hasCloseTarget = false;
|
|
|
|
}
|
|
|
|
//float drop = CalculateDrop(this.bulletSpawnPoint.position, hit.point, this.transform.up * this.firePower);
|
|
|
|
//print(drop);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
targetObject.gameObject.transform.position = transform.position + ray.direction * maxTargetObjDistance;
|
2023-05-17 06:15:44 +02:00
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
targetObject.gameObject.GetComponent<MeshRenderer>().materials[0]
|
|
|
|
.SetColor("_EmissiveColor", new Color(255, 255, 255));
|
2023-05-17 06:15:44 +02:00
|
|
|
hasCloseTarget = false;
|
2023-04-03 17:02:11 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private float CalculateDrop(Vector3 origin, Vector3 destination, Vector3 force)
|
|
|
|
{
|
|
|
|
// Calculate the initial velocity required to reach the destination.
|
|
|
|
var displacement = destination - origin;
|
|
|
|
var time = Mathf.Sqrt(2f * displacement.magnitude / Physics.gravity.magnitude);
|
|
|
|
var velocity = (displacement - 0.5f * Physics.gravity * time * time) / time + force;
|
|
|
|
|
|
|
|
// Calculate the height the object will reach during its flight.
|
|
|
|
var maxHeight = origin.y + velocity.y * time - 0.5f * Physics.gravity.y * time * time;
|
|
|
|
|
|
|
|
// Calculate the distance the object will drop during its flight.
|
|
|
|
var dropDistance = maxHeight - destination.y;
|
2023-04-04 01:23:20 +02:00
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
return dropDistance;
|
2023-04-04 01:23:20 +02:00
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-04-04 01:23:20 +02:00
|
|
|
public void Fire()
|
2023-05-17 06:15:44 +02:00
|
|
|
{
|
|
|
|
Fire(!hasCloseTarget);
|
|
|
|
}
|
2023-05-29 06:16:05 +02:00
|
|
|
|
2023-05-17 06:15:44 +02:00
|
|
|
public void Fire(bool offsetWithTargetBall)
|
2023-04-04 01:23:20 +02:00
|
|
|
{
|
2023-05-29 06:16:05 +02:00
|
|
|
shootLightDuration = 0;
|
|
|
|
shootLight.gameObject.SetActive(true);
|
2023-06-01 17:03:48 +02:00
|
|
|
var projectile = Instantiate(projectilePrefab, bulletSpawnPoint);
|
2023-04-04 01:23:20 +02:00
|
|
|
projectile.transform.localPosition = Vector3.zero;
|
|
|
|
projectile.transform.localEulerAngles = Vector3.zero;
|
|
|
|
projectile.transform.localScale = Vector3.one;
|
2023-06-01 17:03:48 +02:00
|
|
|
var pRigid = projectile.GetComponent<Rigidbody>();
|
2023-05-17 06:15:44 +02:00
|
|
|
|
|
|
|
/*Modified targeting system
|
|
|
|
1. Since aim direction is vector from camera to ball (where player thinks its gonna go), raycast forward there, till hit. If no hit,
|
|
|
|
then set target distance to ~50.
|
|
|
|
2. Modify launch vector apply modified force
|
|
|
|
*/
|
2023-06-01 17:03:48 +02:00
|
|
|
var launchVector = pRigid.transform.up * firePower;
|
2023-05-17 06:15:44 +02:00
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
if (offsetWithTargetBall || aimMode == AimMode.MODIFIED)
|
2023-05-17 06:15:44 +02:00
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
var ballCamVector = targetObject.transform.position -
|
2023-06-01 20:25:46 +02:00
|
|
|
GetComponentInParent<Player.PlayerMovementController>().cam.transform.position;
|
2023-06-01 17:03:48 +02:00
|
|
|
var r = new Ray();
|
2023-05-17 06:15:44 +02:00
|
|
|
r.origin = targetObject.transform.position;
|
|
|
|
r.direction = ballCamVector.normalized;
|
|
|
|
RaycastHit hit;
|
2023-06-01 17:03:48 +02:00
|
|
|
if (Physics.Raycast(r, out hit, ignoreLayers))
|
2023-05-17 06:15:44 +02:00
|
|
|
{
|
|
|
|
launchVector = (hit.point - pRigid.transform.position).normalized;
|
2023-06-01 17:03:48 +02:00
|
|
|
launchVector *= firePower;
|
2023-05-17 06:15:44 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
else if (aimMode == AimMode.CAMERA)
|
2023-05-29 06:16:05 +02:00
|
|
|
{
|
2023-06-01 20:25:46 +02:00
|
|
|
var target = Player.PlayerAim.active.targetPosition;
|
2023-06-01 17:03:48 +02:00
|
|
|
|
|
|
|
var lv = target - pRigid.transform.position;
|
2023-05-29 06:16:05 +02:00
|
|
|
launchVector = lv.normalized;
|
2023-06-01 17:03:48 +02:00
|
|
|
launchVector *= firePower;
|
2023-05-29 06:16:05 +02:00
|
|
|
}
|
2023-05-17 06:15:44 +02:00
|
|
|
|
|
|
|
pRigid.AddForce(launchVector, ForceMode.Impulse);
|
2023-04-04 01:23:20 +02:00
|
|
|
projectile.transform.parent = null;
|
2023-05-29 06:16:05 +02:00
|
|
|
shootEffect.Play();
|
2023-04-03 17:02:11 +02:00
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-04-03 17:02:11 +02:00
|
|
|
public void Enable()
|
|
|
|
{
|
|
|
|
IsEnabled = true;
|
2023-06-01 17:03:48 +02:00
|
|
|
if (targetObject == null) targetObject = Instantiate(targetObjectPrefab);
|
2023-04-03 17:02:11 +02:00
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-04-03 17:02:11 +02:00
|
|
|
public void Disable()
|
|
|
|
{
|
|
|
|
IsEnabled = false;
|
2023-06-01 17:03:48 +02:00
|
|
|
if (targetObject != null) Destroy(targetObject);
|
2023-04-03 17:02:11 +02:00
|
|
|
}
|
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
|
|
|
|
public void LightToggle()
|
|
|
|
{
|
|
|
|
targetingLight.gameObject.SetActive(!targetingLight.gameObject.activeSelf);
|
2023-04-03 17:02:11 +02:00
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
}
|