- Home /
Object sometimes stops after drag, instead of sliding?
Hi,
I would like to make the camera slide a little bit more when the touch ends. With this code, the effect works, but sometimes it stops right after the second attempt. The camera is supposed to slide every time the touch has moved and ends, but it sometimes stops for an unknown reason. Would someone have any idea why?
if (Input.touchCount > 0 ){
if ( Input.GetTouch(0).phase == TouchPhase.Began ) {
//_doLongMove = false;
}
if ( Input.GetTouch(0).phase == TouchPhase.Moved ) {
// Get movement of the finger since last frame
touchDeltaPosition = Input.GetTouch(0).deltaPosition;
float speedDrag = 0.2f;
transform.Translate (-touchDeltaPosition.x * speedDrag, 0, -touchDeltaPosition.y * speedDrag);
}
if ( Input.GetTouch(0).phase == TouchPhase.Ended ) {
speed = 0.3f;
_doLongMove = true;
}
}
if ( _doLongMove ) {
doLongMove();
}
void doLongMove(){
float slowDown = 0.01f;
float force = 1.5f;
if ( speed > 0 ){
speed -= slowDown;
}
if ( speed < 0 ) {
speed = 0;
_doLongMove = false;
}
transform.Translate (-touchDeltaPosition.x * force * speed, 0, -touchDeltaPosition.y * force * speed);
}
EDIT:
I also tried this, but it stops as well, sometimes 3 times in a row, then it works fine :
if ( wasDragging && Input.touchCount == 0 ){
speed=0.3f;
_doLongMove = true;
wasDragging = false;
}
if ( _doLongMove ) {
doLongMove();
}
Thanks
Answer by Kinos141 · Dec 20, 2013 at 06:11 PM
You should try a Mathf.Lerp that takes the current speed and lerps it to zero over time. That way you can have it slow to a halt.
I would also use iTween.
Thanks, I don't know if what I said was clear to describe the problem : when I make several attempts in the same direction to move the camera, ins$$anonymous$$d of sliding continuously, it stops, as if I stopped the finger on the screen, but I did not. Every time I lift the finger, I would like the object to keep on moving. The slow down works fine though (with a $$anonymous$$athf.Lerp as well), except when the camera stops: for some reason, the 3d or 4th time, it does not understand that the effect was a slide.
Your answer
Follow this Question
Related Questions
Touch deltaPosition Android 1 Answer
Ragdoll 2D character controller help 0 Answers
Touch and Drag for Android problem 0 Answers
How to change the scaling of a gameobject during runtime? 2 Answers
I'm not sure how to approach this. 2 Answers