Search Unity

[SOLVED] Make a smooth movement with twitchy data

Discussion in 'Scripting' started by Dylan56, May 24, 2018.

  1. Dylan56

    Dylan56

    Joined:
    Mar 9, 2015
    Posts:
    10
    Hi,

    I have a problem to make a smooth movement on my project.

    Here is the context : I have a object (a car) that receive its position (only x and z) from an external source (not from Unity). I have a component that retreive those coodrinates and send them to the car.

    I update my car position in a FixedUpdate() with a simple
    Code (CSharp):
    1. this.transform.position = new Vector3(x, this.transform.position.y, z);
    and here is how I receive data :


    As you can guess, my car also move by jolt.

    I tried to use a Vector3.Lerp and set the objective position each time we receive a new one (that are underline on my image), but I have some troubles.
    On my first try, Y is automatically calculate, as my car is moving on a road, but when I use Lerp, it will try to pass through the road. Then my car does barrel rolls and bounce of the road.

    If someone have some idea, I'm attentive !

    Thanks !
     
  2. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
    How often does car receive data? Is fixed update useful in this matter?
    Have you tried moving the car by an average over a few values (let's say 3 pairs of x and z)?
     
  3. Dylan56

    Dylan56

    Joined:
    Mar 9, 2015
    Posts:
    10
    Thanks for your answer,

    it receieves data from an Update, so about 60 times per second.
    I used a FixedUpdade more as a reflex than a reflexion (I always use FixedUpdate to move a Rigidbody). But you are right, as I don't use any force, I will use a simple Update.
    If you mean using a kind of Lerp on X and Z over time, no. I only tried with the full position. I will look deeper with that solution
     
  4. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
    I was thinking more along the update cycle of
    1. nextPosition += get position
    2. nextPosition += get position
    3. nextPosition += get position, Begin lerping to nextPosition over the next 3 frames
    4. goto 1.
     
  5. Dylan56

    Dylan56

    Joined:
    Mar 9, 2015
    Posts:
    10
    I'll be honnest, you lost me.
    With your solution (let's take an example in 2D), if my car is in (1, 2), and want to go to (0, 0), and receive this data during 6 frames.

    1. nextPosition = (0, 0) + (1, 2) = (1, 2)
    2. nextPotision = (2, 4)
    3. nextPotision = (3, 6), I'll go in the wrong direction here ?
     
  6. PiterQ70

    PiterQ70

    Joined:
    Mar 3, 2015
    Posts:
    59
    What network solution you use?
     
  7. Dylan56

    Dylan56

    Joined:
    Mar 9, 2015
    Posts:
    10
    I don't use any network, data are retrive from RTMaps. Unity load and run a diagram and get values from it
     
  8. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
    No.
    Due to vector sums you will, in your case, not only go in the correct direction, but also end up at the same location.
     
  9. PiterQ70

    PiterQ70

    Joined:
    Mar 3, 2015
    Posts:
    59
    Ahh, ok :) I'll think about it.
     
  10. Dylan56

    Dylan56

    Joined:
    Mar 9, 2015
    Posts:
    10
    I still don't understand how this can work. My car wants to go to (0, 0) and is placed in (1, 2), going to (3, 6) is not the right way ?

    BTW, I move my movement fonction to a simple Update an separate X, Z and the orientation, and it looks good. I move my car with 1 data late, in that way I can have the time to go to the objective.
     
  11. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
    You don't do all those operations in one update :D. I should have been clearer;
    You do them sequentially through 3 update cycles. Every 3rd cycle you set a new waypoint for the car. This waypoint is the sum of 3 movement vectors which you got the 3 update cycles.
     
  12. Dylan56

    Dylan56

    Joined:
    Mar 9, 2015
    Posts:
    10
    Ok ! I see what you mean now ! It's more like a prediction than the real coordinates. That sound interesting, I'll implement it to compare both solutions !

    Thanks !
     
unityunity