Search Unity

Curved trajectory path

Discussion in 'Scripting' started by Lostwanderer1, Dec 3, 2019.

  1. Lostwanderer1


    Mar 11, 2019
    How would you create a curved trajectory path like the game cool goal?
  2. StarManta


    Oct 23, 2006
    Basically the two things you need to do are:
    1) Simulate the path
    2) Draw the path you've simulated.

    For simulating the path, most likely you just want to simulate the one object by writing your own miniature physics loop. This won't do any collisions,bouncing, etc, but it's much easier (and faster) than attempting to set up a loop that would simulate those things.
    Code (csharp):
    1.  //make these the same values you'd set the fired object's position and velocity to
    2. Vector3 position = whatever;
    3. Vector3 velocity = whatever;
    4. //this is where you'll store the simulated positions
    5. List<Vector3> path = new List<Vector3>();
    7. float duration = 3f; //however long you want to simulate
    8. float timestep = Time.fixedDeltaTime;
    9. for (float t=0f; t < duration; t += timestep)
    10. {
    11.    velocity += Physics.gravity * timestep;
    12.    position += velocity * timestep;
    13.    path.Add(position);
    14. }
    This will get you pretty close to what Unity physics would do assuming the fired object doesn't collide with anything or have any drag. (It's not 100% accurate because Unity physics adds velocity over the course of the physics timestep which this doesn't, but the math for that is much more complicated and this gets you close.) If desired, you could add a simple raycast to this loop to break out of the loop if you hit a surface.

    (Note: If you want the line to be 100% accurate the easiest thing to do is to use the stored positions here and force the object's position/velocity to match them until a collision happens.)

    So now you have a list of points stored into "path", we can move on to step 2, drawing them. There's a ton of ways you can do this, such as particle systems, LineRenderer, a series of dot sprites, etc. Here's an example for LineRenderer:
    Code (csharp):
    1. public LineRenderer lr; //assign in inspector
    3. ...
    5. lr.positionCount = path.Count;
    6. lr.SetPositions(path.ToArray() );
    Make sure that the LineRenderer is set to use world positions, not local.
  3. Lostwanderer1


    Mar 11, 2019
    Can you explain in a little detail, if i am to add the script on a ball then what would be the code without considering the physics forces?
  4. calpolican


    Feb 2, 2015
    You don't really need simulation and is not really a difficult task either.
    It kinds of depend on how you want the player to control the curve. You can use any type of curve, like for example a sin function. But for your case I think what will make it simpler to control is a quadratic brezier curve. It's pretty intuitive, because it'll give you 3 points. The first point is the place where the ball will start, the second one is the place where you want the ball to finish, and the second one is a point that will define the curve depending on how far or close it is to the mid point (see the image on the video cover to see the 3 points).
    There are probably hundreds of free assets to do brezier curves in unity. There may also be code you can copy paste to make a brezier quedratic curve. If instead you want to learn something, you can refer to this tutorial, it really kind of make it look more complicate than it is (breziere curves are just based in lerps, nothing fancy), but anyway, here it is if you want it:

    Any tutorial on brezier curves will do.