Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to pass position/Rotation/Velocity vectors to another entity through a string?

Discussion in 'Entity Component System' started by goodnewsjimdotcom, May 26, 2021.

  1. goodnewsjimdotcom

    goodnewsjimdotcom

    Joined:
    May 24, 2017
    Posts:
    342
    I want to pass position/rotation/velocity via string to other entities. I wanted my player entity to give it its values of position/rotation/velocity, how do I do that?

    What I have now:

    Translation entPos = entityManager.GetComponentData<Translation>(playerEntity);
    Rotation entRot = entityManager.GetComponentData<Rotation>(playerEntity);
    Quaternion q = entRot.Value;

    s = entPos.Value.x + "," + entPos.Value.y + "," + entPos.Value.z + "," + q.eulerAngles.x + "," + q.eulerAngles.y + "," + q.eulerAngles.z+"0"+"0"+"0";


    //Turns out I can use entPos.Value.x,y,z fine and recreate it via:

    entityManager.SetComponentData(myShipObject, new Translation { Value = new Vector3(x1, y1, z1) });

    I try and fail to unpack the rotation via:
    entityManager.SetComponentData(myShipObject, new Rotation { Value = quaternion.EulerXYZ(ex1, ey1, ez1) });//just looks spastic rotating.

    I don't even know how to read the velocity vector...

    Any help is great.
     
  2. Fribur

    Fribur

    Joined:
    Jan 5, 2019
    Posts:
    127
  3. goodnewsjimdotcom

    goodnewsjimdotcom

    Joined:
    May 24, 2017
    Posts:
    342
    I know what I'm doing. I'm using TCP/IP. Yes, I can compress it via bit wise methods, but I like to just pass the string first.

    Can you please answer my question? Do you know the answer? Does anyone know?
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,580
    You need parse string back to values, if you want to use such approach. Easiest but not the most performant way, is using json as massage format. Easy to pack and unpack data.

    Also, you need reference to entity in the message. Some id, but not the same as index and version of entity, as that may be different between clients and server.

    Other than that, I suggest you have a look into unity DOTS networking solutions. Specially live link.
     
  5. Fribur

    Fribur

    Joined:
    Jan 5, 2019
    Posts:
    127
    It looks like you are not separating the velocity vector values via “,”?
    Code (CSharp):
    1. s = entPos.Value.x + "," + entPos.Value.y + "," + entPos.Value.z + "," + q.eulerAngles.x + "," + q.eulerAngles.y + "," + q.eulerAngles.z+"0"+"0"+"0";
    How to you parse your string? string.split at “,”? Do you then use float.parse on the array items? Is “,” also the decimal separator for float values in your language? If you could post a bit more of your code it could help troubleshooting. Likely overkill: for networking there is also a great package called DOTSNET available.
     
    Last edited: May 26, 2021
  6. JediNizar

    JediNizar

    Joined:
    Nov 13, 2016
    Posts:
    110
    I don't know if it cost more performance, but if you need to pass everything as a string, you can convert it to a json string and read it /decode it where ever you need it.

    I'm using something like that on my ASP.Net Core App to store information within a session

    JasonConvert.SerialzeObject() and JasonConvert.DeserialzeObject<T>()
     
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,580
    You a will pay cost in bandwidth.
    Parsing cost will be insignificant in such case. There is also potential GC.
     
  8. goodnewsjimdotcom

    goodnewsjimdotcom

    Joined:
    May 24, 2017
    Posts:
    342
    Antypodish, actually every single byte I can save is very important in networking since it multiplies over many. But that's not my question here.

    I got: Rotation entRot = entityManager.GetComponentData<Rotation>(playerEntity);

    But when I try and set it to another entity via:

    entityManager.SetComponentData(myShipObject, new Rotation { Value = quaternion.EulerXYZ(entRot.Value.x, entRot.Value.y entRot.Value.z);

    The rotation is spastic and not the same as the main entity to be duplicated...

    Also I have no clue to how to get an Entities' velcoity. Setting the rotation based on one entity through xyz and getting an entity's velocity is my actual question... Sorry for obscuring it why I needed xyz and couldn't just pass Rotation entRot directly...
     
  9. BrendonSmuts

    BrendonSmuts

    Joined:
    Jun 12, 2017
    Posts:
    86
    Pretty sure the Rotation component is a quaternion and not Euler. You are using the raw quaternion XYZ to create one from Euler XYZ, which they are not.
     
  10. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    436
    @goodnewsjimdotcom

    BrendonSmuts is right you can't use X,Y,Z components of entRot.Value, those are part of quaternion. You can just do Value = entRot.Value.
    About velocity, how do you apply velocity to your entity? Do you have some component or you are using ECS physics?
     
  11. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,580
    You can not do that, for reason as other said above. You try apply quaternion into Euler. You can not just ig ore W component. You can set entRot however, directly in set component, as long is Rotation component. Or otherwise set as quaternion value.

    Velocity is also difference between current and last postion. Just be aware of potential fluctuation. Depending on solution. You will need delta time. Or if use unity physics, get velocity from rigid body.