Search Unity

How To Spawn Entities With Different Properties

Discussion in 'Entity Component System' started by dkilmer, Apr 11, 2019.

  1. dkilmer

    dkilmer

    Joined:
    Jul 12, 2015
    Posts:
    6
    To use a concrete example -- if I wanted to change the HelloCube_06_SpawnFromEntity example to spawn cubes with different (say, random) rotation speeds, how would I go about that? Changing the RotationSpeedProxy to use a random value gives all of the cubes spawned that same random value.
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    You would modify the spawn job to use a Unity.Mathematics.Random object to generate rotation values and set the rotation the same way the translation is set.
     
  3. dkilmer

    dkilmer

    Joined:
    Jul 12, 2015
    Posts:
    6
    I'm wanting to change the rotation speed rather than the actual rotation. I'm not sure how to access that property in the spawner.
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    Then set the RotationSpeed component instead of the Rotation component in the job.
     
  5. dkilmer

    dkilmer

    Joined:
    Jul 12, 2015
    Posts:
    6
    For anyone who wonders what I was wondering, the answer is to do the following in the Execute() method of the SpawnJob struct. At the top of the method, create a Unity.Mathematics.Random:

    Code (CSharp):
    1. var rnd = new Random(0x482093u);
    and after setting the translation, add:

    Code (CSharp):
    1. CommandBuffer.SetComponent(instance, new RotationSpeed {RadiansPerSecond = rnd.NextFloat(0.1f, 6.282f)} );