- Home /
How to apply force to an object?
I am using a gesture API to detect gestures that the user will use to move objects in a 3D space. I just wanted to know how to add force to an object that has a rigid body. Lets say that i do a swipe gesture and I want it to take in the velocity of the swipe, the position of the swipe (start point and end point) and in the end it determines how fast the object moves across a surface.
I am new to the whole gesture recognition and all I want to know is how to get started. Can anyone help me? Many thanks in advanced!
RigidBody.AddForce(endpoint - startpoint) could do the trick
Expanding on @poncho's comment:
rigidbody.AddForce((endpoint - startpoint) * factor);
This is a starting point. As a pure guess with an object of mass 1.0, 'factor' will be around 20. This will add force based on the length of the stroke, not the speed of the stroke. And it will not be platform independent.
@poncho no, it didnt work, but thanks for your help anyways.
@robertbu I tried that but the object is not moving. What is in your "factor" variable. $$anonymous$$aybe I'm doing it wrong. I even went ahead and just put 20 within the factor variable.
Answer by Benoit Dufresne · Jan 19, 2014 at 02:38 AM
Robertbu and poncho are correct.
The function you need is Rigidbody.AddForce(Vector3 force). It just needs to be strong enough to move your object's mass.
I'd recommend something like:
rigidbody.AddForce((endpoint - startpoint).normalized * force);
to experiment. This will ignore the magnitude of your movement and just apply "force" newtons to your object. Make sure force is greater than the object's mass and see if that works!
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Animate the scaling of a gameObject 4 Answers
Not getting component properly 1 Answer