1

So I am working on a project that has a bunch of dynamic fog objects and I wanted to try and use DOTS for it, basically it is a ton of small sprites that shrink when the player approaches. I got all that working fine, and now I need to update the objects with the player's position.

I tried directly passing in the player's transform, but that didn't work, so I settled on passing a vector2. However, I was unable to update this vector2 in the authoring script or even debug.log I think this is due to the entity being in a sub scene or due to it being an entity, and now I'm looking for a fix.

FogAuthoring.cs

using System.Collections;
using System.Collections.Generic;

using Unity.Entities;

using Unity.Mathematics;

using UnityEngine;

public class FogAuthoring : MonoBehaviour

{

public float LerpSpeed;

public float TargetScale;

public float2 PlayerPosition;

private Transform _player;

private void Start()

{

_player = GameObject.FindWithTag("Player").transform;

Debug.Log("A");

}

private void Update()

{

PlayerPosition = new float2(_player.position.x, _player.position.y);

Debug.Log("B");

}

private class Baker : Baker<FogAuthoring>

{

public override void Bake(FogAuthoring authoring)

{

Entity entity = GetEntity(TransformUsageFlags.Dynamic);

AddComponent(entity, new FogData

{

LerpSpeed = authoring.LerpSpeed,

TargetScale = authoring.TargetScale,

PlayerPosition = authoring.PlayerPosition

});

}

}

}

public struct FogData : IComponentData

{

public float LerpSpeed;

public float TargetScale;

public float2 PlayerPosition;

}

FogControllerDOTS.cs

using System.Collections
using System.Collections.Generic;
using UnityEngine;

using Unity.Burst;

using Unity.Entities;

using Unity.Transforms;

using Unity.Jobs;

public partial struct FogControllerDOTS : ISystem

{

public const float distThreshold = 5;

public void OnCreate(ref SystemState state)

{

state.RequireForUpdate<FogData>();

}

[BurstCompile]

public void OnUpdate(ref SystemState state)

{

/*

foreach ((RefRW<LocalTransform> localTransform, RefRO<FogData> fogData) in SystemAPI.Query<RefRW<LocalTransform>, RefRO<FogData>>())

{

float currentScale = localTransform.ValueRO.Scale;

float targetScale;

if (Vector2.Distance(new Vector2(localTransform.ValueRO.Position.x, localTransform.ValueRO.Position.y), fogData.ValueRO.PlayerPosition) <= distThreshold)

targetScale = Mathf.Lerp(currentScale, 0, fogData.ValueRO.LerpSpeed * SystemAPI.Time.DeltaTime);

else

targetScale = Mathf.Lerp(currentScale, fogData.ValueRO.TargetScale, fogData.ValueRO.LerpSpeed * SystemAPI.Time.DeltaTime);

localTransform.ValueRW = localTransform.ValueRO.WithScale(targetScale);

}*/

FogParticleJob fogParticleJob = new FogParticleJob

{

deltaTime = SystemAPI.Time.DeltaTime

};

fogParticleJob.ScheduleParallel();

}

[BurstCompile]

public partial struct FogParticleJob : IJobEntity

{

public float deltaTime;

public void Execute(ref LocalTransform localTransform, in FogData fogData)

{

float currentScale = localTransform.Scale;

float targetScale;

if (Vector2.Distance(new Vector2(localTransform.Position.x, localTransform.Position.y), fogData.PlayerPosition) <= distThreshold)

targetScale = Mathf.Lerp(currentScale, 0, fogData.LerpSpeed * deltaTime);

else

targetScale = Mathf.Lerp(currentScale, fogData.TargetScale, fogData.LerpSpeed * deltaTime);

localTransform = localTransform.WithScale(targetScale);

}

}

}
1
  • It becomes an entity. The game object gets destroyed. It is more like a prefab. There are a lot of courses on dots and entities and it is a very different way of thinking Commented Jul 30, 2024 at 9:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.