- Home /
Swipe to kill enemy? 2D
I have enemies that are constantly approaching my player. I want to be able to swipe across them with a finger to, essentially, cut them in half. For now, as long as they disappear, that's fine. I can figure out how to do death animations later.
So far, I have:
function OnMouseUpAsButton ()
{
Destroy(gameObject);
}
Which works great for tap killing. But I need swipe killing. Any advice? I've read a bit about raycasting but I'm not sure if that's what I need. Any help is appreciated!
Answer by GiyomuGames · Aug 04, 2016 at 06:08 AM
You should store the start position and start time of the touch. Then when the player stops touching the screen you know what line he has drawn on the screen and how long it took. If this line was drawn relatively fast then it's a swipe, and if it goes through enemies then they should die.
Use Input.GetTouch(0) and use its phase to know if it just started or just ended. https://docs.unity3d.com/ScriptReference/Touch.html https://docs.unity3d.com/Manual/MobileInput.html https://docs.unity3d.com/ScriptReference/Input.GetTouch.html
Your answer
Follow this Question
Related Questions
IOS Swipe Gesture 0 Answers
Time.deltaposition problem on multiple devices 2 Answers
Touch Controls 0 Answers
Detect mouse swipe on 2d object 1 Answer
teleportation problems (2d) 1 Answer