updated shoot mechanics

This commit is contained in:
2023-04-03 19:23:20 -04:00
parent 174e912d1f
commit 260ca88656
9 changed files with 1326 additions and 130 deletions

View File

@ -0,0 +1,30 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletComponent : MonoBehaviour
{
[SerializeField]
private float duration = 5f;
private float existed = 0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void FixedUpdate()
{
if(existed >= duration)
{
Destroy(this.gameObject);
}
existed += Time.fixedDeltaTime;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9ea1ee4df8019a143b013f8837aa48d8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -12,6 +12,16 @@ public class PistolComponent : MonoBehaviour
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()
@ -37,9 +47,20 @@ public class PistolComponent : MonoBehaviour
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()

View File

@ -85,62 +85,71 @@ public class PlayerInteractionHandler : MonoBehaviour
}
if(Input.GetButtonDown("Fire1"))
{
if (!IsCarrying)
if (this.GunEnabled)
{
int t_index = 0;
bool pickupFound = false;
if (itemsInRange.Count > 0)
{
while (t_index<itemsInRange.Count&&!itemsInRange[t_index].CanPickup)
{
t_index++;
}
if (t_index != itemsInRange.Count)
{
pickupFound = true;
invent.AddItem(itemsInRange[t_index]);
itemsInRange[0].transform.gameObject.SetActive(false);
itemsInRange.Remove(itemsInRange[t_index]);
}
}
else if (heavyItemsInRange.Count > 0)
{
pickupFound = true;
heavyInvent = heavyItemsInRange[0];
heavyInvent.transform.parent = carryingPos;
heavyInvent.transform.localPosition = Vector3.zero;
heavyInvent.GetComponent<Rigidbody>().isKinematic = true;
heavyItemsInRange.Remove(heavyItemsInRange[0]);
heavyInvent.Disable();
heavyInvent.DisableAll();
}
if (!pickupFound)
{
foreach(InteractableItem item in itemsInRange)
{
if (!item.CanPickup)
{
if(!item.Interact(ref invent, ref heavyInvent))
{
item.Interact();
}
}
}
}
this.pistol.Fire();
}
else
{
heavyInvent.transform.parent = null;
heavyInvent.GetComponent<Rigidbody>().isKinematic = false;
heavyInvent.EnableAll();
heavyInvent = null;
if (!IsCarrying)
{
int t_index = 0;
bool pickupFound = false;
if (itemsInRange.Count > 0)
{
while (t_index < itemsInRange.Count && !itemsInRange[t_index].CanPickup)
{
t_index++;
}
if (t_index != itemsInRange.Count)
{
pickupFound = true;
invent.AddItem(itemsInRange[t_index]);
itemsInRange[0].transform.gameObject.SetActive(false);
itemsInRange.Remove(itemsInRange[t_index]);
}
}
else if (heavyItemsInRange.Count > 0)
{
pickupFound = true;
heavyInvent = heavyItemsInRange[0];
heavyInvent.transform.parent = carryingPos;
heavyInvent.transform.localPosition = Vector3.zero;
heavyInvent.GetComponent<Rigidbody>().isKinematic = true;
heavyItemsInRange.Remove(heavyItemsInRange[0]);
heavyInvent.Disable();
heavyInvent.DisableAll();
}
if (!pickupFound)
{
foreach (InteractableItem item in itemsInRange)
{
if (!item.CanPickup)
{
if (!item.Interact(ref invent, ref heavyInvent))
{
item.Interact();
}
}
}
}
}
else
{
heavyInvent.transform.parent = null;
heavyInvent.GetComponent<Rigidbody>().isKinematic = false;
heavyInvent.EnableAll();
heavyInvent = null;
}
}
}
if (Input.GetButtonDown("Fire2"))
{