Search Unity

Question Entity movement with ECS

Discussion in 'Entity Component System' started by olegy692, May 24, 2023.

  1. olegy692

    olegy692

    Joined:
    Feb 13, 2023
    Posts:
    2
    Hello,

    I`m trying to implement basic movement system for entity with LocalToWorld since Translation is not available. I have this code but entity is still not changing position. I need to move prefab which spawns when space is hit. Any ideas what i need to change?


    public struct MovementSpeedComponent : IComponentData
    {
    public float speed;
    public LocalTransform localTransform;
    }

    public class MovementSpeedAuthoring : MonoBehaviour
    {

    public float speed;

    }

    public class speedBaker : Baker<MovementSpeedAuthoring>
    {
    public override void Bake(MovementSpeedAuthoring authoring)
    {
    var entity = GetEntity(TransformUsageFlags.Dynamic);
    AddComponent(entity, new MovementSpeedComponent
    {
    speed = authoring.speed

    }) ;
    }
    }


    public partial class MovementSystem : SystemBase
    {
    protected override void OnUpdate()
    {
    float deltaTime = SystemAPI.Time.DeltaTime;

    Entities.ForEach((ref LocalToWorld localToWorld, ref MovementSpeedComponent movementSpeed) =>
    {
    float3 inputDirection = new float3(UnityEngine.Input.GetAxis("Horizontal"), 0f, UnityEngine.Input.GetAxis("Vertical"));
    float3 movement = math.normalize(inputDirection) * movementSpeed.speed;

    float3 newPosition = movementSpeed.localTransform.Position + movement;

    movementSpeed.localTransform.Position = newPosition;

    localToWorld.Value = movementSpeed.localTransform.ToMatrix();

    }).Schedule();
    }
    }
     
  2. Arnold_2013

    Arnold_2013

    Joined:
    Nov 24, 2013
    Posts:
    287
    I don't think you can set the localToWorld, it is calculated in some system. You need to set the localTransform with LocalTransform.FromPosition(newPosition).
     
    daniel-holz likes this.
  3. olegy692

    olegy692

    Joined:
    Feb 13, 2023
    Posts:
    2
    Thank you. It took me some time to figure out how it works.
    here is updated code if someone will need it

    My component has speed and LocalTransform attributes. And Baker.
    public partial class MovementSystem : SystemBase
    {
    protected override void OnUpdate()
    {
    float deltaTime = SystemAPI.Time.DeltaTime;

    float horizontalInput = Input.GetAxis("Horizontal");
    float verticalInput = Input.GetAxis("Vertical");

    Entities.ForEach((ref MovementSpeedComponent movementSpeed, ref LocalTransform transform) =>
    {
    Vector3 newPosition = transform.Position;

    if (horizontalInput != 0f)
    {
    newPosition.x += horizontalInput * movementSpeed.speed * deltaTime;
    }
    if (verticalInput != 0f)
    {
    newPosition.z += verticalInput * movementSpeed.speed * deltaTime;
    }

    transform.Position = newPosition;
    }).ScheduleParallel();
    }
    }


    However, it is harder because before i used Vector3 instead of float, and it was more simple.
     
    Last edited: May 30, 2023