- Home /
With power and angle, what is the best way to create a parabolic arc?
I'm creating a custom controller for a Launch-type game. My opening sequence is already scripted and produces two variables: Angle, and Power. I do not want to use this with a Rigidbody and physics but create the actual parabolic arc and movement myself - I'm targeting lower-end devices. What kind of formula, or script would be best for this method?
EDIT: My goal is to do this using transform. I've already set up the smoothing in order to handle the movement.
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smooth);
Answer by nesis · Feb 04, 2014 at 01:50 AM
I'm not sure if you know this, but if you have a non-kinematic Rigidbody and set its velocity and let gravity influence it, it'll follow a parabolic arc. There are simple, specific equations you can use to calculate a projectile's trajectory.
For a slightly more lightweight solution, could alternatively define a class similar to this (in C#):
public class MyProjectile : MonoBehaviour {
public Vector3 gravity = new Vector3(0f,-9.81f,0f); //could instead use Physics.gravity
private Vector3 velocity;
public void Awake() {
enabled = false; //start off disabled
}
public void FixedUpdate() {
//apply gravity
velocity += gravity*Time.fixedDeltaTime;
/*if you're wanting to collide with things, here would
be a good place to perform physics sweep tests / raycasts
as needed to know where to stop your object and make it
bounce*/
transform.position += velocity*Time.fixedDeltaTime;
}
//call this from whatever script / method you want to
//initialise a projectile from. Eg, if done in another C#
//script, where projectilePrefab is a prefab you've made
//with this script attached:
//GameObject o = (GameObject)Instantiate(projectilePrefab);
//MyProjectile p = o.GetComponent<MyProjectile>();
//if (p) {
// p.Initialise(somePower,someQuaternionAngle);
//}
public void Initialise(float power, Quaternion angle) {
//here, power is speed (ie the magnitude of velocity)
velocity = angle * Vector3.forward * power;
enabled = true; //enable this script's update methods once we've initialised
}
}