Search Unity

Remote controlled boat

Discussion in 'Physics' started by ArmandAnton, Apr 17, 2019.

  1. ArmandAnton

    ArmandAnton

    Joined:
    Mar 19, 2019
    Posts:
    4
    Hi everyone !

    I'm working on a personal project using Unity
    The goal of my project is to make move a boat, thanks to a network communication.

    I send via socket an object called "wave" and this object contains properties which will allow me to make move the boat.

    Here is how i processed to receive data via socket :

    Code (CSharp):
    1.  
    2. //Network.cs
    3. public static void ReceiveDataCustom()
    4.     {
    5.         print("Connecting to remote endpoint ... ");
    6.  
    7.         client = new UdpClient(port);
    8.         while (true)
    9.         {
    10.             try
    11.             {
    12.                 IPEndPoint anyIP = new IPEndPoint(IPAddress.Broadcast, port);
    13.                 print("Listening on " + port + " port ... ");
    14.                 data = client.Receive(ref anyIP);
    15.                 int size = data.Length;
    16.                 IntPtr ptr = Marshal.AllocHGlobal(size);
    17.                 Marshal.Copy(data, 0, ptr, size);
    18.                 wave = (waves)Marshal.PtrToStructure(ptr, typeof(waves));
    19.                 Marshal.FreeHGlobal(ptr);
    20.                 resetPos();
    21.             }
    22.             catch (Exception err)
    23.             {
    24.                 print("NOT CONNECTED: " + err.ToString());
    25.             }
    26.         }
    27.     }
    28.  
    As u can see, at the end of my method i call resetPos() method.
    It allows to set the starting position as current position,
    and the futur position is set to wave.y which is send via socket

    Code (CSharp):
    1.  
    2. //Network.cs
    3. public static Vector3 positionDeDepart;
    4. public static Vector3 positionDArrivee;
    5. static float t = 0F;
    6. static void resetPos()
    7.     {
    8.         positionDeDepart = positionDArrivee;
    9.         positionDArrivee.y = (float)wave.y;
    10.         t = 0F;
    11.     }
    12.  
    So now i get a data called "wave" with properties.
    Here is my properties :

    Code (CSharp):
    1.  
    2. public struct waves
    3. {
    4.     public double x { get; set; }
    5.     public double y { get;
    6.     public double z { get; set; }
    7.  
    8. }
    9.  
    Then now i need to apply this coordinates to my boat.

    Into the gameLoop Update() I the MoveTanker() method which allow to make my boat move.
    Code (CSharp):
    1.  
    2. //Network.cs
    3. static float tempsTotal = 0.0240F;
    4. //Boat.cs
    5. void MoveTanker()
    6.     {
    7.         t = Network.getT();
    8.         t += Time.deltaTime;
    9.         tempsTotal = Network.getTempsTotal();
    10.         transform.position = Vector3.Lerp(Network.getPositionDeDepart(), (Network.getPositionDArrivee()), t / tempsTotal);
    11.     }
    12.  
    Until now there is no problem !
    But ! As u saw it, i only make move my boat on the y axis, and If I try to make move my boat on the x axis to, it doesn't work, let me explain.

    When the boat move on Y axis the movement is linear, there is not any "lag", but if make it move on y axis AND x axis there is like some lag ...


    Is there a way to fix it ?
    Thanks all !
     
    Last edited: Apr 17, 2019