x


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); }

more ▼

asked May 23 '11 at 03:49 PM

thatbigguy gravatar image

thatbigguy
1 5 5 5

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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) }
more ▼

answered May 23 '11 at 03:57 PM

Joshua gravatar image

Joshua
6.5k 19 25 72

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

May 23 '11 at 04:07 PM thatbigguy

So what is your question? Alter the angle.

May 23 '11 at 04:09 PM Joshua

My question is how can I alter the angle.

May 23 '11 at 04:10 PM thatbigguy

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..

May 23 '11 at 04:15 PM Joshua

This is one of the most confusing exchanges I've ever read. Make a turret with a turret script. Make a bullet with a bullet script. The turret will make bullets.

Simple.

May 23 '11 at 07:07 PM flaviusxvii
(comments are locked)
10|3000 characters needed characters left

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.

http://itween.pixelplacement.com/index.php

more ▼

answered May 23 '11 at 11:09 PM

juanelo gravatar image

juanelo
11 7 7 9

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1948
x1071
x481
x300

asked: May 23 '11 at 03:49 PM

Seen: 2269 times

Last Updated: May 23 '11 at 11:09 PM