- Home /
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.
Answer by UnityCoach · Feb 21, 2017 at 11:56 PM
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
}
}
}
Answer by phatpixels · Feb 22, 2017 at 06:56 PM
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 :)
Thanks. I'm happy that it works. If you change 0.5 to a higher value on that line :
if ($$anonymous$$athf.Abs (_delta.x) > 0.5f) // if pointer delta on x is greater than 0.5
it will take a greater motion to make it change.
Coach, that is exactly what i needed, thank you so much.In return if you need any creative work done or a beer for your expertise just shout:-)
Your answer
Follow this Question
Related Questions
Find point of top of rotated sprite 1 Answer
Spawning sprite in for loop 1 Answer
Sprite image not changing 0 Answers
How to scroll progressively a game object? 0 Answers
Sprite Swapping help 1 Answer