1

I am trying to make a game where if the user taps the screen their character moves from one point (say left side of the screen) to another point (right side of the screen) smoothly.

I know you can do this a few ways: Coroutines, Lerp or even an animation. I wouldn’t mind code examples as well..

What are some different ways to achieve this?

Thanks in advance!

Wp3Dev
  • 2,001
  • 7
  • 38
  • 54
  • Define "best"... – FCin Apr 09 '18 at 15:22
  • the "Best" way is really subjective... but it would be helpful if you gave some examples of exactly what you need. For example does this system need to worry about collision detection? Are you using any form of path finding? – AresCaelum Apr 09 '18 at 16:00

1 Answers1

3

Well best is mostly opinion based and not a good way to ask actually... anyway

What the other answer didn't mention:

Of course the smoothness doesn't come from Vector3.Lerp nor Coroutines but actually from the Time.deltaTime.

This Time.deltaTime value is the real time passed since the last frame. This allows to do all kind of smooth and especially always taking the same amount of real time movements and actions independent of the frame rate.

In terms of the specific usecase of movements I personally would prefer the usage of Vector3.MoveTowards over Vector3.Lerp because the later gets slower and slower as it gets closer to the target. (At least if you use the transform.position as first value as often done in examples)

Here is the example provided by the Unity scripting API

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public Transform target;
    public float speed;
    void Update() {
        float step = speed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, target.position, step);
    }
}

Instead of target.position of course you can also just define a Vector3 as target position.

If you need collision detection than both shouldn't be used because you might just move the object to far in one step so the collision isn't triggered.

In this case you might find a hint in this post

Paul
  • 35
  • 7
derHugo
  • 83,094
  • 9
  • 75
  • 115