Is there a way to recalculate the polygon collider at runtime? and possibly with better accuracy? - unity3d

so I have this game in which I have destructible terrain. my setup is that when terrain is destroyed a part of the terrain texture is made transparent, to act as a crater. after any kind of change takes place, I delete the polygon collider on the texture for the terrain and create a new one in order to recalculate the terrain, but obviously this is very taxing on processing power. is there a way to reset the polygon collider without having to do this?
in addition, the polygon collider is not very accurate and I've been told that pixel perfect collision is not possible in unity, but I hold out hope. is there a way to increase the accuracy at least?
here is an example of my problem:

Collider recalculation is expensive, and there's no way around it in Unity, you can't even do it asynchronously.
It depends on your exact situation, but for a Worms-type game with dynamic terrain destruction, I wouldn't use polygonal colliders/Unity physics engine at all.
I'd create my own pixel-perfect collision system that would rely only on the underlying terrain texture.
You already have the terrain data, checking for collisions could be done with Bresenham's algorithm.
Here's a tutorial: http://gamedevelopment.tutsplus.com/tutorials/coding-destructible-pixel-terrain-how-to-make-everything-explode--gamedev-45

I would recommend setting up cluster-based colliders in this case. How do you calculate the area of texture that becomes transparent? If it has a fixed size, you can create a one big collider, which consists of many smaller (hexagon shaped maybe?) sized colliders, whose size equals to the area of texture made transparent.
After you make the texture transparent, you simply destroy the cluster. Though you would have to make the hex(if this shape you choose) grid for every map.
This would be harder to implement in procedurally-generated maps, but it is still possible, as there are no dynamic colliders at the moment.

Related

Unity: opaque area of image should be a collider, transparent area should not

I'm fairly certain that I search every possible answer for this, so please forgive me if there is an answer somewhere...
I have a bunch (35) images that are procedurally assembled to make a "maze" of sorts. The "player" object is placed in a random position on the screen and I it should avoid the collidable space. The "maze" images are 2D with some opaque areas (white) and transparent areas (blue). In Unity, all of the images have Polygon Colliders that are using Composite... All of this works perfectly, except...
My issue is that Polygon Colliders use outlines or polygons, but everything in between is not a collider. That means that, unless the player hits one of the edges of the collider, it won't register an actual collision.
To better understand this, look at the image. The player will spawn in the opaque area of the image, when I want that area to be a collider.
Btw, not using Composite produces the same issue, where the player object spawns between the polygon edges.
Any idea how to make the entire opaque area a collider?
You can check for a collision within the collider by using the void OnCollisionStay2D(Collision2D collision) method for whichever object that you want to handle the test.
However, if you're trying to use the collider to determine whether an area is safe to spawn in, you might want to check whether the spawnpoint is inside the collider using collider.bounds.Contains(player.transform.position) (or whatever position you'd like to check).
Check the documentation for more info: https://docs.unity3d.com/ScriptReference/Bounds.Contains.html

Recommendations for clipping an entire scene in Unity

I'm looking for ways to clip an entire unity scene to a set of 4 planes. This is for an AR game, where I want to be able to zoom into a terrain, yet still have it only take up a given amount of space on a table (i.e: not extend over the edges of the table).
Thus far I've got clipping working as I want for the terrain and a water effect:
The above shows a much larger terrain being clipped to the size of the table. The other scene objects aren't clipped, since they use unmodifed standard shaders.
Here's a pic showing the terrain clipping in the editor.
You can see the clipping planes around the visible part of the terrain, and that other objects (trees etc) are not clipped and appear off the edge of the table.
The way I've done it involves adding parameters to each shader to define the clipping planes. This means customizing every shader I want to clip, which was fine when I was considering just terrain.
While this works, I'm not sure it's a great approach for hundreds of scene objects. I would need to modify whatever shaders I'm using, and then I'd have to be setting additional shader parameters every update for every object.
Not being an expert in Unity, I'm wondering if there are other approaches that are not "per shader" based that I might investigate?
The end goal is to render a scene within the bounds of some plane.
One easy way would be to use Box Colliders as triggers on each side of your plane. You could then turn off Renderers on objects staying in the trigger with OnTriggerEnter/OnTriggerStay and turn them on with OnTriggerExit.
You can also use Bounds.Contains.

Collider for blender pot model inside Unity

i've made a simple pot model inside blender:
So the idea is that after importing to unity, generating mesh collider, adding ridgin body i would like to be able to put smothing inside the pot. Now it's generating collider but without a "hole" inside, so if i throw smth inside it just bounce off the top. Is there any way to do it simple way? I'd like to avoid making a collider by hand in unity, using cubes and so...
Image overview:
Your collider needs to be concave
It isn't clear from your question how you're "generating mesh collider," but the results are clearly generating a convex collider.
That said, some things to know about mesh colliders (and concave ones even more so): They are very compultationally heavy to calculate, so they should never...
move
scale (especially non-uniformly)
rotate
...at runtime.
Alternatively you can use multiple box colliders in the same orientations as the side-segments of your can (along with one or two for the bottom, depending on how small of an object you'll be dropping inside).
Uncheck the generate collider box from the import settings, add a mesh collider component and check convex.
I would use four or six box colliders for the walls of the pot and another for the floor. You can scale each box collider along each axis, but if you want to rotate a box collider you will need to give it a parent and rotate the parent. Box colliders are very low cost for the physics engine compared to mesh colliders.

Unity3D: terrain object's textures flickering on long distances

I'm making RTS project on Unity3D.
I created terrain with Unity's standard Terrain tool, and added textures of grass, mood etc. on it. Then, for creating "man-made" objects of terrain (roads, sidewalks, road curbs etc) - I'm created this objects in separate assets, and placed on terrain.
And I have one issue with this solution. On moving camera away from the terrain, terrain's texture(e.g. mood) are flickering under roads, sidewalks and other objects. AFAIK, this bug caused by insuficcient accuracy of floating-point coordinates in Unity3D engine (?).
Now, as I concerned, my approach to creating terrain objects is not correct. I must create one mesh with terrain, and all manmade objects in 3D modeling software, and then create UV-map for texturing all of it. Is this approach correct? If "yes", is any special approach for modelling and texturing so large and complex object as terrain?
I had the same issue time ago and I solve it by increasing camera's near plane value. I had many objects on the plane that were flickering when moving the camera and was due to having the near plane value at 0.01. I change it to 0.5 and none of the objects where flickering anymore.
I hope this helps!

How can I make custom 2D polygon in Unity with collision detection?

I am learning Unity and I want to make a little game where the user controls a tank and she can shot with it. My plan is when the missile hit the terrain, it makes a hole in that, like in the old school Worms. My first idea was to make the terrain as a textured polygon and when a collision happens, change the polygon structure. Is it possible to implement this?
You can do a little trick with a 3D mesh. If you are using orthogonal camera the Z axis won't be visible and you will be able to add mesh collider. If you still want to do custom things with that mesh like changing shape there are assets on the store:
http://u3d.as/content/chris-west/mega-fiers/1Qa
Or
When a missle hits terrain change you terrain texture pixels alpha channel to 0
http://docs.unity3d.com/ScriptReference/Texture2D.SetPixels.html
Then use raycasts to check if alpha channel != 0 for moving.