Update
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Invector
|
||||
{
|
||||
[vClassHeader("Check Effect Visibility", "Use it to identify if the object is within the camera frustrum and not hide by obstacles", openClose = false)]
|
||||
public class vCheckEffectVisibility : vMonoBehaviour
|
||||
{
|
||||
public LayerMask layerObstacle;
|
||||
[Tooltip("The point to check if effect is visible")]
|
||||
public Vector3 checkPoint = new Vector3(0, 1.5f, 0);
|
||||
public bool debugMode;
|
||||
public vEffectReceiver.vEffectEvent OnVisible;
|
||||
public vEffectReceiver.vEffectEvent OnNotVisible;
|
||||
|
||||
private Camera mainCamera;
|
||||
|
||||
Vector3 visibilityPoint => transform.TransformPoint(checkPoint);
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
if (debugMode)
|
||||
Gizmos.DrawSphere(visibilityPoint, 0.1f);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
mainCamera = Camera.main;
|
||||
}
|
||||
|
||||
public void CheckEffectIsVisible(vIEffect effect)
|
||||
{
|
||||
if (CheckIfEffectIsVisible(effect))
|
||||
{
|
||||
OnVisible.Invoke(effect);
|
||||
}
|
||||
else
|
||||
{
|
||||
OnNotVisible.Invoke(effect);
|
||||
}
|
||||
}
|
||||
|
||||
bool IsObjectVisible(Camera camera, Vector3 position)
|
||||
{
|
||||
// Convert the world position of the object to viewport coordinates
|
||||
Vector3 viewportPoint = camera.WorldToViewportPoint(position);
|
||||
|
||||
// Check if the viewport coordinates are within the range of (0,0) to (1,1)
|
||||
bool isVisible = viewportPoint.x >= 0f && viewportPoint.x <= 1f &&
|
||||
viewportPoint.y >= 0f && viewportPoint.y <= 1f &&
|
||||
viewportPoint.z >= 0f;
|
||||
return isVisible;
|
||||
}
|
||||
|
||||
bool CheckIfEffectIsVisible(vIEffect effect)
|
||||
{
|
||||
if (mainCamera != null)
|
||||
{
|
||||
// Check if the object is within the camera frustum
|
||||
if (IsObjectVisible(mainCamera, effect.EffectPosition))
|
||||
{
|
||||
// Check if any objects are blocking the view between the camera and the object
|
||||
RaycastHit hitInfo;
|
||||
if (Physics.Linecast(effect.EffectPosition, visibilityPoint, out hitInfo, layerObstacle, QueryTriggerInteraction.Ignore))
|
||||
{
|
||||
if (hitInfo.collider.gameObject != gameObject && hitInfo.distance >= 1f)
|
||||
{
|
||||
if (debugMode) Debug.DrawLine(effect.EffectPosition, hitInfo.point, Color.red, effect.EffectDuration);
|
||||
if (debugMode) Debug.Log("Object is not in view because of a obstacle: " + hitInfo.collider.name, hitInfo.collider.gameObject);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (debugMode)
|
||||
{
|
||||
Debug.DrawLine(effect.EffectPosition, visibilityPoint, Color.green, effect.EffectDuration);
|
||||
}
|
||||
if (debugMode) Debug.Log("Object is in view!");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (debugMode) Debug.Log("Object is in view!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 45bdf49c03d1ef249998e2350db254ff
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
28
Assets/Scripts/Player/vEffect/Scripts/vEffectReceiver.cs
Normal file
28
Assets/Scripts/Player/vEffect/Scripts/vEffectReceiver.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
namespace Invector
|
||||
{
|
||||
[vClassHeader("Effect Receiver", "Use with the EffectSender component to trigger a Effect.")]
|
||||
public class vEffectReceiver : vMonoBehaviour
|
||||
{
|
||||
[System.Serializable]
|
||||
public class vEffectByName
|
||||
{
|
||||
public string effectName;
|
||||
public vEffectEvent onReceiveEffect;
|
||||
}
|
||||
[System.Serializable]
|
||||
public class vEffectEvent : UnityEngine.Events.UnityEvent<vIEffect> { }
|
||||
|
||||
public vEffectEvent onReceiveEffect;
|
||||
|
||||
public vEffectByName[] effectsByName;
|
||||
|
||||
public virtual void OnReceiveEffect<T>(T effect) where T : vIEffect
|
||||
{
|
||||
onReceiveEffect.Invoke(effect);
|
||||
for (int i = 0; i < effectsByName.Length; i++)
|
||||
{
|
||||
if (effectsByName[i].effectName.Equals(effect.EffectName)) effectsByName[i].onReceiveEffect.Invoke(effect);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bddfec62bef2c3145b58848930147e3c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
69
Assets/Scripts/Player/vEffect/Scripts/vEffectSender.cs
Normal file
69
Assets/Scripts/Player/vEffect/Scripts/vEffectSender.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Invector
|
||||
{
|
||||
[vClassHeader("Effect Sender", "Use this component with the vEffectReceiver added on your Player Root to trigger Effects", openClose = false)]
|
||||
public class vEffectSender : vMonoBehaviour
|
||||
{
|
||||
[vHelpBox("Make sure you check where the vEffectReceiver is on your player, root object, parented or inside as children. By default is located inside the Invector Components > vEffects")]
|
||||
|
||||
public vEffectStruct[] effects;
|
||||
[System.Serializable]
|
||||
public class vEffectStruct : vIEffect
|
||||
{
|
||||
public enum GetReceiverMethod
|
||||
{
|
||||
InTarget, InParent, InChildren
|
||||
}
|
||||
public GetReceiverMethod getReceiverMethod = GetReceiverMethod.InChildren;
|
||||
[SerializeField] protected string effectName;
|
||||
[SerializeField] protected float effectDuration;
|
||||
public string EffectName => effectName;
|
||||
|
||||
public float EffectDuration => effectDuration;
|
||||
|
||||
public Vector3 EffectPosition { get; set; }
|
||||
|
||||
public Transform Sender { get; set; }
|
||||
}
|
||||
|
||||
public Transform overrideEffectSender;
|
||||
public virtual void SetOverrideEffectSender(Transform t)
|
||||
{
|
||||
overrideEffectSender = t;
|
||||
}
|
||||
public void Send(GameObject target)
|
||||
{
|
||||
Send(target.transform);
|
||||
}
|
||||
public void Send(Transform target)
|
||||
{
|
||||
for (int i = 0; i < effects.Length; i++)
|
||||
{
|
||||
var effect = effects[i];
|
||||
effect.Sender = overrideEffectSender ? overrideEffectSender : transform;
|
||||
effect.EffectPosition = transform.position;
|
||||
vEffectReceiver receiver = null;
|
||||
switch (effect.getReceiverMethod)
|
||||
{
|
||||
case vEffectStruct.GetReceiverMethod.InTarget:
|
||||
if (target.TryGetComponent(out receiver)) receiver.OnReceiveEffect(effect);
|
||||
break;
|
||||
case vEffectStruct.GetReceiverMethod.InParent:
|
||||
receiver = target.GetComponentInParent<vEffectReceiver>();
|
||||
if (receiver) receiver.OnReceiveEffect(effect);
|
||||
break;
|
||||
case vEffectStruct.GetReceiverMethod.InChildren:
|
||||
receiver = target.GetComponentInChildren<vEffectReceiver>();
|
||||
if (receiver) receiver.OnReceiveEffect(effect);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void Send(Collider target)
|
||||
{
|
||||
Send(target.transform);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Player/vEffect/Scripts/vEffectSender.cs.meta
Normal file
11
Assets/Scripts/Player/vEffect/Scripts/vEffectSender.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 73577af56cf082948b69653ea5a93469
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
12
Assets/Scripts/Player/vEffect/Scripts/vIEffect.cs
Normal file
12
Assets/Scripts/Player/vEffect/Scripts/vIEffect.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Invector
|
||||
{
|
||||
public partial interface vIEffect
|
||||
{
|
||||
string EffectName { get; }
|
||||
float EffectDuration { get; }
|
||||
Vector3 EffectPosition { get; }
|
||||
Transform Sender { get; }
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Player/vEffect/Scripts/vIEffect.cs.meta
Normal file
11
Assets/Scripts/Player/vEffect/Scripts/vIEffect.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 374d39d8e7f22a84297f51ff450fbd76
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user