Reputation: 313
I want to move character to left and right from whole screen and if i move from anywhere to left or to right it need to move only how much i have now = 1.5f
I have this code and works perfect but it works only with mouse not with touch screen:
if (!isMoving && Input.GetMouseButtonDown(0))
{
desiredPos = transform.position + Vector3.right * 1.52f;
isMoving = true;
}
if (!isMoving && Input.GetMouseButtonDown(1))
{
desiredPos = transform.position - Vector3.right * 1.52f;
isMoving = true;
}
if (isMoving)
{
transform.position = Vector3.MoveTowards(transform.position, desiredPos, moveSpeed * Time.deltaTime);
// this == is true if the difference between both
// vectors is smaller than 0.00001
if (transform.position == desiredPos)
{
isMoving = false;
// So in order to eliminate any remaining difference
// make sure to set it to the correct target position
transform.position = desiredPos;
}
}
I tried to implement this code and this is not working
private void Update()
{
timer += Time.deltaTime;
int i = 0;
//loop over every touch found
while (i < Input.touchCount)
{
if (!isMoving && Input.GetTouch(i).position.x > ScreenWidth / 2)
{
//move right
desiredPos = transform.position + Vector3.right * 1.52f;
isMoving = true;
}
if (!isMoving && Input.GetTouch(i).position.x < ScreenWidth / 2)
{
//move left
desiredPos = transform.position - Vector3.right * 1.52f;
isMoving = true;
}
++i;
}
if (isMoving)
{
transform.position = Vector3.MoveTowards(transform.position, desiredPos, moveSpeed * Time.deltaTime);
// this == is true if the difference between both
// vectors is smaller than 0.00001
if (transform.position == desiredPos)
{
isMoving = false;
// So in order to eliminate any remaining difference
// make sure to set it to the correct target position
transform.position = desiredPos;
}
}
}
This second code moves character too much to left or to right and i need to move exact like in first code but with touch swipe to left or to right.
I want something like this to move exact 1.5f
Upvotes: 0
Views: 197
Reputation: 27011
To behave same as GetMouseButtonDown
, you need also check the phrase of the touch:
Input.GetTouch(i).phrase == TouchPhase.Began
Upvotes: 1