A Unity ID allows you to buy and/or subscribe to Unity products and services, shop in the Asset Store and participate
in the Unity community.
Discussion in 'Scripting' started by Lostwanderer1, Dec 3, 2019.
How would you create a curved trajectory path like the game cool goal?
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.
//make these the same values you'd set the fired object's position and velocity to
Vector3 position = whatever;
Vector3 velocity = whatever;
//this is where you'll store the simulated positions
List<Vector3> path = new List<Vector3>();
float duration = 3f; //however long you want to simulate
float timestep = Time.fixedDeltaTime;
for (float t=0f; t < duration; t += timestep)
velocity += Physics.gravity * timestep;
position += velocity * timestep;
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:
public LineRenderer lr; //assign in inspector
lr.positionCount = path.Count;
Make sure that the LineRenderer is set to use world positions, not local.
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?
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.