阶段性完成
This commit is contained in:
@@ -1,21 +1,24 @@
|
||||
using System;
|
||||
using Cielonos.MainGame.Characters;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cielonos.MainGame
|
||||
{
|
||||
public class RaycastSubmodule : AttackAreaSubmoduleBase
|
||||
{
|
||||
public bool isDynamicDirection;
|
||||
public bool isDynamicRayLength;
|
||||
public Vector3 direction;
|
||||
public bool isDynamicDirection => direction == default;
|
||||
public float rayLength;
|
||||
public bool isDynamicRayLength => rayLength < 0;
|
||||
public float rayRadius;
|
||||
public bool isSphereRay => rayRadius > 0;
|
||||
public bool isCapsuleRay => rayRadius > 0;
|
||||
public Action<Collider, Vector3> onHit;
|
||||
|
||||
public RaycastSubmodule(AttackAreaBase owner, Vector3 direction, float rayRadius, float rayLength) : base(owner)
|
||||
{
|
||||
this.isDynamicDirection = direction == default;
|
||||
this.isDynamicRayLength = rayLength < 0;
|
||||
this.direction = direction;
|
||||
this.rayLength = rayLength;
|
||||
this.rayRadius = rayRadius;
|
||||
@@ -28,46 +31,50 @@ namespace Cielonos.MainGame
|
||||
|
||||
public void Update()
|
||||
{
|
||||
Vector3 rayDirection = isDynamicDirection ? owner.topParent.forward : direction;
|
||||
Ray ray = new Ray(owner.transform.position, rayDirection);
|
||||
|
||||
float sphereRadius = Mathf.Max(rayRadius, 0.1f);
|
||||
Collider[] hitColliders = new Collider[8];
|
||||
int size = Physics.OverlapSphereNonAlloc(owner.transform.position, sphereRadius, hitColliders, LayerMask.GetMask("Characters", "Default", "Ground"));
|
||||
if (size >= 1)
|
||||
{
|
||||
Debug.Log("RaycastSubmodule detected colliders: " + size);
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
CharacterBase character = hitColliders[i].GetComponentInParent<CharacterBase>();
|
||||
if (character != null)
|
||||
{
|
||||
onHit?.Invoke(hitColliders[i], hitColliders[i].ClosestPoint(owner.transform.position));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
onHit?.Invoke(hitColliders[0], hitColliders[0].ClosestPoint(owner.transform.position));
|
||||
return;
|
||||
}
|
||||
|
||||
if (owner.moveSm != null && isDynamicRayLength)
|
||||
{
|
||||
rayLength = owner.moveSm.scaledVelocity.magnitude;
|
||||
rayLength = owner.moveSm.scaledVelocity.magnitude * Time.deltaTime;
|
||||
}
|
||||
|
||||
if (isSphereRay)
|
||||
Vector3 rayDirection = isDynamicDirection ? owner.topParent.forward : direction;
|
||||
Ray ray = new Ray(owner.transform.position, rayDirection);
|
||||
|
||||
if (isCapsuleRay)
|
||||
{
|
||||
if (Physics.SphereCast(ray, rayRadius, out RaycastHit hit, rayLength * Time.deltaTime,
|
||||
LayerMask.GetMask("Characters", "Default", "Ground")))
|
||||
float capsuleRadius = Mathf.Max(rayRadius, 0.1f);
|
||||
float capsuleHeight = Mathf.Max(rayLength, 0.1f);
|
||||
Vector3 point0 = owner.transform.position + rayDirection.normalized * capsuleHeight;
|
||||
Vector3 point1 = owner.transform.position - rayDirection.normalized * capsuleHeight;
|
||||
Collider[] hitColliders = new Collider[8];
|
||||
int size = Physics.OverlapCapsuleNonAlloc(point0, point1, capsuleRadius, hitColliders,
|
||||
LayerMask.GetMask("Player", "Enemy", "Default", "FadableEnvironment", "UnfadableEnvironment", "Wall", "Ground"));
|
||||
if (size >= 1)
|
||||
{
|
||||
Debug.Log("RaycastSubmodule detected colliders: " + size);
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
CharacterBase character = hitColliders[i].GetComponentInParent<CharacterBase>();
|
||||
if (character != null)
|
||||
{
|
||||
Debug.Log("RaycastSubmodule hit character: " + character.name);
|
||||
onHit?.Invoke(hitColliders[i], hitColliders[i].ClosestPoint(owner.transform.position));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
onHit?.Invoke(hitColliders[0], hitColliders[0].ClosestPoint(owner.transform.position));
|
||||
return;
|
||||
}
|
||||
/*if (Physics.CapsuleCast(point0, point1, capsuleRadius, rayDirection, out RaycastHit hit, rayLength,
|
||||
LayerMask.GetMask("Player", "Enemy", "Default", "FadableEnvironment", "UnfadableEnvironment", "Wall", "Ground")))
|
||||
{
|
||||
onHit?.Invoke(hit.collider, hit.point);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, rayLength * Time.deltaTime,
|
||||
LayerMask.GetMask("Characters", "Default", "Ground")))
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, rayLength,
|
||||
LayerMask.GetMask("Player", "Enemy", "Default", "FadableEnvironment", "UnfadableEnvironment", "Wall", "Ground")))
|
||||
{
|
||||
onHit?.Invoke(hit.collider, hit.point);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user