So, I have this piece of code that moves my sprite from side to side whenever a finger slides on the phone; movement is just like Rolling Sky.
void FixedUpdate() {
if (GameController.Instance.isGameOver()) {
animator.enabled = false;
}
else {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
transform.position = new Vector3(worldPoint.x, transform.position.y, transform.position.z); //sprite moves along with finger
}
rb2D.position = new Vector3(Mathf.Clamp(rb2D.position.x, boundary.xMin, boundary.xMax), 0.0f, 0.0f);
}
}
My problems are:
If you double tap on any spot on the screen, the sprite instantly jumps to that location, as if it can teleport.
The sprite moves slightly (1/2 inch) after making contact with the (sprite)object that triggers game over; main sprite not suppose to move after game is over. The sprite moving makes it seem like you didn’t actually lose because it’s not touching the object anymore after game is over.
These aren’t major problems because my game is still playable, but I rather it not have these problems.
You are just setting the object’s position based on the touch.
I think what you want is to adjust the position from what it is by an amount proportionate to the distance between the touch on this frame, and the previous frame, i.e., the delta motion of your touch.
And as a corollary, the first time your finger goes down, do not move it at all, since that is zero motion. But of course you would write down that position and use it the next frame to calculate delta.
The touch gives you “where that particular finger is right now.”
If you want a background to slide according to your finger’s sliding, you want to adjust the background’s by, “how much did this finger move since the last time I was here in Update()?”
Generally this is handled by having a member variable to track the “last position” of the touch. You could just have a Vector3 called LastTouch.
Then each Update(), if the finger goes down (phase == TouchPhase.Began), you assign the current touch to that LastTouch variable.
Next, in the same Update(), if phase == TouchPhase.Moved, you subtract LastTouch from the new position, and use that value (scaled appropriately) to move your background.
Finally, you write down the current touch in the LastTouch, so that next time through, if the finger doesn’t move, the motion is zero.
Think through the above steps as you place your finger down, sort of “run those steps in your head” and you will see how it sorta makes sense.
I didn’t mean to say I didn’t comprehend “any”. I understood some the first time. But thanks! And my background doesn’t move O.o Only the turtle does, and it moves horizontally at the same y-axis point.
My original answer was specifically to address (symptomatically) both of the problems you indicated in your first post: the jump on double-tap, plus the slight motion at initial touch.
Either way, I hope you and your turtle are able to enjoy the type of motion that you aspire to.