- Home /
Generate obstacles within boundry
Basically I am working on racing game. For that I have used multiple fixed path images to draw path for car. Following image represent one of the demo of that path.
My target is to generate enemy cars within this type of paths. In this image black portion is my road so within that I have to spawn enemy cars. At present I can't able to figure out how to spawn car within that area because I have other path also with some other patten in those.
I need some help in spawning cars.
I think this is general question and everybody has to face it in ga$$anonymous$$g field. So please friends help me in this. I want some master way to solve this problem.
Answer by KMKxJOEY1 · Aug 15, 2014 at 02:33 PM
even-odd rule algorithm will be helpful if you want to raycast a position. http://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm
Personally, I don't really like raycasting and is hard to implement for a random spawn position; usually it would be used for checking if an already predetermined point is inside of a polygon. Here is what I would do:
Given a line segment between P0 (x0,y0) and P1 (x1,y1), another point P (x,y) has the following relationship to the line segment. Compute (y - y0) (x1 - x0) - (x - x0) (y1 - y0) if it is less than 0 then P is to the right of the line segment, if greater than 0 it is to the left, if equal to 0 then it lies on the line segment. - http://stackoverflow.com/questions/4243042/c-sharp-point-in-polygon A simple google really goes quiet a long way. The code provided at the link should be helpful, but if you are unfamiliar with .NET programming I could convert it into 'unity code' for you.If the polygon is convex then one can consider the polygon as a "path" from the first vertex. A point is on the interior of this polygons if it is always on the same side of all the line segments making up the path.
Your answer
![](https://koobas.hobune.stream/wayback/20220613160636im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Dynamic terrain creation for 2d game 0 Answers
How can I make a missile home to an enemy in arc fashion? (Unity 4.3 2D) 5 Answers
Collision is not detected ??? 0 Answers
I am having spawning problems. 2 Answers