Trying to draw a line to the collider.bounds.max/min. Problem is when i rotate the object the rays are no longer in the correct place. I’m not sure how to make them follow with the rotation of the objects collider. Examples because i’m not great with words.
Images:
Doesn’t seem to work when i try to embed in post? New to this!
Seems to be working until i rotate the objects.
public void OnTriggerEnter(Collider collider)
{
if (collider.tag == "Interactable" || collider.tag == "Player" || collider.tag == "NPC")
{
if (!FieldOfView.Contains(collider))
{
FieldOfView.Add(collider);
}
}
}
public void OnTriggerExit(Collider collider)
{
if (FieldOfView.Contains(collider))
{
FieldOfView.Remove(collider);
}
}
public float TargetDistance(GameObject target)
{
float targetDistance = Vector3.Distance(target.transform.position, transform.position);
return targetDistance;
}
public void RaycastTarget()
{
int layerMask = 1 << 8;
layerMask = ~layerMask;
foreach (Collider collider in FieldOfView)
{
RaycastHit topHit;
RaycastHit leftHit;
RaycastHit rightHit;
RaycastHit bottomHit;
Vector3 topOffset = new Vector3(collider.bounds.center.x, collider.bounds.max.y, collider.bounds.center.z);
Vector3 bottomOffset = new Vector3(collider.bounds.center.x, collider.bounds.min.y, collider.bounds.center.z);
Vector3 leftOffset = new Vector3(collider.bounds.min.x, collider.bounds.center.y, collider.bounds.center.z);
Vector3 rightOffset = new Vector3(collider.bounds.max.x, collider.bounds.center.y, collider.bounds.center.z);
Physics.Raycast(transform.position, topOffset - transform.position, out topHit, Mathf.Infinity, layerMask);
Physics.Raycast(transform.position, bottomOffset - transform.position, out bottomHit, Mathf.Infinity, layerMask);
Physics.Raycast(transform.position, leftOffset - transform.position, out leftHit, Mathf.Infinity, layerMask);
Physics.Raycast(transform.position, rightOffset - transform.position, out rightHit, Mathf.Infinity, layerMask);
if(topHit.collider == collider)
{
Debug.DrawLine(transform.position, topHit.point, Color.green);
}
else
{
Debug.DrawLine(transform.position, topHit.point, Color.red);
}
if (bottomHit.collider == collider)
{
Debug.DrawLine(transform.position, bottomHit.point, Color.green);
}
else
{
Debug.DrawLine(transform.position, bottomHit.point, Color.red);
}
if (leftHit.collider == collider)
{
Debug.DrawLine(transform.position, leftHit.point, Color.green);
}
else
{
Debug.DrawLine(transform.position, leftHit.point, Color.red);
}
if (rightHit.collider == collider)
{
Debug.DrawLine(transform.position, rightHit.point, Color.green);
}
else
{
Debug.DrawLine(transform.position, rightHit.point, Color.red);
}
}
}