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

Showcase Place an object at every vertex of an object

Discussion in 'Scripting' started by AtlasT2022, Sep 24, 2022.

  1. AtlasT2022

    AtlasT2022

    Joined:
    Mar 12, 2022
    Posts:
    60
    This is just a fun thing you could use in your game.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Fragmenter : MonoBehaviour
    6. {
    7.     // The object you want to spawn at the vertex points
    8.     public GameObject fragmentObject;
    9.  
    10.     // Awake is called when the object is loaded
    11.     void Awake()
    12.     {
    13.         MeshFilter filter = GetComponent<MeshFilter>();
    14.         Vector3[] thisVertices = filter.mesh.vertices;
    15.         foreach (Vector3 vecthis in thisVertices)
    16.             Instantiate(fragmentObject, vecthis, Quaternion.identity);
    17.     }
    18. }
    19.  
     
    Kurt-Dekker likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,515
    Love it... looks like maybe you're making a grenade?

    You could iterate the normals of the mesh too, and use that to turn each item outward by setting its rotation.

    If you want more random procgen stuff (and to me Unity has the BEST procgen easy-to-use API), check out my MakeGeo project. MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo

    PS - what on earth is your avatar photo?! Is that some kind of giant bug in your palm eating a carrot??
     
    AtlasT2022 and orionsyndrome like this.
  3. AtlasT2022

    AtlasT2022

    Joined:
    Mar 12, 2022
    Posts:
    60
    Giant weta eating a carrot.
     
    orionsyndrome and Kurt-Dekker like this.
  4. AtlasT2022

    AtlasT2022

    Joined:
    Mar 12, 2022
    Posts:
    60
    It's not in my palm though.
     
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,070
    I hear Unity likes wetas, so you're well-equipped ;)
     
    AtlasT2022 likes this.
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,070
    btw this would make the objects stand upright against some arbitrary point inside of mesh
    Code (csharp):
    1. for(int i = 0; i < mesh.vertices.Length; i++) {
    2.   var vert = mesh.vertices[i];
    3.   var normal = (point - vert).normalized;
    4.   var tangent = Vector3.Cross(normal, Vector3.right);
    5.   Instantiate(fragmentObject, vert, Quaternion.LookRotation(tangent, normal));
    6. }
     
    Kurt-Dekker and AtlasT2022 like this.