i very strange issue,
i used a ray to detect if an object is in front of the camera , the ray is going from the camera view port, and the camera moving in the scene, the object that it must detect stay in it’s place…
if the camera moved slowly, the ray detect the object when it in front of it and the code working perfectly, but when it moved in a high speed, the ray sometime detect the object but sometimes dose not !! which is strange …
the camera is a child of a sphere that rotate 360 degree around it’s pivot, and the object is static on its place like the image below…
What happens here at high speed is that the change you make in 1 frame is large enough to skip the object.
In the update function you will calculate the rotation of the camera but when it’s to much you rotation from 1 frame to another is to large and comes out to another position
Perhaps you can addapt your code and calculate the angle between those 2 rays and cast some rays in between. For exmaple when the angle between the last ray and the current ray is larger than 10 degrees, cast a ray at 5 degrees from the first.
how could i do that from the first??
i understand that the large speed may skip the object detection, and i tried to increase the rays numbers like in this code :
var ray=camera.ViewportPointToRay(Vector3(0.01,0.5,0));
var ray2=camera.ViewportPointToRay(Vector3(0.07,0.5,0));
var hit : RaycastHit;
if (Physics.Raycast (ray, hit) || Physics.Raycast (ray2, hit)) {
//Do something
}
but it dosen’t solve the problem too, i cant also make a big distance between the first ray and the second cause i want the event done exactly when the object appear on the camera…
Do you need to detect if the object is visible to the camera or if it is directly in front? If you need to detect visibility, you could probably just add an OnBecameVisible function to your script.
i want to detect the object in a specific places on the camera view,also i have 3 cameras on my scene, and the OnBecameVisible works on all the three cameras, thats mean if the object appear on camera1 and i want camera3 to detect it, the function will also work…
Don’t know if this could help but it is worth the try…
In another thread i read you can change the frequency the physics are calculated. Because raycasting is done by the physics, perhaps increasing this value could also work for you…
is that will lead to side effect on the project??
what is the minimum value that i could put it to the timestep??
and also, is that will cause a bad performace?
Because of the timestep change, the pysics will calculate more often so increase the workload on the processor. It depends on how much the load is… you will have to make the correct choose depening on how many physics you realy need in the game.
If you realy are creating a physics heavy game were almost all of the components use rigidbody then it can be an issue when decreasing the timestep. If you don’t have that much rigidbody’s i guess it will be ok.
Just try and see for youself. See how many FPS you get from one setting and compare it to the other. If you go from 200 to 30 than it’s not good… If you go from 200 to 180 than it’s accepteable i guess.
nothing changed!!
i changed the timestep to 0.01,0.005,0.001 and nothing changed, the object sometime had detected and sometime had not… i donno what else can i do in this situation !!
BTW, the speed of the sphere is 1 period per 1 second, that means the camera rotate 360 per 1 second, is that a high speed that will not let me contain the project on it?
Syntax isn’t quiet good but i guess you know what i mean.
This also means that you will have redundant checks. Every frame you will cast rays in the same direction but that’s good because than you are sure you really cover everything.
thanks for your help but let me try to understand :
you want to rotate the same ray!! how is that ! there’s no rotation for the ray !
but if u mean that u want to make a lot of rays in the same frame that already done ! i write something like that in the above code that i wrote … and the problem still there …
You might be able to use the GeometryUtility class. This enables you to model the camera’s view frustum as a set of planes. You can then test whether an object is visible or not for any camera. It will be inside the frustum if it is visible, outside otherwise.
The advantage of using the for loop to generate the multiple rays is that you can add lots more rays very easily. This may help you figure out what the problem is.
The loop you were shown would generate 20 rays. You could even increase that to 100 rays and see if that always detects your object. If it does, then decrease the number gradually, see what settings work for you. However, if the object is small or the area on the screen is small, this is going to be difficult to make foolproof.
Worth mentioning, the loop is making some assumptions, it goes from -10 to +10 degrees horizontally. There are actually two problems you may need to solve:
The camera rotation effect we’ve been talking about
The area you need to test might not be a single point. In other words, if the object appears along one side of the view area, a ray shot down the middle won’t hit it.
The solution to both of these is to fire more rays, but the trick is picking which rays to fire. Looking 10 degrees to the left might detect the object too soon (if the camera is rotating right-to-left.)
For #1, you might store the camera ray from the last frame, and use a for-loop to generate a series of rays Lerped from that ray to the current camera ray.
For #2, you might solve it with additional rays fired from the sides of the viewing area. Or we might approximate it with the for-loop that was already posted.
These approaches might have to be combined, it totally depends on the size of the objects and how fast they move/rotate.
It would be helpful if you could figure out exactly how the rays are missing the object. One trick is you could draw the rays in the scene using Gizmos to try and see how they are missing the object. Then we might be able to suggest which of these solutions would work best.
You don’t rotate 1 ray but you create different rays, each rotated a bit from the other. This way you dont have to manually add rays but the loop this if for you. In the example i give you, you create every ray 1 degree rotated from the previous.
He starts with -10 degrees from your reglar point of view of the camera to +10 degrees from you point of view.
thank you all for helping me in this issue…
i tried to do what u told me to do by code , but the unity just crashed everytime i play the scene …
the code was like that :
var minDegree = 0.0;
var maxDegree = 0.09;
function Update(){
LaunchRay();
}
function LaunchRay(){
for(degree = minDegree;degree<maxDegree;degree=degree+0.01)
var ray = camera.ViewportPointToRay (Vector3(degree,0.5,0));
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
//Do Something
}
}