Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

[SOLVED][CLOSED]Procedurally generated Capsule

Discussion in 'Scripting' started by sacunha, May 27, 2016.

  1. sacunha

    sacunha

    Joined:
    Apr 21, 2016
    Posts:
    148
    Does anyone have a script that creates a procedurally generated capsule, where I can control its height and radius?

    Please do not suggest this http://docs.unity3d.com/ScriptReference/PrimitiveType.Capsule.html because it doesn't allow me to control it's height and radius. (Unless it does allow me and I don't know about it, In that case, please do suggest that and point out how I can control those parameters).
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    only by altering the xyz scale parameters on the transform
     
  3. sacunha

    sacunha

    Joined:
    Apr 21, 2016
    Posts:
    148
    Yes I've already tried that and it stretches out the capsule, and that does not work for me. That is why I requested a procedural capsule. This page has some primitieves but unfortunatly, not the one I need :S
    http://wiki.unity3d.com/index.php/ProceduralPrimitives
     
  4. bart_the_13th

    bart_the_13th

    Joined:
    Jan 16, 2012
    Posts:
    480
    why not create one from 3d software, a capsule with 2 bones for each end. you can change the radius by scaling the bones and change the height by changing the bones local position...
     
    LeftyRighty likes this.
  5. sacunha

    sacunha

    Joined:
    Apr 21, 2016
    Posts:
    148
    Never worked with bones before but sounds interesting. Thanks
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    in unity terms "bones" are just gameobjects (empties I think), the fun stuff is how the SkinnedMeshRenderer manipulate the vertex points in relation to those bones.
     
  7. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
    Just because I like playing around with meshes and stuff in code, see attached script. :D

    Attach to an empty gameObject, it automatically adds the MeshFilter and MeshRenderer components. Right-Click on the script and there will be an option in the drop-down menu "Generate Procedural Capsule". If you change any values in the inspector, right-click script and select this again to refresh.

    TODO:
    Custom Editor to refresh mesh in real time.
    Add calculating tangents ( solution here )
    Save mesh asset ( possible solution here )
     

    Attached Files:

    slowDrag0n, rostok and LeftyRighty like this.
  8. sacunha

    sacunha

    Joined:
    Apr 21, 2016
    Posts:
    148
    That is realy cool man!
    Works great! Thanks :)
     
  9. Digika

    Digika

    Joined:
    Jan 7, 2018
    Posts:
    211
    I'm stealing this because I dont want bother with trigonometry and vector math. However, your mesh generation code has issue as Drawmesh API complains about indexes being OOB.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    32,311