I am trying to scale my object by a finger touch just like in Jelly Shift game. It should scale up/down when I move my finger up and down and it should stop when the touch is in stationary phase.
The problem is when I pressure the screen without moving my finger, touch phase changes to "moved" and my object scales down. The harder I pressure, the faster it scales down. I tried to clamp touch.pressure to 1 everytime it detects a finger on screen, but it didn't work. How can I ignore pressure of the touch and scale the object just by finger moves ? Thank you so much.
Note: I test codes on iPhone 7 Plus
Here I check the input by touch
private void CheckInput()
{
if (Input.touchCount == 1)
{
_touch = Input.GetTouch(0);
// I've tried to clamp touch pressure like below but it didn't work
_touch.pressure = 1;
if (_touch.phase == TouchPhase.Began)
{
_isInputAllowed = true;
}
else if (_touch.phase == TouchPhase.Moved)
{
_isInputAllowed = true;
}
else if (_touch.phase == TouchPhase.Stationary)
{
_isInputAllowed = false;
Vertical = 0f;
}
else if (_touch.phase == TouchPhase.Ended || _touch.phase == TouchPhase.Canceled)
{
_isInputAllowed = false;
Vertical = 0f;
}
CalculateVerticalInput();
}
}
I process the input below
private void CalculateVerticalInput()
{
if (_isInputAllowed)
{
if (_touch.deltaPosition.y > 0.5f)
{
Vertical = 1f;
}
else if (_touch.deltaPosition.y < 0.5f)
{
Vertical = -1f;
}
else
{
Vertical = 0f;
}
}
}
Object scaling method
private void Scale()
{
if (_inputData.Vertical == 0)
{
return;
}
sourceScaleVal = (_inputData.Vertical > 0) ? _settings.MinScaleValue : _settings.MaxScaleValue;
targetScaleVal = (_inputData.Vertical > 0) ? _settings.MaxScaleValue : _settings.MinScaleValue;
transform.localScale = Vector3.MoveTowards(transform.localScale, new Vector3(sourceScaleVal, targetScaleVal, transform.localScale.z), _settings.ScalingSpeed * Time.deltaTime);
PlayerHelper.SetCurrentScale(transform.localScale);
}