Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug [Problem] Combine line renderers into a single mesh and a single collider (All vertices are 0, 0, 0)

Discussion in 'Scripting' started by AylanJ123, Mar 31, 2023.

  1. AylanJ123

    AylanJ123

    Joined:
    Oct 27, 2019
    Posts:
    4
    In the game I'm currently working on, the player draws the weapon blade to fight with. It's drawn on a "Physical canvas" in the world. The drawing system currently works with LineRenderers that record the mouse positions (Optimized to reduce the amount of points) and it works just fine. The player can make more than one "Stroke" and each one is one LineRenderer.

    Now, I need to make collisions for the blade and they need to be quite similar to the actual drawing, I was thinking about using a mesh collider baking the line renderers and combining them. I don't know if it's actually possible to bake and combine meshes at runtime, I hope so. If it is, then I'm finding a problem. I'm trying to render the mesh and it's just non-existing and if I try to use it on collisions it says: "Combined mesh" mesh must have at least three distinct vertices to be a valid collision mesh.

    I printed all the vertices and collapsed the logs, all of them are (0, 0, 0) so there is something wrong with it. Here is the code:

    Code (CSharp):
    1. CombineInstance[] meshCombiner = new CombineInstance[blade.childCount];
    2. for (int i = 0; i < blade.childCount; i++) {
    3.    Mesh mesh = new();
    4.    mesh.name = i.ToString();
    5.    blade.GetChild(i).GetComponent < LineRenderer > ().BakeMesh(mesh, mainCam, true);
    6.    meshCombiner[i].mesh = mesh;
    7. }
    8. Mesh combined = new();
    9. combined.CombineMeshes(meshCombiner, false, true);
    10. combined.name = "Combined mesh";
    11. for (int i = 0; i < combined.vertices.Length; i++) Debug.Log(combined.vertices[i]); //Al are equal to Vector3.zero
    12. blade.GetComponent < MeshCollider > ().sharedMesh = combined;
    13. blade.GetComponent < MeshFilter > ().sharedMesh = combined;
    Thanks before hand!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    When in doubt, print it out!(tm)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  3. AylanJ123

    AylanJ123

    Joined:
    Oct 27, 2019
    Posts:
    4
    Welp, this seriously looks like a copy paste for helping newbies on Unity. Anyway, thanks for the advices despite I already did most of them here. The info you gave me didn't help too much tho. The only error I'm getting makes sense because my combined mesh just has all vertices on (0, 0, 0) so it's just not combining properly, I don't really know why.

    Actually, if I use any of the line renderers mesh, it actually works! The problem comes when combining, it just refuses to combine properly but each line renderer is baking how it's supposed to bake.
     
  4. AylanJ123

    AylanJ123

    Joined:
    Oct 27, 2019
    Posts:
    4
    Well, for anyone that stumbles on here. If I mark the third argument of CombineMeshes false, it combines properly and if I mark the third of LineRender.Bake false too then it will appear just where the line was drawn.

    Sadly I'll just strip the whole idea because I need the blade to be dynamic but dynamic objects MUST have convex mesh colliders so it is basically a box collider but with worse performance. If I want to allow objects to pass through, for example, a drawn circle, it must be non-convex and kinematic.

    I'll just manage to use some sort of capsules or box colliders and optimize it to not have too many on the drawing. It won't be exact but definitely closer than using a single box that surrounds the whole drawing.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    No, it is a copy/paste that I send when a poster has not yet done the basic work to answer these questions:

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)