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

Smooth interpolation between two transforms (Position and rotation/direction)

Discussion in 'Scripting' started by Mixir, Oct 5, 2014.

  1. Mixir

    Mixir

    Joined:
    Oct 5, 2014
    Posts:
    3
    I'm trying to interpole an object nicely between certain checkpoints (transforms) in Unity. These checkpoints are supposed to represent the position and direction the object should be facing when it arrives.

    I have been trying to make it a smooth transition between two transforms where the object moves in an arc between the points, but the effects were more 'curly' than i was hoping to generate.

    I also found a spline controller script where i messed around with for a bit, but it also generated even more 'curly' arcs than I need.

    To demonstrate what i want i made two simply images that demonstrate what i want.

    The first image shows what kind of arcs i am hoping to generate, while the second one shows a complete path of what i want to generate, and a comparison of the same path which i got with the spline controller, an effect that i do not want. Pictures aren't perfect, made them quick and dirty, but they should have no problem getting the point across.

    Image 1:


    Image 2:


    Knowing myself i'm probably thinking way to complicated and is the solution probably very easy, but right now i'm stuck on this.

    Any help fixing my problem would be appreciated a lot!
     
  2. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    Vector3.Lerp() and Vector3.SmoothDamp() are what you're searching for.
    They would however, not create those arcs.
    To get this 'overshoot', I'd probably Lerp the object's velocity as opposed to its position. That way the object's momentum would take care of the rest.
    The behavior would be adjustable by tweaking the mass/drag/friction of the object.

    It's only my first idea, from the top of my head. There might be an easier way to do that.
     
  3. Mixir

    Mixir

    Joined:
    Oct 5, 2014
    Posts:
    3
    Thanks! That sounds doable. Would you (or anyone else) mind to give me a code example? That would be greatly appreciated! I'm currently not on my desktop where i have unity installed, so i can't actually test it right now. Would be awesome if i could continue with what i was working on tomorrow!
     
    Last edited: Oct 5, 2014
  4. Mixir

    Mixir

    Joined:
    Oct 5, 2014
    Posts:
    3
    I've tried to use Snipers method, but didn't really manage to get it work the way i was hoping for, probably an error on my side.

    Still looking for a solution or a code example of Snipers method. Any help would be greatly appreciated!
     
  5. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    I'll quickly make an example once I'm back from University.
     
  6. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    Sooooooo. Here we go:
    Download and keep it. It won't be there forever.

    It's a sample project with a proof of concept implementation. The implementation has it's issues and needs more work, but it works good enough to start with.

    How it works:
    The object is moved from A to B in a straight line and, after it reaches the waypoint, makes a turn to go to the next one. No snapping, a smooth curve. The code should be pretty self-explanatory since it's pretty much boils down to 5 lines (including waypoint logic).

    Problem:
    Sharp turns can result in the object missing the waypoint. In this case it will overshoot too far, miss, then turn around to hit it.
    It pretty much behaves like in the lower image (the part how it's not supposed to), if the waypoints are in a certain constellation.

    Solution(s):
    1. Make the waypoints bigger (the invisible part - be it colliders or a distance check), so that it's more forgiving and pinpoint accuracy isn't required anymore.
    2. Chain multiple waypoints close together, to have more control over the curve.
    3. Play with the variables (damp, speed and rigidbody-drag).
    4. To get the behavior for straight lines right, implement a second type of waypoints. They store the position of their partner and set the velocity of the object using that vector. That way you'll get a straight line.
    5. Make it intelligent.*

    *Instead of just going for the next waypoint, you could make the following waypoints influence the movement direction. That way the object will ease the curve, so the waypoint that comes afterwards is reached easier.
    I leave that to you and your linear algebra skills. ;)