- Home /
How I can make easily this swipe controls?,
As you can see in the code, I made the transform.Translate move to where I wanted and that with the if I can't continue making it go out of the way. How can I make them move smoothly and not appear to be teleporting from one place to another?
private void OnSwipeLeft()
{
Debug.Log("SwipeLeft");
if (transform.position != new Vector3(-2f,transform.position.y,transform.position.z))
{
transform.Translate(-2f,0,0);
}
}
private void OnSwipeRight()
{
Debug.Log("SwipeRight");
if (transform.position != new Vector3(2f,transform.position.y,transform.position.z))
{
transform.Translate(2f,0,0);
}
}
Answer by FlorentAlbyon · Aug 06, 2020 at 03:30 PM
There are a number of ways to do this. The cleanest one i can think of is to use a Coroutine that you'd start when you detect that your user has swipped. so instead of
transform.translate(-2f, 0f, 0f);
You could have:
StartCoroutine(SwipeToLeft);
with the following function defined:
private IEnumerator SwipeToLeft(){
while(/*you've not reached the desired position*/){
/*
move a little toward your desired position
Dont forget to take into account Time.deltaTime so it's not framerate dependant
You can also multiply by the speed at which you want the swipe to happen
*/
yield return null;
}
}
Hope this helps :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613005036im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Smooth movement using transform.Translate 2 Answers
Smoothing Network Movement with OnSerializeNetworkView 0 Answers
How do I make a smooth animation in unity 1 Answer
Vector 3 coordinates confusion (transform.Translate(...)) 2 Answers
character controller was not working when the apply rootmotion option is enabled? 0 Answers