I'm implementing a touch controller for my game.
I am attempting to imitate Tiki Taka Soccer's "ball shooting" controls (gameplay video), where the curvature of the player's swipe affects the curve of the shot's trajectory.
Basically, I want to move the object to where the user raised their finger, but also apply a curve to the movement, corresponding to the curve of the swipe.
How can I do this?
I'm using Unity 5, and I'll be using the Lean Touch plugin from the asset store to capture swipes.
Answer
Detecting swipe curvature
Treat the finger swipe as a polyline. Approximate its curvature, and use that as a multiplier for how much to “curl” the resulting shot either left or right.
Let's say a swipe path has no curvature if it goes linearly from the start (the circle), to the end, or curvature value 0
.
I'll emphasise other swipes' differences to this baseline with blue and red colours.
One that makes a 90° right turn has a maximum right curvature, or curvature value 1
.
One that makes a 90° left turn has a maximum left curvature, or curvature value -1
.
We can hence find any intermediate curvature by calculating the proportion of the area it covers on either side of the baseline path, as a proportion of that maximum!
When the line curves partially to either side, just add the signed areas to get the overall curvature.
Of course, you might still get particularly insane swipes that make a really far-out turn, tighter than 90°.
It's up to you how you handle these. Maybe you want to clamp them to the [-1, 1] range to prevent people from doing a Fire Tornado Spin Kick—but then again, maybe that would be fun!
Applying curvature to object motion
To apply a curve to an object's forward motion, apply an impulse in its intended direction (the red arrow to the right; to create the forward motion), then an impulse perpendicular to it (the red arrow upward; to push it "outward"), then apply a constant force perpendicular to it in the other direction (the green arrow downward; to pull it "in" again). For a motion curve in the other direction, just flip the perpendicular impulse and force directions.
You can find the perpendicular of a 2D vector by exchanging its x
and y
components and negating one of them. Here, you probably want to normalise the perpendicular vectors and multiply them by the curvature value.
Tweak the strength of the forces and impulses to get the effect you want.
No comments:
Post a Comment