- Home /
Unity 4.3, generate 2d mesh
Hello everyone.
For a top down 2D simulation I'm trying to create characters with a displayed FOV cone. I've thought up the math and everything, but the problem I'm facing is implementing this in the 2D mode.
It would be great to create a filled version of the meshCollider2d as a meshfilter2d (which doesn't exist). I have 2 reasons for wanting to do this in the 2D mode:
I want to use 2D line casting against 2D colliders on sprites.
I want to display the dynamic FOV in between the floor and whatever is above it as a semi-transparent overlay.
I've been googling for over an hour trying to find a matching solution, but nothing turns up for the new 2D mode.
Is this possible or is this outside the bounds of what the 2d mode made for?
I am not sure I understand your goal here. A mesh filter has a mesh it is irrelevant to the filter if it is 2D or 3D. I think I am misunderstanding your objective.
Sorry about the use of meshfilter and meshcollider.
Let me rephrase. I want to create a dynamic mesh (from code). A mesh requires a mesh renderer which uses the Z-axis for depth display (3D) A sprite uses a quad wich uses a depth parameter in the inspector for depth display (2D) What I'm trying to achieve is displaying a mesh in between 2 sprites.
Example: 0: floor sprite 1: FOV mesh 2: table sprite
FOV mesh will display above the table. How to set a 2D depth parameter to it?
Second point is... Is it bad practice to place a 2D polygon collider and a $$anonymous$$esh rendered (3D) on the same object?
Answer by Adamcbrz · Mar 18, 2014 at 12:52 PM
All of this is rendered down to opengl so unless they are creating 3d meshes either way. So unity is doing some more advanced stuff under the hood to handle the depth. If you want to interlace 3d and 2d you need to set all the sprite depth's to zero and use z-position for depth instead. This doesn't effect collision because 2D collision ignores z-position. Second question: yes you can use any of the 2d colliders with a 3d mesh renderer.
Answer by Smots · Mar 19, 2014 at 10:28 PM
Thanks for the information Adamcbrz. I did find another way to fix the depth issue. A youtube video on unity 2d displayed a sprite based game with a 3d mechanical arm/crane on the background with a parallax background behind that, so I did decide to google some more, found this after another few hours: http://forum.unity3d.com/threads/212006-Drawing-order-of-Meshes-and-Sprites
In short, all I needed to do was place this in the Start() function
renderer.sortingOrder = 1;
I did encounter a problem when updating the MeshFilter and the PolygonCollider2D in the same run (Tried in both Update and FixedUpdate). The MeshRenderer started to flicker. Disabling the PolygonCollider2D by unchecking it fixed the flickering. Ps, I noticed the PolygonCollider2D flickering in the scene view.
Am I right to assume then that the PolygonCollider2D and the MeshRenderer don't mix?
I used the following at the end of my MeshFilter update:
GetComponent(MeshFilter).mesh = mesh;
And at the end of my PolygonCollider2D update:
var polyColl2D : PolygonCollider2D = gameObject.GetComponent("PolygonCollider2D");
polyColl2D.SetPath(0, points);
Your answer
Follow this Question
Related Questions
I can't get my objects to collide 1 Answer
gameObject with 2 colliders with different layerMasks ? 1 Answer
Joint2D Tutorials 1 Answer