Smooth position transform with swipe controls ?
Hello, I have a problem with smooth position transform with my swipe control script. My character is moving from one place to another without any smoothness. I tried to imply an animation but it started had playing after the character was in a desired position. Here's my script, please help me!
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwipeControls : MonoBehaviour
{
public float speed = 5.0f;
private Vector3 startpos; // start position
private Vector3 endpos; //end position
public int xmovementlimit = 0;
public int ymovementlimit = 0;
void Start()
{
}
void Update()
{
PositionChange();
}
private void PositionChange()
{
foreach (Touch touch in Input.touches)
{
Vector3 newPosition;
if (touch.phase == TouchPhase.Began)
{
startpos = touch.position;
endpos = touch.position;
}
if (touch.phase == TouchPhase.Moved)
{
endpos = touch.position;
}
if (touch.phase == TouchPhase.Ended)
{
newPosition = transform.position;
if (Mathf.Abs(startpos.y - endpos.y) > Mathf.Abs(startpos.x - endpos.x))
{
if ((startpos.y - endpos.y) > 100 && pionowa > -1) //swipe down
{
ymovementlimit--;
newPosition.y -= speed;
transform.position = newPosition;
}
if ((startpos.y - endpos.y) < -100 && pionowa < 1) //swipe up
{
ymovementlimit++;
newPosition.y += speed;
transform.position = newPosition;
}
}
else
{
if ((startpos.x - endpos.x) > 100 && pozioma > -1) //swipe left
{
xmovementlimit--;
newPosition.z -= speed;
transform.position = newPosition;
}
}
if ((startpos.x - endpos.x) < -100 && pozioma < 1) //swipe right
{
xmovementlimit++;
newPosition.z += speed;
transform.position = newPosition;
}
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Mobile Optimization question 1 Answer
Network Transport Layer API does not work with iOS to PC???? 0 Answers
How to get chunks to the right position 0 Answers
Camera isn't move position? Why my camera isn't change position? 0 Answers
Remember position for a simple player controller (Left/Right)? 0 Answers