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 binga30 · Jun 18, 2012 at 11:58 AM · javascriptphysicstransformlaunch

how to launch a ball?

Hi, I'm new here and also new to unity. I have done some web development and know a little javascript so I think that will be my language of choice.

Basically I want to create a game where when you press the button, a ball will travel on a trajectory and then hit the ground and roll to a stop.

The keys to this is that the ball slows down when it hits the ground and doesn't roll too far.

So far I have created a sphere on my terrain. I have been searching for how to launch projectiles and moving objects to a position but I have not found any useful code.

If someone could point me in the right direction or offer a sample of what my script will need to look like to achieve this I would be very greatful.

Thanks

Steve

Comment
Add comment · Show 2
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 bubblegumsoldier · Jun 18, 2012 at 12:03 PM 0
Share

If I understood you right the main point is the slowing down of the ball?

Just attach a rigidbody to the ball and the rest should work... For instantiating an Object look here:

http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html

avatar image binga30 · Jun 18, 2012 at 12:17 PM 0
Share

I think I might have replied in the answer box lol.

Anyway yeh the slowing down is important, as if the effects of the terrain when the ball lands, ie- the ball would roll down a slope.

However my first step is to get the ball launching into the air on a trajectory when you press click a button or press a key.

Thanks for your link, I am not familiar with the instantiate methods. Is this how you make projectiles fly through the air to a designated position?

Thanks again

2 Replies

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

Answer by whydoidoit · Jun 18, 2012 at 12:21 PM

  • Attach a rigidbody and a sphere collider to your ball.

  • Attach a collider to the ground.

  • choose a physics material for your ground and your Ball's colliders. This affects bounciness, friction etc. there is a standard asset package with some basic materials.

  • when you get your key press, use rigidbody.AddForce to propel your ball into the air.

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 binga30 · Jun 19, 2012 at 07:34 AM 0
Share

$$anonymous$$ike thanks for your answer. I must admit I am a little more newb than I would like. Using your solution, how would I specify the trajectory for the ball. It would reach its highest point at about 80% of its distance. Would i be asking too much to get a sample of what the script would look like? Thanks again

avatar image binga30 · Jul 09, 2012 at 10:08 AM 0
Share

Although rather brief on details this answer got me what i wanted.

avatar image
0

Answer by Mizuho · Jun 19, 2012 at 08:08 AM

You're gonna need to use some Physics for this (hopefully I'm not a Unity moron, and Unity actually has realistic Physics calculations). Assuming we have that set relation of 80%, here's what it looks like (in C#, because I don't write in Javascript on Unity since it's different from the ECMA standard):

 float y = 50;
 float dX = y * 0.8f;

 // y = (vNotY ^ 2) / 2g
 float vNotY = y * 2 * Physics.gravity.y;
 vNotY = Mathf.Sqrt(vNotY);

 // t = 2 * v0y / g
 float time = 2 * vNotY / Physics.gravity.y;

 // v = d / t
 float vX = dX / time;

 Vector3 force = new Vector3(vX, vNotY, 0);
 ball.rigidbody.AddForce(force);

I'm HOPING this works. I haven't tested this and I haven't worked with the Physics part of Unity (so I can't guarantee what force in AddForce() actually represents. I also haven't taken Physics in a few years (this actually took forever because I suck at trajectories now T_T).

Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Wrong if command 3 Answers

I am having a problem trying to simulate football(soccer) physics 2 Answers

Item Icon goes to origin, not set transform. (JAVASCRIPT) 0 Answers

Rotating An Object On Its Axis Once 3 Answers

stop vehicle from moving through walls 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