- Home /
Raycast not detecting distance of my GameObject
I have 2 Models both have Box Colliders and Rigid Bodies attached. Both have the detection script as mentioned below.
1st model is detecting the 2nd model object if comes infront of it but the other one is not detecting the 1st object :( Please Help
Below is the code for detecting object through raycast.
function FixedUpdate () {
var distance : int = 5;
if(Physics.Raycast(transform.position, Vector3.forward , hit , distance))
{
if(hit.collider.tag == "Car")
{
Debug.Log("There is something infront of object");
}
}
}
Are both models looking for a hit.collider.tag named "Car"?
OOoh I see I have observed that as my Objects are 3DS $$anonymous$$AX $$anonymous$$odels and their pivot points are at the bottom not in the center and the Ray is Casted from the bottom of the object because of the pivot points that is why I have to change the y positions up and down to make perfect raycasting of 2 objects.
I think I have to set the Ray casting points in the middle of object.
How I came to know about this ?
I used the Prmitive shape CUBE for this purpose and Cube casts any Ray from the middle of itself.
Does anyone know how can I set the Pivot points of object to its center ?
Answer by Abdullah Sattar · Sep 16, 2013 at 07:36 AM
Yes and both of them are assigned this tag.. Just now I found something that Both my cars were having Box Colliders and by looks both colliders were infront of each other and on runtime I just slightly reduced the Y position of one Box Collider on 1st car and it started detecting the 2nd car and 2nd car also started detecting the 1st one.
That was annoying for me because they were just at the right positions but were not detecting each other untill I check and set the positions of the colliders :(
Your answer
![](https://koobas.hobune.stream/wayback/20220613114243im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make sure that the raycast points in the right direction? 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Raycast Pause Between Hits? 0 Answers
Changing position of a RayCast 1 Answer
Convert c# to Js? 1 Answer