I’m hoping to create a filled circle around the character when they’re clicked on to show movement range (Think Fire Emblem), but I need the circle to reshape itself dynamically if it overlaps with terrain. If, say, it runs into a wall then I need the circle to cut off at the terrain and not go behind it. I’ve tried looking up how to do this, but I have absolutely no idea where I should begin. It doesn’t look like a LineRenderer component would work because it only displays the outline (as best as I can tell) and I don’t want to use a shader because shaders are completely beyond me.
You can use a LineRenderer and fabricate the points yourself, and then in order to make it visible even on uneven terrain, use two cameras and layers so that one camera sees only the line renderer’s layer, and the other camera sees everything else, and layer the two cameras on top of each other. There’s some tutorials for that sorta thing and it can be fiddly to get right at first, but it’s a pretty simple way to handle this sort of thing.
If you prefer to make the geometry yourself, I have a MakeGeo package that can show you basic procedural geometry, including making a circle of arbitrary size and orientation. MakeGeo is presently hosted at these locations:
I think you misunderstand. I want it so that the circle can’t so much as go through certain terrain. I don’t want the circle to climb over the wall. I want it to stop at the wall.
Here is one approach: in order to produce the boundary points, raycast out from the player in all directions and when it hits something you want the circle to NOT go through, stop the point at that raycast intersection. Otherwise just use the full radius for that point on the circle.
Again, I’m not asking how to detect when the circle should be clipped. I’m asking how to do the actual clipping of the circle.
You don’t do the clipping of the circle.
You create polygon that is based on circle shape.
And you create it with a raycasts.
Optionally you could do something that resembles shadow casting of point light but only on particular plane.
This is not easy technique that will involve using depth buffer generated from origin point of your circle. It will be a screen space shader.
It is probably not worth the effort.
Pseudocode to make the list of circumference points:
for each pie-shaped wedge of circle:
raycast outward from the center for the radius of the circle
if you hit something, use that point for that circle wedge
if you don't hit something, use the full radius for that circle wedge
Now fabricate the circle with that list of generated points.
That will get you 99% of the way there. You may see slight gaps at the point of contact with the wall, but that can be addressed by:
- using more wedges and more raycasts
- interpolating recursively between hit and no-hit raycasts
- always going a little bit beyond the hit point, but never beyond the full radius of the circle
Wait so raycasts actually have a function that allows you to literally draw them on the screen? Because that’s what it sounds like you two are saying and, if that’s true, then I had no idea and would like to know how to do it. I remember there was an option to do that for debugging, but I thought it could only be used for debugging.
No, you are going to raycast to determine the perimeter points that make up your circle.
If your raycasts hit nothing, your circle is full radius, round.
If any of the raycasts hit something, then the point where they hit constitutes the points for that wedge of the circle you make. In the case of a wall it would cause a flat spot on one side.
You would be remaking the circle mesh (you can just modify the Vector3 points in-place) each time the player moved, or even every frame for simplicity.
And the idea from there would be to use those points in a line renderer, right? Because at that point the problem becomes how to fill in the circle since I know I can’t use a line renderer for that.
You can use the points however you like! Line Render perhaps for the border, or you can refer to my first post and there is a handy code snippet in there to produce a circle at runtime. You could hack that up to accept points from your list instead of synthesizing them the way I do.
The script I wrote is in the MakeGeo project as MakeUVCircle.cs and you can even go to github and pluck that one file out and look at how a filled circle is created as procedural geometry.
MakeGeo is presently hosted at these locations:
https://bitbucket.org/kurtdekker/makegeo
This is the actual one pinpointed relevant file:
There are other simpler things in there too, since the circle one supports arbitrary axis orientation, multi-sides, etc., so it’s a bit involved to see the nugget of useful code.
I’m not exactly the best coder in the world, so do you think it’s possible to explain how it works?
I think there is a fine line between asking for help and letting others do the work entirely. I think you do the latter. Procedural mesh generation is a complicated topic. So either you could try to learn and improve your coding skills or you drop your wishes and do something which you are capable of. Just try to get other people doing the work for you is not only lame but also embarrassing. Kurt-Dekker has provided you a good explanation and even some open source code to base your efforts on. Just go ahead and try it and ask if you have specific problems or issues. Just saying “I’m lazy so someone else should do it” will not work (or at least it should not work) in this forum.
It’s noones fault here but yours that you are not “the best coder in the world”. The forum is here to provide help in form of hints, discussion and knowledge. Not providing copy and paste solutions for free. If all else fails find someone who you can pay for doing it. But behavior like yours always makes me wonder what people ecpect from a forum nowadays.
If I can’t really even tell that’s what it is then I can’t very well look up information about it now can I? Maybe don’t be an arrogant jackass. All he really said was “Here’s something I made that does what you want”. That doesn’t help me if I don’t even know what it’s doing in the first place.
There is no time like the present to start learning, and there are bazillions (almost literally) of tutorials online with more being added every day!
As @exiguous pointed out, procedural mesh generation is a hard topic, so you might want to back up and try something that is within your reach initially, get some early success in order to give yourself that “Hey, look what I just did!!!” feeling, and from there begin expanding your skillset.
