Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
3
Question by nekologic · Mar 14, 2010 at 03:34 PM · physicsaddforce

Apply A Force to Reach a Specific Height

I want to apply a force upwards against gravity such that an object reaches a specific height.

Imagine a ball at rest at position (0,0,0), I would like to bounce the ball upwards such that the highest point it reaches will be (0,3,0) before gravity takes over and pulls it back down.

It is necessary that this looks like a natural motion, and I want to be able to change the height. So that I can bounce the ball to 3, 5, 15, or whatever value I choose to set. I'm not overly familiar with physics. What is the best way to achieve this?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by Jason_DB · Mar 14, 2010 at 08:42 PM

Assuming that the physics in Unity work like real life, then this equation should help (note this is not code, just physics equations)

1) h = (vy)t + 0.5(g)t^2 which becomes: 2) vy = (h - 0.5(g)t^2) / t

variables: h = height (in meters) vy = the initial y velocity of the object when fired (the upward velocity in m/s) t = time (in seconds) g= the force of gravity (which should be -9.81 m/s^2)

The maximum height will be reached after this many seconds

3) t = (0- vy) / g

So if you plug in what you get from equation (3) into equation (2) along with your desired height you can get the required y velocity of your projectile. In case you aren't sure what to do with that velocity you can just use some trigometry to get the overall velocity:

alt text

This velocity doesn't necessarily give you the force, though, since Force is based on object mass and acceleration, so I don't know the best way to apply this speed. Also I'm sorry if this isn't the best explanation.

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
avatar image
3

Answer by Lipis · Mar 14, 2010 at 10:49 PM

Here is a little example of how you can play with the AddForce and check the actual maximum height during run-time, while you can check DastardlyBanana's answer for the physics behind it.

In order to see it in action:

  • Create new Sphere
  • Put it on the ground (the initial position of the Sphere is essential for reseting the experiment)
  • Add a Physics->Rigidbody
  • Change the Material to Rubber
  • Attach this JavaScript to it

private var force: String = "100"; private var mass: String = "1.0"; private var time: float = 0; private var maxHeight: float = 0; private var startPosition: Vector3;

function Start() { startPosition = transform.position; resetObj(); }

function Update () { time += Time.deltaTime; if (maxHeight < (transform.position.y - startPosition.y)) { maxHeight = transform.position.y - startPosition.y; time = 0; } if (time > 5) { maxHeight = 0; } }

function OnGUI () { GUI.Box(Rect(16, 16, 60, 24), "Force Y:"); GUI.Box(Rect(16, 48, 60, 24), "Mass:"); force = GUI.TextField(Rect(80, 16, 64, 24), force); mass = GUI.TextField(Rect(80, 48, 64, 24), mass); if (GUI.Button(Rect(16, 80, 128, 24), "Apply Force!")) { rigidbody.mass = parseFloat(mass); rigidbody.AddForce(Vector3.up * parseInt(force)); } if (GUI.Button(Rect(16, 112, 128, 24), "Reset")) { resetObj(); } GUI.Box(Rect(16, 144, 128, 24), "Max Height: " + maxHeight.ToString("f4") + "m"); }

function resetObj() { maxHeight = 0; rigidbody.velocity = Vector3.zero; rigidbody.angularVelocity = Vector3.zero; transform.position = startPosition; rigidbody.Sleep();
}

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
avatar image
0

Answer by Dardanelles · Oct 19, 2014 at 11:18 PM

Hello Lipis,

It definetely worked amazing. I am new on coding. I understand some part of the code you provided but i can't some of them... can you explain each line of code for what purpose they are there ?. In the end it just worked perfectly.

Thanks you alot once again.

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

1 Person is following this question.

avatar image

Related Questions

How to create normal physics reactions when collision is caused by a GameObject controlled by mouse position 0 Answers

Ball physics help 2 Answers

Jump Height Different On Android 0 Answers

Recieving a nullpointerexception while following the Unity Instantiation tutorial 1 Answer

move 2d character affected by physics with velocity and/or add force 2 Answers


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