Rebuilt MoveTo problem?

Hi, due to my boss asking to make a custom MoveTo in the scope of making the basic modules for a bigger project AND enabling me to get familiar with Unity (this was a few months ago when i began using Unity), i made the customised MoveTo. It’s pretty simple, but now that i have to change it to get a new behaviour, i’m a bit confused at what i wrote then. After reading it a few times i still think the code isn’t really good to use as the base for the new behaviour. Can anyone tell me if i’m right in thinking that the code isn’t written properly?

public float movementSpeed = 1;             //The default speed of movements
public float movementSmoothing = 0.25F;     //The default step smoothness of movements
private Vector3 moveTo;                     //Used by MoveTo, saves the target point
private bool isMoving = false;              //Used by MoveTo, tells if Update should compute a MoveTo on transform

public void MoveTo(Vector3 point)
{
    moveTo = point;
    isMoving = true;
}

void Update()
{
    //MoveTo
    if (isMoving)
    {
        if (!moveSmoothActive)  //Used if no smoothing is activated
        {
            //Computes the direction of the object
            Vector3 direction = Vector3.Normalize(moveTo - transform.position);
            //Computes the next step the object should do
            Vector3 step = transform.position + direction * Time.deltaTime * movementSpeed;
            //If closer than next step, go to final step, else go to next step
            if (Vector3.Distance(transform.position, moveTo) > Vector3.Distance(transform.position, step))
                transform.position = Vector3.Lerp(transform.position, step, movementSmoothing);
            else
            {
                transform.position = moveTo;
                isMoving = false;
            }
        }
        //Used if smoothing is activated
        else if (Vector3.Distance(transform.position, moveTo) > movementStopDistance)
            transform.position = Vector3.Lerp(transform.position, moveTo, movementSmoothing);
        //Else go to 'moveTo'
        else
        {
            transform.position = moveTo;
            isMoving = false;
        }
    }
}

So, i know this is my code, but it was strongly influenced by what the other, more experienced with Unity, coder told me while i was making it. As i wrote previously, i find the code wierd without being able to put the finger on what exactly is the weird part.

It looks like you were trying to do smooth and linear movement in that code, somehow. I ended up writing something from scratch that is probably easier to follow and should do about the same stuff. While it might behave slightly differently from your old code, I think it’s a bit easier to follow and easier to add new modes. You could also make an overload of MoveTo which takes a SmoothMode as parameter so it’s easier to configure how the object should move. You can slap this on a cube game object in a new blank scene to try it out.

  • Space relocates the object to a random location.
  • 1 on keypad selects “Linear” (no smoothing).
  • 2 on keypad selects “Smooth Damping” (eases in and out).
  • 3 on keyboard selects “Smooth Lerping” (eases out).

I hope it helps you find your way to your solution. Btw, the Smooth Lerping code was sort of ripped from your old code - it won’t work well on machines with vsync off because it won’t pay attention to frame rate at all. The other two methods do, however.

using UnityEngine;

public class Movement : MonoBehaviour
{
    public enum SmoothMode
    {
        Linear,
        SmoothDamp,
        SmoothLerp,
        // Just keep adding movement modes if you need to...
    }

    public SmoothMode smoothMode = SmoothMode.SmoothDamp;
    public float speed = 10.0f;
    public float smoothDamping = 0.25f;
    public float smoothLerping = 0.25f;
    public float targetArrivalRadius = 0.005f;
    private Vector3 target;
    private Vector3 velocity;
    private bool isMoving;

    public void MoveTo(Vector3 point)
    {
        target = point;
        isMoving = true;
    }

    void Update()
    {
        // Remove this after testing on a cube game object.
        QuickDemoKeyboardShortcuts();

        if (isMoving)
        {
            // Apply selected movement method.
            if (smoothMode == SmoothMode.Linear) LinearMove();
            if (smoothMode == SmoothMode.SmoothDamp) SmoothDampMove();
            if (smoothMode == SmoothMode.SmoothLerp) SmoothLerpMove();

            // Are we there yet?
            if (Vector3.Distance(transform.position, target) < targetArrivalRadius)
            {
                isMoving = false;
                transform.position = target;
            }
        }
    }

    void QuickDemoKeyboardShortcuts()
    {
        if (Input.GetKeyDown(KeyCode.Space)) MoveTo(Random.onUnitSphere * 5.0f);
        if (Input.GetKeyDown(KeyCode.Keypad1)) smoothMode = SmoothMode.Linear;
        if (Input.GetKeyDown(KeyCode.Keypad2)) smoothMode = SmoothMode.SmoothDamp;
        if (Input.GetKeyDown(KeyCode.Keypad3)) smoothMode = SmoothMode.SmoothLerp;
    }

    void LinearMove()
    {
        transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
    }

    void SmoothDampMove()
    {
        transform.position = Vector3.SmoothDamp(transform.position, target, ref velocity, smoothDamping, speed);
    }

    void SmoothLerpMove()
    {
        transform.position = Vector3.Lerp(transform.position, target, smoothLerping);
    }
}