Is it possible to change position of Main Camera in a System with Burst Compiler?

I want to change the camera position in my system. I add a component to the main camera. And i am changing the LocalTransform position. But in my game tab, camera does not moving.

using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.NetCode;
using Unity.Transforms;

[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
public partial struct CameraFolowSystem : ISystem
{
    public void OnCreate(ref SystemState state)
    {
        var builder = new EntityQueryBuilder(Allocator.Temp);
        state.RequireForUpdate(state.GetEntityQuery(builder));
    }

    public void OnUpdate(ref SystemState state)
    {
        foreach (var (cameraData, cameraTransform) in SystemAPI.Query<CameraData, RefRW<LocalTransform>>().WithAll<CameraData, LocalTransform>())
        {
            foreach (var transform in SystemAPI.Query<LocalToWorld>().WithAll<LocalTransform, GhostOwnerIsLocal>())
            {
                cameraTransform.ValueRW.Position = new float3(transform.Position.x, transform.Position.y, cameraTransform.ValueRO.Position.z);
            }
        }

    }
}
  1. Lines 12 and 13 are doing nothing for you. There’s nothing in that EntityQueryBuilder to build a valid query from.
  2. You aren’t even using Burst. You need the [BurstCompile] attribute on OnUpdate().
  3. You say the following:

This could mean a bunch of different things. Can you explain in detail how exactly you are doing this?

Unless you have a CompanionLink somewhere, your solution is going to require TransformAccessArray and IJobParallelForTransform if you want a full Burst solution. But you probably don’t need that for just a camera.

Hello, i add CameraAuthoring.cs script to my Main Camera. Then i get the Main Camera’s Local Transform Entity with query and change its position values. I can see the changes in Entity Hierarchy tab.

CameraAuthoring.cs

using Unity.Entities;
using UnityEngine;

public class CameraAuthoring : MonoBehaviour
{
}

public struct CameraData : IComponentData
{
}

public class CameraBaker : Baker<CameraAuthoring>
{
    public override void Bake(CameraAuthoring authoring)
    {
        var entity = GetEntity(TransformUsageFlags.Dynamic);
        AddComponent<CameraData>(entity);
    }
}

CameraFolowSystem.cs;

using Unity.Entities;
using Unity.Mathematics;
using Unity.NetCode;
using Unity.Transforms;

[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
[BurstCompile]
public partial struct CameraFolowSystem : ISystem
{
    [BurstCompile]
    public void OnCreate(ref SystemState state)
    {
    }
    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        foreach (var (cameraData, cameraTransform) in SystemAPI.Query<CameraData, RefRW<LocalTransform>>().WithAll<CameraData, LocalTransform>())
        {
            foreach (var transform in SystemAPI.Query<LocalToWorld>().WithAll<LocalTransform, GhostOwnerIsLocal>())
            {
                cameraTransform.ValueRW.Position = new float3(transform.Position.x, transform.Position.y, cameraTransform.ValueRO.Position.z);
            }
        }

    }
}



There is no native ECS camera type, and your camera entity doesn’t reference a GameObject in any way. There’s no connection between your entity and the camera that is rendering.

You’ll need to tie the two together manually. Everyone has their own preferred way of doing it.

Oh nice, how can i do that?

I think most people just write to Camera.main.transform in a SystemBase. My personal approach is a bit heavy-handed: Latios-Framework-Documentation/Transforms/Getting Started.md at main · Dreaming381/Latios-Framework-Documentation · GitHub

This asset might interest you too :