Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
2
Question by wijesijp · Jan 21, 2013 at 08:26 AM · addforce

Addforce to go a specific distance and height

I have a rigidbody sphere with mass 1 and use gravity set to true i wanted to add force to the sphere so that it goes distance d and height h (a parabolic path)

Is there an equation to find add force vector ?

Comment
Add comment · Show 3
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 Scribe · Jan 21, 2013 at 10:16 AM 0
Share

will the starting speed of the sphere always be 0? is this in 3D space or from a 2D point of view? and if its in 3D in which direction would you define distance (displacement really as distance doesn't have direction!)

Thanks, Scribe

avatar image Fattie · Jan 21, 2013 at 10:31 AM 0
Share

this is a purely general math or game question

this site is only for very specific questions about Unity

go here

http://en.wikipedia.org/wiki/Parabola

or ask on stackoverflow, or gamedev, for many answers instantly

avatar image wijesijp · Jan 21, 2013 at 10:53 AM 0
Share

yes starting speed always 0

the game is in 3D space but 2D view (orthographic)

I am using x (distance) y (height)

2 Replies

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

Answer by aldonaletto · Jan 21, 2013 at 12:26 PM

This is the so-called "ballistic trajectory": an initial velocity is given to the projectile at certain elevation angle, and physics does the rest. The mass doesn't matter, only the direction and magnitude of the initial velocity, thus it's better to calculate the velocity vector and assign it directly to rigidbody.velocity.
You can split the velocity into two components, vertical and horizontal. The vertical speed is "consumed" linearly by the gravity, and the maximum height is reached when it becomes zero. This allows us to calculate the initial vertical speed:

     vertSpeed = Mathf.Sqrt(2 * g * maxHeight);

The horizontal velocity doesn't change during the trajectory (we're not taking air resistance into account), thus the distance can be calculated by:

     horSpeed = maxDistance / totalTime;

The total time depends on the height of the target: if it's at the same height as the launching point, the total time will be equal to twice the time taken to reach the max height:

     totalTime = 2 * vertSpeed / g;

If in your case the vertical and horizontal directions are the axes Y and X, you can calculate the rigidbody velocity as follows:

     var g = Physics.gravity.magnitude; // get the gravity value
     var vSpeed = Mathf.Sqrt(2 * g * maxHeight); // calculate the vertical speed
     var totalTime = 2 * vertSpeed / g; // calculate the total time
     var hSpeed = maxDistance / totalTime; // calculate the horizontal speed
     rigidbody.velocity = Vector3(hSpeed, vSpeed, 0); // launch the projectile!





Comment
Add comment · Show 1 · 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 Gentleman Johnson · Oct 04, 2014 at 05:19 PM 0
Share

Thanks! This help me a ton in my 2d game since I needed "bounce" the player to a specific height. In case it is of any help to anyone, with a variation on the vSpeed formula I used:

 float velocityY = $$anonymous$$athf.Sqrt(
     2f * -Physics2D.gravity.y * 
    rigidbody2D.gravityScale * verticalDistance);
 rigidbody2D.velocity = new Vector2(0, velocityY);

avatar image
0

Answer by wijesijp · Jan 22, 2013 at 04:43 AM

Aldo:

I think i got some idea why my projectile is not working. When I use your equation for a single rigidbody it is working well.

What i have is 3 rigid bodies connected together with HingeJoints. When I apply your equation to one of the links it is moving very short distance.

What should I do in a situation like this?

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 sdgd · Jan 22, 2013 at 05:01 AM 0
Share

well I have some experience with force

it will never stop except draw slows them down and the mass of the other 2 components HingeJoints might be applied to the 1 with the script with force

I probably don't understand totally how your project goes and probably most of my info isn't true to the fact I don't understand exactly what's happening in your project

avatar image wijesijp · Jan 22, 2013 at 05:17 AM 0
Share

i guess other 2 objects are slowing the object where i apply the velocity. And the object does not reach set height or distance.

i guess if we can calculate the velocity for full object and applied to 1st link in chain it might do the trick.

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

13 People are following this question.

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

Related Questions

collider not working when apply "addForce" high enough 2 Answers

addforce + persp camera movement weirdness 1 Answer

Rolling Ball Code Isn't Working 6 Answers

OnTriggerEnter AddForce 1 Answer

Raycast Hit Question 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