2D touch - Why is this movement so jittery?
So I've been struggling for years on how to get an object to move relative to touch delta horizontally in a smooth way, and my best attempt so far has been this:
if (touch.phase == TouchPhase.Moved)
transform.Translate(new Vector3(((touch.deltaPosition.x/screen.x) * velocityConstant * Time.deltaTime), 0, 0));
What I've done here: the ratio of the deltaPosition over screen.x pixel count is to normalize deltaPosition across screen sizes, and velocityConstant is a multiplier to move the object further than the actual movement.
Nonetheless, it is jittery, unpredictable and very, very inconsistent. All in all it doesn't behave like it should.
All I need is a movement relative to finger slide that moves further than the actual touch slide (hence why I say it's "relative" and not the exact length).
Any help is appreciated.
Can you post a screenshots on what the game look like and how the object is moving ?
I could try to capture it later. But for now I can add that I feel like the speed fluctuates unpredictably - i.e.: I move my finger similar distances twice, but on one instance it moves normally and in the other is slower or faster, especially when it changes direction. The differences aren't big, nonetheless noticeable and really kill the gameplay. I would just like to know if the code is "right" so I could look for other causes for this weird behavior.
Okay so, basically you're trying to make an object moving when you Hold and swipe your finger on the screen right ?
Answer by Raimi · Mar 08, 2018 at 05:21 AM
When you use a multiplier on any movement it will cause some jitteryness. The bigger the multiplier, the bigger the jitteryness. This is happens because the object is jumping by the multiplier amount each frame.e.g if the multiplier is three, it will move by 3*time.deltatime each frame, so if its 10 you can imagine whats happening.
Also, instead of using translate, modify the transform directly or use vector3.lerp.
hope it helps
Great info, I'll change it up and see how it behaves. Thanks
Late response: set it to directly change the transform via this:
transform.position = new Vector3(startPos.x + ((touch.deltaPosition.x / screen.x)*velocityConstant*Time.deltaTime), startPos.y, 0);
It now moves butter smooth, thanks!
Your answer
Follow this Question
Related Questions
2D android swipe movement,2D Mobile Movement 0 Answers
Any one help me in this script? 0 Answers
How can i move just one object ? 0 Answers
Is my Touch Algorithm right? 0 Answers
How can I determine the speed of my touch movements? 0 Answers