Cannot write to LocalToWorld but Translation works just fine...

  • Unity Version: 2019.1.0b7

  • Related Package Version:
    “com.unity.burst”: “1.0.0-preview.6”,
    “com.unity.collections”: “0.0.9-preview.16”,
    “com.unity.entities”: “0.0.12-preview.29”,
    “com.unity.jobs”: “0.0.7-preview.9”,
    “com.unity.mathematics”: “1.0.0-preview.1”,

  • OS: Mac 10.14 Mojave

  • Purpose: Simply Move player using input in OnUpdate
    Codes to Reproduce:

1.This doesn’t work (Assign value to LocalToWorld just like what the Boid Sample does.)

using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;

public class MovementSystem : JobComponentSystem
{
    private ComponentGroup m_PlayerGroup;
    private ComponentGroup m_NPCGroup;
    private ComponentGroup m_TreeGroup;
    [BurstCompile]
    public struct Movement : IJobProcessComponentData<LocalToWorld, MovementSpeed>
    {
        public float DeltaTime;
        public float3 direction;
        public void Execute(ref LocalToWorld localToWorld, [ReadOnly] ref MovementSpeed movementSpeed)
        {
            var speed               =   DeltaTime * movementSpeed.Speed;
            var direction_normalize =   math.normalizesafe(direction);  // if not normalizesafe, it's boom.
            var movePosition        =   localToWorld.Position + direction_normalize*speed;
           
            localToWorld = new LocalToWorld
            {
                Value = float4x4.TRS(
                    movePosition,
                    quaternion.LookRotationSafe(direction_normalize,math.up()),
                    new float3 (1.0f,1.0f,1.0f)
                )
            };
        }
    }
    protected override JobHandle OnUpdate(JobHandle inputDependency)
    {
        Movement MovementJob = new Movement
        {
            direction = new float3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")),
            DeltaTime = Time.deltaTime,
        };
        Debug.Log(MovementJob. direction);
        return MovementJob.ScheduleGroup(m_PlayerGroup, inputDependency);
    }
    protected override void OnCreateManager()
    {
        m_PlayerGroup = GetComponentGroup(new EntityArchetypeQuery
        {
            All = new []
            {
                ComponentType.ReadOnly<Player>(),
                    ComponentType.ReadWrite<LocalToWorld>(),
                    ComponentType.ReadOnly<MovementSpeed>()
            }
        });
    }
}
  • This works just fine. ( Just change everything to translation)
  public struct Movement : IJobProcessComponentData<Translation, MovementSpeed>
    {
        public float DeltaTime;
        public float3 direction;
        public void Execute(ref Translation Translation, [ReadOnly] ref MovementSpeed movementSpeed)
        {
            var speed               =   DeltaTime * movementSpeed.Speed;
            var direction_normalize =   math.normalizesafe(direction);  // if not normalizesafe, it's boom.
            var movePosition        =   Translation.Value + direction_normalize*speed;//direction_normalize * speed;
            Translation.Value       =   movePosition;
           
        }
    }
  • I’m struggling with this a day… I’m not sure if it’s a bug or my bad(I guess it’s the latter), any help would be highly appreciated!

Have you taken into account what it says here?

https://docs.unity3d.com/Packages/com.unity.entities@0.0/manual/transform_system.html

  • User code may write directly to LocalToWorld to define the transform for an instance, if no other transform components are associated with the same entity.

Thanks, I re-read it and found this in first page…