This repository has been archived on 2023-09-13. You can view files and clone it, but cannot push or open issues or pull requests.
station_obscurum_unity/Assets/Scripts/Pistol/PistolComponent.cs

89 lines
2.2 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]
private GameObject projectilePrefab;
[SerializeField]
private Transform bulletSpawnPoint;
[SerializeField]
private float firePower = 20f;
[SerializeField]
private float maxProjectileDuration = 5f;
//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 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;
}
}
}
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;
}
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; } }
}