170 lines
5.4 KiB
C#
170 lines
5.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
|
|
public class PistolComponent : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private Light targetingLight;
|
|
|
|
|
|
private bool IsEnabled = false;
|
|
|
|
GameObject targetObject;
|
|
|
|
[SerializeField]
|
|
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;
|
|
|
|
|
|
private bool hasCloseTarget = false;
|
|
|
|
//private Dictionary<int,float> projectiles = new Dictionary<int, float>();
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
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;
|
|
}
|
|
private void FixedUpdate()
|
|
{
|
|
|
|
if (this.IsEnabled)
|
|
{
|
|
Ray ray = new Ray(transform.position, transform.up);
|
|
RaycastHit hit;
|
|
if (Physics.Raycast(ray, out hit, 50))
|
|
{
|
|
|
|
float hitDist = Vector3.Distance(hit.point, transform.position);
|
|
if (hitDist < maxTargetObjDistance)
|
|
{
|
|
targetObject.gameObject.transform.position = hit.point;
|
|
|
|
targetObject.gameObject.GetComponent<MeshRenderer>().materials[0].SetColor("_EmissiveColor", new Color(255, 0,0));
|
|
//Track if we have a close target
|
|
hasCloseTarget = true;
|
|
}
|
|
else
|
|
{
|
|
targetObject.gameObject.transform.position = transform.position + (ray.direction * maxTargetObjDistance);
|
|
|
|
targetObject.gameObject.GetComponent<MeshRenderer>().materials[0].SetColor("_EmissiveColor", new Color(255, 255,255));
|
|
//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
|
|
{
|
|
targetObject.gameObject.transform.position = transform.position + (ray.direction * maxTargetObjDistance);
|
|
|
|
targetObject.gameObject.GetComponent<MeshRenderer>().materials[0].SetColor("_EmissiveColor", new Color(255,255,255));
|
|
hasCloseTarget = false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
public void Fire()
|
|
{
|
|
Fire(!hasCloseTarget);
|
|
}
|
|
public void Fire(bool offsetWithTargetBall)
|
|
{
|
|
|
|
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>();
|
|
|
|
/*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
|
|
*/
|
|
Vector3 launchVector = pRigid.transform.up * this.firePower;
|
|
|
|
if(offsetWithTargetBall)
|
|
{
|
|
Vector3 ballCamVector = targetObject.transform.position - this.GetComponentInParent<PlayerMovementController>().cam.transform.position;
|
|
Ray r = new Ray();
|
|
r.origin = targetObject.transform.position;
|
|
r.direction = ballCamVector.normalized;
|
|
RaycastHit hit;
|
|
if (Physics.Raycast(r,out hit))
|
|
{
|
|
launchVector = (hit.point - pRigid.transform.position).normalized;
|
|
launchVector *= this.firePower;
|
|
}
|
|
}
|
|
|
|
pRigid.AddForce(launchVector, ForceMode.Impulse);
|
|
projectile.transform.parent = null;
|
|
|
|
|
|
|
|
}
|
|
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; } }
|
|
|
|
}
|