- Home /
Raycast cannot tell when moving to a new object
I've been working on a system that uses raycasts to open a series of things, specifically doors, dresser doors, and drawers. The script looks like this:
private void Update()
{
RaycastHit hit;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
int mask = 1 << LayerMask.NameToLayer(excludeLayerName) | layerMaskInteract.value;
if(Physics.Raycast(transform.position, fwd, out hit, rayLength, mask))
{
if (hit.collider.CompareTag(interactableTagRight))
{
if (!doOnce)
{
raycastedObj = hit.collider.gameObject.GetComponent<MyDoorController>();
CrosshairChange(true);
}
isCrosshairActive = true;
doOnce = true;
if (Input.GetKeyDown(openDoorKey))
{
raycastedObj.PlayAnimation();
}
I actually have 3 of these if statements in the update, one for right doors, one for left doors, and one for drawers. I have come into an issue however with the box colliders overlapping preventing the raycast from working properly.
The box colliders DO NOT actually intersect but rather when my raycast moves from one object to another without ever leaving an object with an interactable tag I get one of two results. If the tags are different (left door/right door), I get an error and the animation does not play. If they have the same tag (drawer) it animates the first object my raycast hit. This happens until the raycast stops hitting any item with a tag listed in the script. I have thought about programming something that stops the raycast every 5 frames or so for a frame before bringing it back, but that seemed problematic for processing and the chance for the player to press the button on the perfect frames and have their input eaten, or the problem to persist if they are fast enough.
What are my options here?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Clicking on an Object to Make it the Variable Target 1 Answer
Player Attack Script. I Need Help! 0 Answers