Hi there.
I have a problem with Instant some object and check the forward and behind the object with raycast, if there is no object then set the position else, change the position while no other object in the range of this object.
But it’s not working! raycast can not hit instant objects but hit the scene environment object!
Here is my code:
The BasicPosition is a Vector3(2.5f, 0, 0) . if the object should be in the second lane the BasicPos will be 2.5f * 2
The problem is the Z position, sometimes two objects place in each other!
When do I call SetPosition function?
In the start function, I call the Instantiate function and the call the SetPosition :
the raycast was specified to consider objects by layering
You can use layers and a layermask to isolate the colliders you are interested in finding.
ANOTHER way is to avoid Raycast completely and keep your own list of objects you wish to avoid, then iterate that list each time and make sure you have enough room. This is the most reliable method because it is entirely under your control, which means you can trivially debug it by listing the coordinates of the objects you are avoiding, and reasoning about why issues arise.
Thank Kurt-Dekker.
I still have the problem but I think the problem is about unity instant all the objects in one frame and that’s because the rays do not hit any objects because no object (instant obj) is not in the scene.
I have to send raycast after instant completely.
Don’t guess about problems this way as it is often not productive.
When you Instantiate() something that can be raycast-hit, it CAN BE HIT IMMEDIATELY if you correctly raycast, even in the same frame. Otherwise the documentation would certainly note otherwise.
To prove this, run the scene in the enclosed unitypackage.
Please review the list of OTHER reasons I posted above for why your raycast might not be firing.
Hey Kurt-Dekker. Thanks for the reply.
I change my algorithm because I want to do it like object pooling but I update your c# file, and now you can understand what was my problem.
you work with one sphere, see what happens if you have more than one.
Press the Space key after getting the first notice of sending ray. (At the start it doesn’t hit anything but after a frame raycast hit something named sphere)
when I write the Debug.Log(MaxY); all of them have the same values. It’s like unity Instantiate in the frame one and next lines (setpositions) do in the next frames!
In my opinion, the InstantButton function should run completely and then go to the next command (next for loop i++).
What is wrong?