Question by
TheCrimsonMoon · May 22, 2018 at 12:07 AM ·
unity 5physicsboxcast
[Help] BoxCast doesn’t recognize object into it !!
Hey guys !!
i’m working on stealth game, which player must pass level without guards seen him.
my issue in monitor script of guard i don’t know which perfect method use for that but i found BoxCast method and i made a script for that, but the problem is when player be inside BoxCast the method Physics.BoxCast doesn’t recognize player into it just if the player on edges of boxes it recognize but if player walk to guard, player will be inside the BoxCast and Physics.BoxCast doesn’t recognize him.
i hope it’s my explanation is clear.
more explanation:
=================
E: Enemy(Guard)
———-: Distance
[ ]: BoxCast
P: Player
=================
(1) when BoxCast reconize the player
E———-[ ]P
(2) when BoxCast doesn’t reconize the player
E———-[ P ]
The script:
public class BoxCaster : MonoBehaviour {
public GameObject currentHitObject;
public Vector3 dimensions;
public float maxDistance;
public LayerMask layerMask;
private Vector3 origin;
private Vector3 direction;
private float currentHitDistance;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update()
{
origin = transform.position;
direction = transform.forward;
RaycastHit hit;
if (Physics.BoxCast(origin, dimensions, direction, out hit, Quaternion.identity, maxDistance, layerMask, QueryTriggerInteraction.UseGlobal))
{
currentHitObject = hit.transform.gameObject;
currentHitDistance = hit.distance;
if(hit.transform.tag == "Player")
{
Debug.Log("Catch player");
}
}
else
{
currentHitDistance = maxDistance;
currentHitObject = null;
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Debug.DrawLine(origin, origin + direction * currentHitDistance);
Gizmos.DrawWireCube(origin + direction * currentHitDistance, dimensions);
}
}
Comment