Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SlapnutsGT · Apr 07, 2011 at 07:44 PM · javascriptguiphysicsprojectilemotion

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by KeithK · Apr 07, 2011 at 08:01 PM

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SlapnutsGT · Apr 07, 2011 at 08:19 PM 0
Share

Not quite ...

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

avatar image ThomasQ · Apr 07, 2011 at 10:52 PM 0
Share

"declaring your banana" lol

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Is this a problem of unity 3 or i am doing something wrong. 1 Answer

Show "Score:5" on my screen, how? 1 Answer

A job task : Projectile Motion 0 Answers

Simple projectile motion with wind 0 Answers

Finding angle of angular motion 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges