# Translate swipe gesture to turret rotation.

I’m looking for some guidance doing the following:

• rotating around the turret with the finger rotates turret
• swiping left/right will rotate the turret in either direction (left will rotate opposite of right)
• swiping up/down will rotate the turret in either direction (up will rotate opposite of down)

Like the following:

I’ve tried various things but haven’t found anything that feels right.

I’d like to just translate these gestures into velocity (+/- for rotate clockwise/anticlockwise)

Any ideas on how to translate that set of gestures into a velocity value?

I’ve got working code for the left/right or up/down which is essentially this (cut down to show the poor implementation I have):

``````//---------------------------
//
// Happens in my onMouseDown event
//

if (previousPosition.x > turretScreenPoint.x) {
directionY = -1;
} else {
directionY = 1;
}

if (previousPosition.y > turretScreenPoint.y) {
directionX = 1;
} else {
directionX = -1;
}

//---------------------------
//
// Happens in my onMouseMove event
//

float distance = Vector2.Distance(previousPosition, mousePoint);

float processingTimeDelta = Time.time - lastProcessTime;
lastProcessTime = Time.time;

float distanceX = previousPosition.x - mousePosition.x;
float distanceY = previousPosition.y - mousePosition.y;

if (Mathf.Abs (distanceX) > Mathf.Abs (distanceY)) {
if (distanceX < 0) {
distanceX = Vector2.Distance (previousPosition, mousePoint) * -1;
} else {
distanceX = Vector2.Distance (previousPosition, mousePoint);
}

distanceX *= directionX;
velocity = getVelocity (distanceX / processingTimeDelta);
}
else
{
if (distanceY < 0) {
distanceY = Vector2.Distance (previousPosition, mousePoint) * -1;
} else {
distanceY = Vector2.Distance (previousPosition, mousePoint);
}
distanceY *= directionY;

//
// I actually smooth the value here but i'm trying minimise the logic for this example
//
velocity = distanceY / processingTimeDelta;
}

previousPosition = mousePoint;
``````

So this only handles left/right, up/down gestures and fails miserably at giving something sensible when the user tries to rotate around the turret…

Any ideas?

Hi, i guess this forum is were you should ask this type of question: Unity Engine - Unity Discussions

1 Like