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
0
Question by monremobile · Aug 26, 2017 at 11:40 PM · physicsprojectileballpredict

How to predict where the ball will fall?

I'm making a 3D basketball game. CPU players need to catch the rebound.

Knowing the velocity and direction of the ball...

  • How can I calculate where it will fall in an imaginary plane?

    ( above players' heads or... when ball.tranform.position.y=3f; )

  • And when? (time to reach the hit point)


...so CPU player can jump to the ball at the right time.

alt text

balon.jpg (17.1 kB)
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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Eno-Khaon · Aug 27, 2017 at 02:57 AM

For my examples, I'll be reusing a little content I previously included here.

Because you already have the velocity of the ball at any given time, you can create a prediction of where the ball will be by stepping through Unity's general physics steps.

To note, the general implementations of drag and velocity (per physics update) in Unity are:

 rigidbodyDrag = Mathf.Clamp01(1.0f - (rb.drag * Time.fixedDeltaTime)); // Per FixedUpdate()
 // This means that if your drag is equal to the framerate of FixedUpdate(), your object will lose 100% of its speed every frame
 
 velocityPerFrame = lastFrameVelocity + (Physics.gravity * Time.fixedDeltaTime);
 velocityPerFrame *= rigidbodyDrag;
 positionPerFrame += (velocityPerFrame * Time.fixedDeltaTime);

With those taken into consideration, you'll want to create either a list or an array (with a maximum time estimate factored in as a safety), then populate it (at least) until your goal is met.

 // C#
 // Example maximum of 5 seconds
 int maxIterations = Mathf.RoundToInt(5.0f / Time.fixedDeltaTime);
 Vector3 pos = ball.position;
 Vector3 vel = ballRigidbody.velocity;
 float drag = Mathf.Clamp01(1.0f - (ballRigidbody.drag * Time.fixedDeltaTime));
 positionList.Add(pos);
 float elapsedTime = 0.0f;
 
 for(int i = 0; i < maxIterations; i++)
 {
     vel = vel + (Physics.gravity * Time.fixedDeltaTime)
     vel *= drag;
     pos += vel * Time.fixedDeltaTime;
     elapsedTime += Time.fixedDeltaTime;
     positionList.Add(pos);
     if(pos.y - ballRadius < targetHeight && Vector3.Dot(vel, Vector3.down) > 0)
     {
         // The ball must be moving downward to fit the second criteria
         break;
     }
 }

Using this approach, the prediction would only need to be updated when something directly modifies the ball's current trajectory (or if the ball was rocketing into space and would take longer to return than the safety timeout).

Edit: Added in time factor

Comment
Add comment · Show 3 · 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 gambitslacklines · Aug 30, 2017 at 08:59 PM 0
Share

Can i ask how i could reverse this?

I tried your script and it works fine for falling, but i would love to use it while rising, to get the time which my character needs from start of jump, till velocity0(fallpoint).

I need the time cause i want to use it as a var in mecanim to adjust animation speeds depending on the jump and fall down time of my character.

Would be great if you could help out.

avatar image Eno-Khaon gambitslacklines · Sep 02, 2017 at 07:57 PM 0
Share

I'm not entirely certain what you mean when you refer to "while rising" in this case.

If you're looking for the time when the ball is at any given point, you can save elapsedTime (in my example above) as its own variable per predicted physics "frame", like the positionList List<> does. By extension, you could ins$$anonymous$$d choose to create a class or struct to hold per-predicted frame data, such as position, time and velocity, then you'll know when the ball will reach any given point and can more easily adapt additional conditions, such as when a player might not be able to reach or jump as high as another one can.

If you mean to be able to estimate ahead of time how long it would take the ball to go from Point A to Point B, you can base that on either of the angle- or speed-based variations on prediction I described here.

avatar image unity_BEDD0EE267DB4B14EF73 · Nov 11, 2021 at 06:03 AM 0
Share

Hi, sorry for bumping such an old post but I have been struggling to find what I'm looking for on the internet, and your post's on this forum have been the closest to what I'm looking for. I'm not sure whether I've implemented the code incorrectly or the code doesn't work but I haven't had any luck in getting it too work.

My goal is to calculate the optimum world position in which I need to aim at in order for my ridged body projectile to hit, given the ridged bodies velocity and drag. I do not need to account for the horizontal direction as my target is stationary and so is the source position (3D space), but the source position will be changing height and distance from the target object.

(after some further debugging it turns out it was my fault I have managed to resolve my issue thank you)

avatar image
0

Answer by TheSOULDev · Aug 27, 2017 at 12:43 AM

You will have to record the velocity at which the ball is shot - taking into consideration the z component you can calculate the time it will be in the air. Since you have the time and the x component of the shot, you only need to multiply the time with the x component of the ball's velocity to find out at what distance the ball is going to be from the position it was shot at. Finally, by looking at the x and y component of the velocity, you can create a Vector2 that will give you the direction of the shot. You will need to normalize the Vector2 and multiply it by the distance, and that will give you a Vector2 from the position the ball was shot to the position the ball is going to land. Once you have that Vector2, just add the original position the ball was shot from to it, and you'll get the X and Y coordinates of the place your AI has to go to.

This is all fine and dandy, but honestly it would be way better if you just made your AI follow the X and Y coordinates of the ball, and if they can make it to the ball, cool, if not, well, a real player couldn't either. While it's precise to define an AI to get the ball, it is more realistic to make it follow the X and Y position of the ball like a real player would, although most pro basketball players have enough experience to make a good judgement on where the ball will fall anyways.

tl;dr you can use the x and y components of an angled shot from physics to calculate the time, the distance and the location of the point the ball will fall, or you could simply make your AI move with a velocity Vector3 equal to (ballposition.x - AIposition.x, ballposition.y - AIposition.y, AIposition.z).normalized times an arbitrary constant.

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 5billion · Nov 01, 2017 at 11:42 PM

I am creating a 3D basketball game also. Need answers too. Best answer for rebounding is Using 'Colliders as Triggers' https://www.youtube.com/watch?v=BYwkd80T5Zk https://unity3d.com/learn/tutorials/topics/physics/colliders-triggers In theory, one or invisible 'colliders' near backboard/rim space and invisible 'colliders' in key areas under basket where player rebounding most likely to happen. Two events, basketball in collider space triggers nearest player animation in rebounding collider space. In code calculate for speed, velocity, trajectory as in relation to collider triggers.

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 5billion · Nov 02, 2017 at 12:11 AM 0
Share

I am using character/mecanim. A Player shoots basketball animation. The basketball is parented to GameObject /hierarchy player hand(left or right) At point of release in Animation window, a marker is assigned to keyframe, that marker and basketball is referenced in C# script. $$anonymous$$arker = Throwball(). In script basketball (rigidbody) has transform direction and force in relation to player animation. Reference video "How to throw object" https://youtu.be/Qzgyq5Youd0

Want to revise script so that the basketball has Arc, velocity. and Target (basket/rim) empty GameOject/colliderTrigger. Base upon all the documentation / videos cannonball launch, and plugins I know its possible in Unity but How. Fairly new to coding.

avatar image
0

Answer by Feretro · Aug 27, 2021 at 09:44 AM

Hello! Sorry ... I'd need something similar but on a 2D knowing where the ball is going to fall on the X axsis... in a ArcadeVolleyball like game!

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 Eno-Khaon · Aug 29, 2021 at 07:39 PM 0
Share

As a general rule, you can typically expect all singular gravitational forces to only utilize a two-dimensional plane when no other external forces are being applied. One of the first steps in 3D trajectory calculations is constructing that plane. A 2D game can skip that phase altogether.

With that in $$anonymous$$d, part of the answer I gave here previously relates to finding all the points in the immediate future where the Rigidbody will travel through. Firing a Raycast in the direction of gravity (or even using raw Y value if all your ground is always perfectly flat) from each point along the way can let you know when and where the ball will reach the floor.

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

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

Related Questions

The Physics of Ball Spin in Tennis, Topspin and Backspin 3 Answers

Projectile(Arrow) angle during flight is not realistic 1 Answer

What is the best way to move a paddle accurately in a circle? 2 Answers

Arcing projectile using physics 0 Answers

Make a rolling ball always on ground without falling when reaching the edges of the map 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