#Problem with Player movement with an ECS

1 messages · Page 1 of 1 (latest)

burnt narwhal
#

Having trouble to get my player to move, they are using an ECS, is there any problems with my code?

using UnityEngine;
using Unity.Entities;
using Unity.Burst;
using Unity.Transforms;
using Unity.Mathematics;
[BurstCompile]
public partial struct PlayerSystem : ISystem
{
    //public void OnCreate(ref SystemState state){ }
    //public void OnDestroy(ref SystemState state) { }
    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        float horizontalInput = Input.GetAxisRaw("Horizontal");
        float verticalInput = Input.GetAxisRaw("Vertical");
        Vector2 movement2D = new Vector2(horizontalInput, verticalInput);
        movement2D = movement2D.normalized;
        float3 movement = new float3(movement2D.x, movement2D.y, 0);
        foreach (var (player, localTransform) in SystemAPI.Query<RefRW<Player>, LocalTransform>())
        {
            movement = math.normalize(movement);
            Entity playerEntity = player.ValueRW.Entity;
            LocalTransform playerTransform = state.EntityManager.GetComponentData<LocalTransform>(playerEntity);
            playerTransform.Position += movement * player.ValueRW.Speed;// * Time.deltaTime;
            state.EntityManager.SetComponentData(player.ValueRW.Entity, localTransform);
            state.EntityManager.SetComponentData(player.ValueRW.Entity, player.ValueRW);
        }
    }

}


burnt narwhal
#

still looking for help if anyone has any ideas

drifting zenith
#

well you have some issues here

#
        float verticalInput = Input.GetAxisRaw("Vertical");```
#

i'd be surprised if this could be burst compiled

#
       state.EntityManager.SetComponentData(player.ValueRW.Entity, player.ValueRW);
#

you don't need to do this

#

you should return this
LocalTransform by RefRW<LocalTransform>

#

so you can write to it direct

#

honestly i'm quite confused why you are querying Player and then writing to a different entity

burnt narwhal
# drifting zenith well you have some issues here

hello, thank you for your response, as I am only going to have one play er I don't really need it to be burst compiled so I may just remove that part, I have gotten an enemy script that uses a similar method to move. But it does not take input.
the input seems to work but is not passing to the player to move it.
what do you mean I am writing to a different entity? Is that the problem? And what LocalTransform do I need to turn into a RefRW?

#
using UnityEngine;
using Unity.Entities;
using Unity.Burst;
using Unity.Transforms;
using Unity.Mathematics;
//[BurstCompile]
public partial struct PlayerSystem : ISystem
{
    //public void OnCreate(ref SystemState state){ }
    //public void OnDestroy(ref SystemState state) { }
    //[BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        float horizontalInput = Input.GetAxisRaw("Horizontal");
        Debug.Log(horizontalInput);
        float verticalInput = Input.GetAxisRaw("Vertical");
        float3 movement = new float3(horizontalInput, verticalInput, 0);
        Vector2 movement2D = new Vector2(movement.x, movement.y);
        movement2D = math.normalize(movement2D);
        foreach (var (player, localTransform) in SystemAPI.Query<RefRW<Player>, LocalTransform>())
        {
            MovePlayer(ref state, player, movement2D);

        }
    }
    private void MovePlayer(ref SystemState state, RefRW<Player> player, Vector2 movement2D)
    {
        var time = Time.deltaTime;
        Entity playerEntity = player.ValueRW.Entity;
        LocalTransform playerTransform = state.EntityManager.GetComponentData<LocalTransform>(playerEntity);
        playerTransform.Position.x += movement2D.x * player.ValueRW.Speed * time;
        playerTransform.Position.y += movement2D.y * player.ValueRW.Speed * time;
        player.ValueRW.CurrentPosition = new float3(playerTransform.Position.x, playerTransform.Position.y, playerTransform.Position.z);
        //state.EntityManager.SetComponentData(playerEntity, playerTransform);
    }
}

Here is what I am currently working with aswell.