Search Unity

Having problems with a simple Rotate System

Discussion in 'Entity Component System' started by dimitroff, Nov 28, 2018.

  1. dimitroff

    dimitroff

    Joined:
    Apr 3, 2013
    Posts:
    131
    Hi,
    I feel very stupid, but I could not get that simple Rotation System to work.

    Here is the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Collections;
    5. using Unity.Entities;
    6. using Unity.Transforms;
    7. using Unity.Mathematics;
    8. using System;
    9. using System.Runtime.CompilerServices;
    10.  
    11. public class OneRotationSystem : ComponentSystem {
    12.  
    13.     public struct CubeGroup {
    14.         public ComponentDataArray<Rotation> cubeRotation;
    15.         public EntityArray Entities;
    16.         public readonly int Length;
    17.     }
    18.  
    19.  
    20. #pragma warning disable 649
    21.     [Inject] private CubeGroup cubeGroup;
    22. #pragma warning restore 649
    23.  
    24.     protected override void OnUpdate()
    25.     {
    26.         float dt = Time.deltaTime;
    27.         float speed = 100;
    28.  
    29.         for (var i = 0; i < cubeGroup.Length; ++i)
    30.         {
    31.             var rotation = cubeGroup.cubeRotation[i];
    32.             rotation.Value = math.mul(math.normalize(rotation.Value), quaternion.AxisAngle(math.up(), dt * speed));
    33.         }
    34.     }
    35. }
    36.  
    My entities, have Position, Rotation and MeshInstanceRenderer Components. I have instantiated them and I see them in the scene, but I could not modify the rotation, I have a feeling that something is overwriting the old value every frame I try to modify it.

    I am using Unity 2018.3b11 and the latest Entities version available in the Package Manager.

    Thanks!
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    You're only modifying a local copy of rotation.

    Remember that ComponentData are structs, not a classes, so you need to apply it back.

    Code (CSharp):
    1.             var rotation = cubeGroup.cubeRotation[i];
    2.             rotation.Value = math.mul(math.normalize(rotation.Value), quaternion.AxisAngle(math.up(), dt * speed));
    3.             cubeGroup.cubeRotation[i] = rotation;
    Side note, [Inject] and ComponentDataArray are being removed so you should look into chunk iteration.
     
  3. Ofx360

    Ofx360

    Joined:
    Apr 30, 2013
    Posts:
    155
    Are you sure about ComponentDataArray?
     
  4. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
  5. e199

    e199

    Joined:
    Mar 24, 2015
    Posts:
    101
    I feel like it's easier to write common tasks with Jobs than regular systems now.

    IJobProcessComponentData / IJobProcessComponentDataWithEntity and substractive attributes just saving me from using Chunk iteration by hand.

    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Jobs;
    3. using Unity.Mathematics;
    4. using UnityEngine;
    5.  
    6. [UpdateAfter(typeof(RemoveHeatingSystem))]
    7. public class DecreaseTemperatureSystem : JobComponentSystem
    8. {
    9.     [RequireSubtractiveComponent(typeof(Heating))]
    10.     private struct DecreaseTemprerature : IJobProcessComponentData<Temperature>
    11.     {
    12.         public float Delta;
    13.         public void Execute(ref Temperature data)
    14.         {
    15.             data.Value = math.max(data.Value - Delta, 0f);
    16.         }
    17.     }
    18.    
    19.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    20.     {
    21.         return new DecreaseTemprerature{Delta = Time.deltaTime}.Schedule(this, inputDeps);
    22.     }
    23. }
     
  6. Ofx360

    Ofx360

    Joined:
    Apr 30, 2013
    Posts:
    155
    Thanks SubPixel