- Home /
How to get force from a rigidbody2D?
As the title explains, I need to know how to get the force from an rigid body object, I have already applied the force and the object moves through the applied force. I need to apply the force received from the rigid body to a different game object script. Thanks.
EDIT 01: I have uploaded the image for better clarity. 
You should let the Unity's physics engine handle that, after all that's what it is for. Unless the two physics object don't really come in contact, then you have a few options to deter$$anonymous$$e the force the rigid body is applying, if at all. Are you constantly applying for on the former object? Only then will it be applying force. The equation is Force = $$anonymous$$ass x Acceleration, where acceleration is delta velocity over time. You could also just save the constant force as a variable and pass that. Can you give more details?
@lordlycastle: Thanks. Well, I have a game object fruit which will be thrown from the slingshot once the fruit is been thrown it will follow a path, which is bezier spline path. Am using a rigidbody force (similar to angry birds) to throw the fruit, I want the force to be carried when it follow the spline path.
Problem: Since there are different types of fruits (Orange, Banana and Watermelon) they are been instantiated from prefab and the issue is the force will be deter$$anonymous$$ed from the user pulling distance(from slingshot like angry birds) and then the force need to be applied to the fruit, so more force the fruit will follow the spline curve more accurately and pass each points in the curve.
I know is it bit complicated and am not good at explaining things, let me know if you need more details, I will do my best to explain. Am kind of struck at this point as not sure how to proceed.
Isn't the velocity of the rigidbody the force that's currently affecting it?
It sounds like you are using the force to generate the spline, while the object is flying. So, why not simply store your initial force in the fruit's script when it is "shot".
(or more accurately, you should store "initial velocity" as hex implied- since, once launched, the path should be the same regardless of the mass of the fruit. Though the mass of the fruit WILL effect the initial velocity, given a particular force e.g. to launch a watermelon on the same path as an orange will require more force- but once launched they will have the same initial velocity.)
p.s. @Hexagonius I suspect you know this already, but just for the sake of clarification: velocity of a mass can be described as $$anonymous$$enetic energy, momentum, stuff like that- but not a force. Force is a CHANGE in the velocity of a mass (acceleration).
@Glurth: Thanks for your $$anonymous$$gestion.
So if I store the initial velocity and can calculate the force of the fruit so as per the force initially calculated I can make the fruit follow the spline curve? I didn't quiet understand this. Can, you give me an example, it would be great.
Answer by lordlycastle · Mar 07, 2015 at 07:07 PM
I think you might be over complicating, if you think that in Angry birds, the bird moves by following a path. But that path is there just so the user knows where he's shooting. What's happens there is that game records the what vector user has dragged, and then applies force inverse of that vector. The bird then shoots, and as gravity loves to pull everyone down, it goes in a curve, instead of flying of screen in that vector direction. When it hits another rigid body the physics engine takes care of the calculations. It would just be too tedious to do all the physics calculations yourself.
Now from my understanding of the behaviour you are trying to achieve is; longer the user pulls, more accurately the force will be applied on the fruit (please tell me if I'm wrong). So what you want to do is set the delta percentage amount the force is allowed to stray using 1 / Vector component i.e. you have to do it for all x, y, and z separately, let's say deltaX for x component. Then you have to use Random.Range; where min is x - x * deltaX, and max is x + x *deltaX, to set the components of the force which will be applied. This should make it so that more the user pulls, more accurate the force vector will be. Remember as the value approaches zero, the deal variable is going to approach infinity. So you should make sure it never does.
If your issue is with different sizes, then you can change the mass property of each rigid body in the prefab. So for larger fruits will require more force to move the same distance.
If something is unclear, don't be afraid to ask. :)
PS: Just to clarify only objects like missiles are constantly applying force, you easily can get the force of these object, which would be mass X ∆velocity per second. But objects like thrown ball, the force is only applied just once (impulse), and they would maintain the same velocity if there is no friction. Have a look at Force Modes in Unity.
@lordlycastle: Thanks for your detailed reply. Highly appreciated :)
okay, let me break it down.
Concept: The game am building is different to the angry birds game mechanism. In my game the player can move up and down. In angry birds the bird slingshot is stationary, whereas in my game the player moves up and down and the slingshot is tied to the player. So, when the user touch and drag, player will move up and down, so the slingshot will also move along with player. There are 3 fruits in the game, Each fruit need to follow a path (Spline Curve)
Problem: Once the fruit is thrown from the slingshot (force is been calculated from the user pulling strength) and after that I have placed an empty object with collider it in. So after this fruit collide with the empty object it need to follow the spline curve.
Issue is like explained earlier how to store the force/ power which is been released from the slingshot and depending on the power/force t he fruit will pass each points in the spline curve. So more powerful it is, the more points it can pass in the spline curve.
Sorry, am terrible in explaining things :( Let me know if am not clear. I will do my best to explain it. Thanks.
Cool. This idea is something that I never thought about. Will give a try. Thanks.
Answer by tangentstorm · Mar 08, 2015 at 02:37 PM
From the comments, it sounds like you are trying to make your projectile follow a fixed Bezier curve. You probably don't need to do this, unless you are going for some kind of cartoon or non-realistic physics.
For an Angry Birds style projectile, just apply the force in the direction the player aimed, and let gravity do the work. The arc will happen naturally, and the projectile will hit the target with whatever force you would get in the real world, based on the initial acceleration, the gravity setting, and masses of the two objects.
If you want to adjust the behavior to get non-realistic physics, then what you can do is store the acceleration data in a variable, and then implement an OnCollisionEnter handler:
http://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
This will give you a Collision object. You can take its .rigidBody and apply whatever extra forces you want to it:
http://docs.unity3d.com/ScriptReference/Collision-rigidbody.html
@tangentstorm: Thanks for your suggestion. The problem is Arc is parented to the player. So the arc moves up and down as well. In angry bird game, bird doesn't move, In my game the player can move up and down so each time the player moves up or down, the arc will also follow the player (parented the object to the player) Arc is made of spline points.
In angry bird game, it doesn't follow a path, wheres in my game the fruit once thrown with the force need to follow the spline path. So am not quiet sure to deter$$anonymous$$e the force and use that force to make the fruit follow the spline path. So for instance if the force is less the spline path will be more weak and it will not pass all the points in the spline.
Answer by hexagonius · Mar 10, 2015 at 09:02 AM
I would do this: - throw the fruit - make the collider a trigger so the fruit won't loose velocity and save it on enter - with the magnitude of the velocity you can do your math determining whatever it shall do on the spline (didn't get it actually) - while moving along the spline, adjust the velocity vector in FixedUpdate without changing anything else on the rigidbody. If you want to be perfectly accurate you will need to Lerp between spline points because you won't reach each one due to time stepping. - after the spline part is done just stop influencing the velocity and it'll move physically correct again
Hope that's an idea
Your answer
Follow this Question
Related Questions
Unity 2d Physics Collision Jumping / Glitching 0 Answers
How to check place on empty for placement polygon Rigidbody2d? 0 Answers
Rotating child object in compound collider 0 Answers
Creating a simple swinging vine/rope/line 0 Answers
Rigidbody2D freezes 0 Answers