- Home /
Add impulse force using input.touch
I have a code like this:
if (Input.touchCount > 0) {
var theTouch : Touch = Input.GetTouch (0);
var ray = Camera.main.ScreenPointToRay(theTouch.position);
if(Input.touchCount == 1){
if(Physics.Raycast(ray,hit,50,layerMask)){
var scrollDeltaX = theTouch.deltaPosition.x;
var scrollDeltaY = theTouch.deltaPosition.y;
scrollDistanceX = scrollDistanceX+scrollDeltaX;
scrollDistanceY = scrollDistanceY+scrollDeltaY;
var dir : Vector3 = Vector3(scrollDistanceX, scrollDistanceY, 0);
hit.rigidbody.AddForce(dir*coef,ForceMode.Impulse);
}
}
So the idea is to apply an Impulse force to a rigidbody object when user perform drag gesture on it. But it should be applied only once per gesture, so that force doesn't multiply during one finger movement.
I tried to use timer (AddForce applied once per second), i works some how but with strange behavior. (Response was not constant).
I think i need to use some Touch.phase, but i'm not sure how.
Thanks for your response;
So, do you want the force applied at the end of the gesture, or when the gesture reaches a given threshold point? Force$$anonymous$$ode.Impulse should only be used for one-off forces, not every frame the way you are using hit here.
Yes, i want force to be applied at the end of the gesture.
Force$$anonymous$$ode.Impulse should only be used for one-off forces
example, please.
Well, in this case, an example would be actually what you are doing here.
Well, Touch.phase has what you need here- you need to apply the force when the touch is released, but at the same time you need to store information about each touch in some data structure (depending on the detail you need for your gestures, this could be either just the start point, or a list of points generated over the course of the touch) when the touch has not been released. Touches are sorted by integers, no? Then you can just use a list of Vector2 arrays, and access it by number whenever you have a given touch message. Then, at release, you can ask the list for the information it has collected, and clean it out at the same time ready to be used again!
Your answer
Follow this Question
Related Questions
making perpetual movement 1 Answer
Does rigidbody.AddForce work for the iphone? 1 Answer
how do i move a object with direction of touch 0 Answers
RigidBody.AddForce not working 1 Answer