- Home /
How can I make a game object move in parabolic motion as if it were under gravity?
I am modifying a shooting game where instead of the bullet traveling straight up, I want it to act under gravity and move in a parabolic motion. Right now, I have the bullet moving in a straight line with a positive slope. Is there any way I can change my code for the bullet to make it behave this way? My game is in 2D and also uses the X-axis and Y-axis. Here is the bulletScipt:
var bulletSpeed : int;
var bulletAngle: float;
var explosion : Transform;
useGravity = true;
function FixedUpdate () { amtToMove = bulletSpeed * Time.deltaTime;
transform.Translate(Vector3.right + Vector3.up * amtToMove);
if(transform.position.y >=6.7){ Destroy(gameObject); }
if(transform.position.y <= -5){ Destroy(gameObject); } }
function OnTriggerEnter(otherObject: Collider){
if(otherObject.gameObject.tag == "enemy"){
playerScript.playerScore += 50;
otherObject.gameObject.transform.position.y = 7;
otherObject.gameObject.transform.position.x = Random.Range(-6,6);
var tempExplosion: Transform;
tempExplosion = Instantiate(explosion, transform.position, transform.rotation);
Destroy(gameObject);
} }
function OnGUI() { GUI.Label(Rect(10,70,200,50),"Bullet Velocity: "+ bulletSpeed);
GUI.Label(Rect(10,90,200,50),"Bullet Angle: "+ bulletAngle); }
Answer by Joshua · May 23, 2011 at 03:57 PM
Either give the bullet GameObject a Rigidbody with gravity enabled or script your own gravity;
var startPosition : Vector3;
var startTime : float;
function Start () { startPosition = transform.position; startTime = Time.time; }
function Update () { transform.position.y = startPosition - 4.905 * (Time.time-startTime)*(Time.time-startTime) }
I was able to give the bullet a Rigidbody and enable gravity and when i fire, it does act under gravity. However, I want to control the angle (which can be controlled with the motion of the mouse) at which it is fired as well in order for the trajectory to be more parabolic
Right but this is the script controlling the bullet, to change where you instantiate it I assume you'd have to alter something in the turret's script. Check this out: http://www.youtube.com/watch?v=TfRhQO5PcuQ.
But basically you just want to alter the angle of the object that shoots.. can't make it any simpler then that..
This is one of the most confusing exchanges I've ever read. $$anonymous$$ake a turret with a turret script. $$anonymous$$ake a bullet with a bullet script. The turret will make bullets.
Simple.
Answer by juanelo · May 23, 2011 at 11:09 PM
I believe the iTween library has function for handling that type of animation directly, without having to delve into the math yourself. Might come in handy.