- Home /
RayCast is not triggering another object
I'm attempting to send a ray from a little box, Object A, to another object that has a trigger collider. That other object, Object B, has a script that is basically "OnTriggerEnter" with a debug notification. I know the ray is hitting object B because it tells me it's distance from the object and the layermask can only hit that one object.
I've eliminated so many variables that I really don't know what I could have done wrong. Any help would be greatly appreciated.
Object A
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Object_Physics : MonoBehaviour
{
// Start is called before the first frame update
public int maxDistance = 50;
//The maximum distance at which the object can be detected
public double rayDistanceObj = 0;
//tracks the distance between both objects
void FixedUpdate()
{
int layerMask = 1 << 9;
//Detects only the Mask I choose
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, maxDistance, layerMask, QueryTriggerInteraction.Collide)){
rayDistanceObj = hit.distance;
}
}
}
` Object B
using System.Collections.Generic;
using UnityEngine;
public class SC_RigidbodyMagnet : MonoBehaviour
{
void FixedUpdate()
{
}
void OnTriggerEnter(Collider other)
{
Debug.Log("Object triggered");
}
void OnTriggerExit(Collider other)
{
}
}
Answer by Captain_Pineapple · Jan 30 at 01:35 PM
If i understand your question correctly then you assume that a Raycast triggers the OnTriggerEnter
callback. This is just not the case. OnTriggerEnter
is only called when an actual collision between 2 colliders takes place.
What you want to do is basically modify your code so that:
if(hit.distance < someThreshold)
{
if(hit.transform.CompareTag("someTagOfB"))
hit.Transform.GetComponent<SC_RigidbodyMagnet>().callTrigger();
}
and give your SC_RigidbodyMagnet
script a function called
public void callTrigger()
Thank you! That's a way better way to trigger another object.
Your answer
Follow this Question
Related Questions
How to Find Owner of a Trigger? 1 Answer
Multiple Cars not working 1 Answer
Help With Player Decrease Enemy Health!,Need Help with Character 1 Answer
How can i make only some Raycasts hit triggers 1 Answer
Distribute terrain in zones 3 Answers