So I have a bullet that I want to curve in an arc by first going down in an downward arc but the continue upwards.
It is a bubble in the water shot from a bubble gun. I have goten it to start curving upwards with negativ gravity but I first want it to curve down be for rising to the surfis.
I do not know if I am describing this well but there you have it.
There are several ways to do what you’re looking for. Here’s one that’s really extensible and gives you quite a bit of freedom to experiment and change the path of your projectile quickly and easily. I would create a script on your projectile object. Something like this…
public class ProjectilePath : MonoBehaviour {
//These curves which you'll edit in the inspector will define the path of your projectile. There are 2 separate curves, one for the x component and one for the y.
public AnimationCurve yPath;
public AnimationCurve xPath;
//This will allow you to define how far the projectile should travel.
public Vector2 projectileExtents;
//duration lets you determine how long the projectile should take to reach the extents.
public float duration = 2f;
//This will be set in Start to the inverse of duration giving us the correct amount to increment to ensure your duration is accurate
private float rate;
//We'll reuse our newPosition vector each update so we're not creating garbage for the collector everyframe.
private Vector2 newPosition = Vector2.zero;
//This will be the objects position at instantiation time
private Vector2 offset;
//The age variable allows us to calculate how long the projectile has been alive. We'll use it to find out where along each curve we should be. It starts at 0 and we'll increment it every frame by Time.deltaTime * rate.
private float age = 0f;
void Start() {
rate = 1 / duration;
offset = transform.position;
}
void Update () {
age += Time.deltaTime * rate;
//These 2 lines will send our age variable to each curve and return the value at age. Then we multiply that value by the extents for each component to get where our projectile should be every frame.
newPosition.x = xPath.Evaluate (age) * projectileExtents.x;
newPosition.y = yPath.Evaluate (age) * projectileExtents.y;
//Add back in it's starting position so it doesn't start at the origin everytime.
newPosition += offset;
//Update the projectile's position.
transform.position = newPosition;
}
}
In the inspector, you’ll have 2 animation curves that you can edit. These will define how your projectile will move in both the x and y. The extents variable will be how far in the X and how far in the Y the projectile should travel. The duration is how long it should take, in seconds, to reach the extents. You can also experiment and play around with different settings in the animation curves like loop, ping pong etc… If you want to change the path of the projectile, or if you wanted a different path, there’s no new coding, just edit the curves in the inspector and play with the duration and extents.
I wrote this code in the browser window, so I haven’t tested it, so if anything’s giving you trouble, or you have any other questions, let me know.