Fake 2d object carousel with swipe

Hello,

Seems simple in theory, I am trying to create the effect of spinning an object on mobile swipe, but using a sequence of 12 sprites.
Like spinning a car image sequence left to right or vice versa, on finger input.
I’ve searched but maybe I’m not using the correct terminology perhaps.

Didn’t test it, you may have to play with this a bit.

public List<Sprite> sprites; // the list of sprites to assign in the editor
private int current; // the current index
private Vector2 _delta; // the pointer delta
public SpriteRenderer spriteRenderer; // the sprite renderer to assign in the editor

void Update ()
{
	if (Input.touchCount > 0) // if there's at least one finger on screen
	{
		Touch touch1 = Input.touches[0]; // assign the first touch
		switch (touch1.phase)
		{
			case TouchPhase.Began : // if we just touched
				_delta = Vector2.zero; // reset
				break;
			case TouchPhase.Moved : // if we move
				_delta += touch1.deltaPosition; // update the pointer delta
				break;
			case TouchPhase.Ended : // if we just released
				_delta = Vector2.zero; // reset
				break;
			default:
				break;
		}
		if (Mathf.Abs (_delta.x) > 0.5f) // if pointer delta on x is greater than 0.5
		{
			current += (_delta.x > 0) ? 1 : -1; // move current to +1 if positive, -1 otherwise
			if (current == -1) // if out of bounds below 0
				current = sprites.Count-1; // reset to max index
			if (current == sprites.Count) // if out of bounds above max
				current = 0; // reset to 0
			spriteRenderer.sprite = sprites[current]; // replace the sprite renderer with the sprite from the list
		}
	}
}

A quick test and it works quite well it seems. Only a bit sensitive, next step will be for me to slow it down in some way. I can’t thank you enough UnityCoach. U Certainly live up to your name :slight_smile: