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

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.

Related

Specify walkable area in Unity 2D

I want to limit the area where my player can walk. In this case only on red segments.
And I want to use 2D sprites as background images not 3D models. But my main player would be in 3D
Can I use NavMesh here? Or should it be done with Tilemap? Or should I place some 3D objects above it?
here is another scene that I would like to understand how to build
You can try adding invisible colliders attached to an empty gameobject to prevent your character from walking into certain areas.

ARCore collider on generated planes

I'm just playing around with AR core and want to have an object flying around the room and able to land. I'm unsure about how to add colliders to the planes that ARcore is generating based on the visuals on the room. Would this involve instantiating box colliders on the planes somehow?
The ARCore SDK's trackable planes are essentially identified flat surfaces such as the ground or a tabletop.
You can ask the SDK for a list of points for each trackable plane's boundary polygon (retrieved in clockwise order) and create a mesh from those points via triangulation. With the mesh ready, create a GameObject and add a MeshCollider component that references it.
I've created a free Unity plugin that does exactly this. Feel free to use it: https://github.com/jonas-johansson/ARCoreUtils.
I hope that helps!
Assuming you are doing this in Unity, you may want to use a Mesh Collider on the surface instead of a Box Collider.
I'm not sure about ARCore in Unity specifically, but in other AR frameworks it works something like in the attached screen shot, so I imagine it would be similar.
Adding a rigid body to the object causes gravity to be applied set gravity to 0 on the object rigid bidy. That will stop it falling away

How to make pseudo 3D or 2.5D effect?

After using Unity for over a year now for creating 3D games I'd like to do my first pseudo 3D or 2.5D game (or whatever it's called). What I mean is games like Clash of Clans or Boom Beach where objects are really 2D images rendered to give 3D feeling. Because I don't even know the correct word for that type of games it's really hard to get started. What I can think of is giving Camera rotation of (45, -45, 0) so that it looks down at the ground from the upper right corner and then creating empty gameObjects with Sprite Renderers and setting their rotation to (22.5, -45, 0) so that they face the camera. I don't know if that's how it's really done so could somebody give me a link to some neat tutorial or something because I'm stuck.
The keyword you are looking for is isometric projection.
You can use Unity to create that effect with 3D graphics. You need to set the camera rotation to (45, -45, 0) and set the Camera Projection to Orthographic.
If you want to only use 2D graphics then the graphics are made in such a way that they are drawn in an angle that it looks like you are looking at them in 45 degrees.
If you search for isometric unity then you should find a ton of tutorials for Unity.

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

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.

Can a Polygon Collider 2D work with a Mesh Collider in Unity?

EDIT: This is now a rather simple question. I have a 2D sprite that really needs the precision of a polygonal hitbox. The 2D, tile-based world around it uses a tile Mesh for efficiency reasons, and thus has a Mesh Collider.
Before, the tiles in the world were each GameObjects with Box Colliders and Rigidbody 2D's, and the ship and the tiles collided just fine. Now that I am using a Mesh Collider, however, they cannot collide. (I have read that this is because one is 2D and one is 3D.) So what should I do to get collisions (preferably with rigidbody physics) between a polygonal ship and a 2D tile mesh? [end edit]
In a 2D, tile-based, procedurally-generated, chunk-based exploration game (in Unity 4.5), I have a player ship which uses a Rigidbody 2D and a Polygon Collider 2D for collision detection.
This worked fine back when I used a Rigidbody 2D / Box Collider 2D for world tiles. However, this is horribly slow, so I replaced the discrete blocks with a tile mesh, using a Mesh Collider and other associated paraphernalia.
The problem is: I simply cannot get collision detection to work. I have tiles on the x-y plane, and the collision mesh (I can see it in the Scene View, so I know it works) consists of four rectangles perpendicular to the tile. (If you can't visualize this, I don't blame you. See here.)
What have I looked at so far? Well, I verified that the (2D) ship actually passes through the collision boxes in the Scene View. Also, neither of the colliders "Is Trigger".Since there seems to be no official documentation on how to actually use meshes (is there? Where?), I can't find out whether Mesh Colliders and Polygon Colliders actually can interact. Because one is 2D and one is 3D, does this not work? If so, then what should I do instead? I tried using a Box Collider [3D] for the ship, but this didn't work either. I could have potentially made a mistake here, though.
Am I supposed to handle the collision manually (with the OnCollisionEntered [or something] method)? Before, the rigidbody2D objects handled everything automatically. Otherwise, is there any other possible reason the collision might not work?
Well, I'm quite disappointed there seems to be no built-in way to do this in Unity. My solution was to attach a GameObject to the player that would read the block data from the world and create (pooled, of course) real but invisible "collider blocks" with Box Colliders 2D in a small area around the player, such that the player could collide with blocks near them. It works great, and I also implemented an algorithm to spawn rectangular collider blocks over groups of blocks; this eliminates the "ghost pixel" bug in the 2D physics engine.
Uni2D plugin (https://www.assetstore.unity3d.com/en/#!/content/3826) automatically creates 3d colliders (as a group of mesh colliders) from any 2 texture with transparency. A bit expensive but works.