x


Before updating to Windows 10 you may need to return your Pro license. More information here

Projectile motion with angle and force in one direction

EDIT

Figured it all out.

ORIGINAL

I am trying to make a simple game where there is a GUI with two text fields and a button ... player enters in angle and force into the two text areas and presses the button to launch an object forward (in x-direction) with player defined angle and force.

I running into a few problems. First off here is what I have so far:

var force:int; var angle:int; //var prefabBanana:Transform; var temp:int; var temptwo:int; var prefabBanana : GameObject; //var gorilla : GameObject; var elevationAngle : Vector3; var throwForce : int;

function OnGUI () {

     if(GUI.Button(Rect(10, 50, 80, 20), "Launch!!")) {
     Launch();
     }

     var text = GUI.TextField(Rect(10, 10, 50, 20), angle.ToString());
     var texttwo = GUI.TextField(Rect(10, 30, 50, 20), force.ToString());

     if (int.TryParse(text, temp)){
     angle = Mathf.Clamp(0, temp, 360);
     }

     else if (text == "") angle = 0;

     if (int.TryParse(texttwo, temptwo)){
     force = Mathf.Clamp(0, temptwo, 360);
     }

     else if (texttwo == "") force = 0;

}

function Launch() {

 elevationAngle = Vector3(angle,0,0);
 throwForce = force;

 var banana : GameObject = Instantiate(prefabBanana, transform.position, Quaternion.identity);

 var elevation : Vector3 = Quaternion.Euler(elevationAngle) * transform.forward;

 new banana.rigidbody.AddForce(elevation * throwForce);

     }

I getting this error when I attempt to launch the object:

"InvalidCastException: Cannot cast from source type to destination type. fire.Launch () (at Assets/fire.js:40) fire.OnGUI () (at Assets/fire.js:15)

Nothing happens, the object is never launched, but the game runs fine and the script compiles fine.

Any ideas on what is wrong? Sorry I am not the best programmer out there!!

Thanks!!

more ▼

asked Apr 07, 2011 at 07:44 PM

avatar image

SlapnutsGT
3 6 4 8

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

1 answer: sort voted first

In your Launch() function, try declaring your banana on a separate line, and defining it on the next line.

var banana : GameObject;
banana = Instantiate(prefabBanana, transform.position, Quaternion.identity);

I'm not a JavaScript developer. So if this works, I cannot explain why it needs to be done like that. Hopefully a JS guru can come explain it. =)

more ▼

answered Apr 07, 2011 at 08:01 PM

avatar image

KeithK
917 7 7 21

Not quite ...

I am starting to think my problem is with the object I have it attached to.

Apr 07, 2011 at 08:19 PM SlapnutsGT

"declaring your banana" lol

Apr 07, 2011 at 10:52 PM ThomasQ
(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:

x8171
x6703
x3870
x422
x152

asked: Apr 07, 2011 at 07:44 PM

Seen: 2830 times

Last Updated: Apr 07, 2011 at 11:44 PM