#Issue in moving objects in ecs1.0.16

1 messages · Page 1 of 1 (latest)

thin minnow
#

I'm using urp 2D and I want to move my objects from the initial position to the target position,
I did made custom aspect and I'm using refrw of local transform.
I do have a move method which contains the following code:
public void Move(float deltatime)
{
float3 direction = math.normalize(targetposition - local transform.position);
Local transform.position += direction * deltatime* speed;
}

There are no errors in console but the object doesn't move .
I'm using unity ver: 2022.3.11 lts and ecs 1.0.16
Urp 2D project.
Thanks in advance
#1062393052863414313

plain hamlet
#

your code is illegal and wouldn't compile (local transform)

#

but my guess is you're writing to a local copy of LocalTransform

#

instead of using ref

#

or writing it back

thin minnow
#

I'm using refrw for local transform and also other 2 components which I'm using in customtransform aspect

plain hamlet
#

well in the code you posted that isn't true

#

so maybe post the actual code?

thin minnow
#

Ok sorry for the wrong code

#

public readonly partial struct HorizontalAspect : IAspect
{
readonly RefRW<LocalTransform> localTransform;
readonly RefRO<MobItemData> m_MobItem;
readonly RefRW<MobSpawnData> m_MobSpawnData;

public void Move(float deltaTime)
{        
    float3 direction = math.normalize(m_MobSpawnData.ValueRW.endPosition - localTransform.ValueRW.Position);
    localTransform.ValueRW.Position += direction  deltaTime  m_MobItem.ValueRO.mobSpeed;        
}

}

#

@plain hamlet here is the code for custom transform aspect

plain hamlet
#

i think its missing some *

#

but ok that looks fine

thin minnow
#

You mean the system?

plain hamlet
#

direction deltaTime m_MobItem.ValueRO.mobSpeed;

#

missing *

#

but yeah code wise that is fine

#

i dont see why it wouldn't move as long as
a) you are actually passing a DT in thats not 0
b) your mobSpeed isn't 0

thin minnow
#

I have checked it multiple times the mobspeed is 2

#

It instantiates correctly but it doesn't move

#

It moves with out target position in a particular direction forever

#

But when I calculate the direction and give it to the movement thing it doesn't move

plain hamlet
#

you're just going to have to debug it and see what's going on

sly burrow
#

Please post code in format to help people reading them

sly burrow
thin minnow
#

I'm running it on main thread

#

From a moving system which is implemented using isystem
And in its update method for each I'm calling that aspect move method

sly burrow
#

Doesn't help us to discern your issue at all.

#

You can just post the system code here

thin minnow
#

Okay

sly burrow
#

all of the system code

thin minnow
#

@sly burrow do you also need the MobItemData.cs and MobSpawnData.cs

#

Those two are components

sly burrow
thin minnow
#

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

public partial struct MobMovingSystem : ISystem
{
    public void OnCreate(ref SystemState state)
    {
        state.RequireForUpdate<MobSpawnData>();
    }

    public void OnUpdate(ref SystemState state)
    {
        float deltaTime = SystemAPI.Time.DeltaTime;
        foreach (var item in SystemAPI.Query<HorizontalAspect>())
        {
            item.Move(deltaTime);
        }
    }
}

public readonly partial struct HorizontalAspect : IAspect
{    
    readonly RefRW<LocalTransform> localTransform;
    readonly RefRW<MobItemData> m_MobItem;
    readonly RefRO<MobSpawnData> m_MobSpawnData;

    public void Move(float deltaTime)
    {
        float3 direction = math.normalize(m_MobSpawnData.ValueRO.endPosition - localTransform.ValueRW.Position);
        localTransform.ValueRW.Position += direction * m_MobItem.ValueRO.mobSpeed*  deltaTime;        
    }
}
sly burrow
#

Place a Debug.Log after this line item.Move(deltaTime); to see what happens?

thin minnow
#

I'm not able to see debugs in Ecs

#

In console it's blank

sly burrow
#

Turn off Burst

thin minnow
#

I did disable it

#

Still no output of debug

sly burrow
#

Remove this line state.RequireForUpdate<MobSpawnData>();
And place a Debug.Log before this line float deltaTime = SystemAPI.Time.DeltaTime;

thin minnow
#

I can see the debug which I placed before the line deltatime = systemapi.time.deltatime

#

Can we use 2 different components in same aspect ?

#

Cz I tried removing 1 component either MobItemData or MobSpawnData then I can see the visual that object is moving but that's not what I want though

sly burrow
#

You can use 1 backtick for inline code. 😉

sly burrow
#

You can't query 3 components on 2 or more entities.

thin minnow
#

That's not the scenario here

sly burrow
#

Then it's odd that you can't query the entities you want.

thin minnow
#

I have 1 prefab where I have attached MobItemData which contains its speed
And I have attached the other a gameobject where I want to spawn the prefabs
Which contains data of spawn amount, spawn position, prefab reference and end position

sly burrow
#

In this case would you please show your Entities Hierarchy? And the inspector when you select 1 entity.

thin minnow
#

Ok

sly burrow
#

You have to close the subscene.

thin minnow
#

should i show in run mode?

sly burrow
#

Close the subscene and show it in run mode, yeah.

thin minnow
sly burrow
#

You see, your entity only have LocalTransform and MobItemData

#

There is no MobSpawnData on this entity

#

So the query won't work

thin minnow
#

so what should i do

sly burrow
#

Why do you need MobSpawnData?

thin minnow
#

i seperated it cz i wanted to instantiate

#

and give them spawnposition and endposition explicity

#

as you can see there are two towers both of them will be having different spawn points and end points to bifercate them i seperated the component

sly burrow
#

Then make an EndPosition component and add it to your entity after instantiating

thin minnow
#

same authoring and different component?

sly burrow
#

The rule is: your entity must have all the component in the query.

thin minnow
#

okay

#

so if i did create a new component for endposition how should i add it after instantiation

#

any refernce to it?

sly burrow
#

How is your instantiation system atm?

thin minnow
#
using Unity.Entities;
using Unity.Mathematics;

public struct MobSpawnData : IComponentData
{
    internal Entity mobPrefab;
    internal float maxSpawnCount;
    internal float3 spawnPosition;
    internal float3 endPosition;    
}
using Unity.Entities;
using UnityEngine;

public class SpawningAuthoring : MonoBehaviour
{
    [SerializeField] internal GameObject mobPrefab;
    [SerializeField] internal Transform endPosition;
    [SerializeField] internal float maxSpawnCount;    
}

public class SpawnerBaker : Baker<SpawningAuthoring>
{
    public override void Bake(SpawningAuthoring authoring)
    {
        var entity = GetEntity(TransformUsageFlags.None);
        AddComponent(entity, new MobSpawnData
        {
            mobPrefab = GetEntity(authoring.mobPrefab, TransformUsageFlags.Dynamic),
            spawnPosition = authoring.transform.position,
            endPosition = authoring.endPosition.position,
            maxSpawnCount = authoring.maxSpawnCount,            
        });        
    }
}
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;

public partial struct SpawningSystem : ISystem
{
    float counter;
    uint updateCounter;
    public void OnUpdate(ref SystemState state)
    {
        foreach (var item in SystemAPI.Query<MobSpawnData>())
        {
            if (counter < item.maxSpawnCount)
            {
                SpawnMob(ref state, item);
                Debug.Log("Spawning");
            }
        }        
    }

    public void SpawnMob(ref SystemState state, MobSpawnData mob)
    {        
        Entity newEntity =  state.EntityManager.Instantiate(mob.mobPrefab);

        var randomY = Unity.Mathematics.Random.CreateFromIndex(updateCounter++);
        state.EntityManager.SetComponentData(newEntity, LocalTransform.FromPosition(mob.spawnPosition + randomY.NextFloat3(-1,1.5f)));

        counter++;
    }
}
#

currently im spawning mobs like this
i can control the amount i want to spawn

sly burrow
#

yeah if you're already doing instantiation in the main thread then you can use state.EntityManager.AddComponent for EndPosition.

thin minnow
#

and when will i know when the object is instantiate?
do i need to make a new system for that? and then write state.RequiredForUpdate to add the components?

sly burrow
#

You are using EntityManager so right after this line
Entity newEntity = state.EntityManager.Instantiate(mob.mobPrefab);

thin minnow
#

and what about the inside content?

#

i will be needing an authoring component too for it?

#

and also the AddComponent takes 2 arguements what about the first parameter

#

Sorry, i'm new to ECS getting my hands on it.....

sly burrow
#
public void SpawnMob(ref SystemState state, MobSpawnData mob)
{        
    Entity newEntity =  state.EntityManager.Instantiate(mob.mobPrefab);

    // Just like this
    state.EntityManager.AddComponentData(newEntity, new EndPosition { value = mob.endPosition });

    var randomY = Unity.Mathematics.Random.CreateFromIndex(updateCounter++);
    state.EntityManager.SetComponentData(newEntity, LocalTransform.FromPosition(mob.spawnPosition + randomY.NextFloat3(-1,1.5f)));

    counter++;
}
#
public struct EndPosition : IComponentData
{
    public float3 value;
}
thin minnow
#

where we will assign the end point value then?

sly burrow
#

Well, read the code, it's basic C# btw 🤷

thin minnow
#

oh sorry i thought it was a different component

#

btw i would also want to change the sprite.color attribute of the prefab which will be instantiating

#

is there any way i could achive it or how can i add color component in an entity

#

i tried float4 for rgba values but at the time of authoring i was lost on how to do it

sly burrow
#

My bad, it's AddComponentData

#

not AddComponent

#

AddComponent only add the type of the component to the entity

#

AddComponentData will add the type and set the value

#

To set sprite.color, try this
var spriteRenderer = state.EntityManager.GetComponentObject<SpriteRenderer>(entity);?

sly burrow
#

Well, from this point onwards, you should look things up in the EntityManager document 😄

thin minnow
#

okay

#

manual or scripting api?

sly burrow
#

both?

#

read the manual very carefully so you don't miss crucial things there

#

Scripting API to look for things you want to do but aren't covered in the manual

#

At the end if you can't find any, you're free to ask here.

thin minnow
#

btw even at this point i cant see endpointData component been added into the entity prefab

sly burrow
#

No, it's added to the actual entity

#

not the prefab entity

thin minnow
#

i did check in runtime

#

the entities are not moving and also i cant see the component added to the entity

sly burrow
#

there is no log?

thin minnow
#

there is but not inside the foreach one

#

the log which is above the code item.Move(deltatime) is not visible

#

it spawns multiple entities

#

more then the number i used to restrict

sly burrow
#

any entity has the EndPosition?

thin minnow
#

no

#

i checked all the entity none of them had that component

sly burrow
#

Ok, maybe it's because of the AddComponentData method

#

Well, then you have to add the component to the prefab in your baker

#

and use SetComponentData in your spawn system instead

thin minnow
#

which baker should i add then?

sly burrow
#

The baker where you add MobItemData

#

Or you can simple do it like this

public class SpawnerBaker : Baker<SpawningAuthoring>
{
    public override void Bake(SpawningAuthoring authoring)
    {
        var entity = GetEntity(TransformUsageFlags.None);
        var mobPrefab = GetEntity(authoring.mobPrefab, TransformUsageFlags.Dynamic);

        AddComponent<EndPositionData>(mobPrefab);

        AddComponent(entity, new MobSpawnData
        {
            mobPrefab = mobPrefab,
            spawnPosition = authoring.transform.position,
            endPosition = authoring.endPosition.position,
            maxSpawnCount = authoring.maxSpawnCount,            
        });        
    }
}
thin minnow
#

Its working thanks, what we did is like we gave him a dummy value and then we overwrite the data ?

#

is that right?

sly burrow
#

yah

#

This also works the same way, but decouples the Sprite and the Spawner:

public class SpriteAuthoring : MonoBehaviour
{
}

public class SpriteBaker : Baker<SpriteAuthoring>
{
    public override void Bake(SpriteAuthoring authoring)
    {
        var entity = GetEntity(TransformUsageFlags.Dynamic);
        AddComponent<EndPositionData>(entity );

        // You can add many more components here
    }
}

public class SpawnerBaker : Baker<SpawningAuthoring>
{
    public override void Bake(SpawningAuthoring authoring)
    {
        var entity = GetEntity(TransformUsageFlags.None);

        AddComponent(entity, new MobSpawnData
        {
            mobPrefab = GetEntity(authoring.mobPrefab, TransformUsageFlags.Dynamic),
            spawnPosition = authoring.transform.position,
            endPosition = authoring.endPosition.position,
            maxSpawnCount = authoring.maxSpawnCount,            
        });        
    }
}