Question by
geeksband · Dec 25, 2017 at 09:13 PM ·
scripting problemcontrols
Independent control with smoothness
I make control: move to the right - moves to the right by 2 units, move to the left - moves left by 2 units, where exactly pressed does not matter. The problem is that there is no smoothness and when sometimes you lead to the right, let's say, it pulls to the left and returns to the right
/// <summary>
///
/// </summary>
/// <param name="add"></param>
void MoveToAdd(float addX, float addY)
{
float x = Mathf.Lerp(cachedTransform.position.x, cachedTransform.position.x + addX, Time.deltaTime * SPEED_TOUCH);
//float y = Mathf.Lerp(cachedTransform.position.y, cachedTransform.position.y + addY, Time.deltaTime * SPEED_TOUCH);
x = Mathf.Clamp(x, LEFT_BORDER, RIGHT_BORDER);
//y = Mathf.Clamp(y, UP_BORDER, DOWN_BORDER);
cachedTransform.position = new Vector3(x, startedPosition.y, 0);
}
/// <summary>
///
/// </summary>
private void Control()
{
isControl = false;
if (controllerType == ControllerType.Swipe)
{
if (Input.touchCount > 0)
{
Touch touch = Input.touches[0];
Debug.Log(touch.deltaPosition.x);
float x, y;
/*
if (touch.phase == TouchPhase.Began)
{
previousPosition = touch.position;
}
*/
if (touch.phase == TouchPhase.Moved)
{
if (touch.deltaPosition.x > 0) x = speed;
else if (touch.deltaPosition.x < 0) x = -speed;
else x = 0;
if (!isControl && x != 0) MoveToAdd(x, 0);
//previousPosition = touch.position;
}
}
}
else
{
Accelometer();
}
}
Comment