# [SOLVED] Interpolated movement proportional to distance to destination...

UPDATE: The mystery has been solved!

Here’s the much more compact, much more efficient code:

``````using UnityEngine;
using System.Collections;

public class camera : MonoBehaviour {

public int initialSpot;
public int camSpot;
public float camSmoothening;
public Transform[] camSpots;

void Update () {

if (Input.GetKeyDown (KeyCode.C)) {

if (camSpot+1 < camSpots.Length) camSpot++;
else camSpot = initialSpot;
}
transform.position = Vector3.MoveTowards (transform.position,
camSpots [camSpot].position,
Vector3.Distance (transform.position, camSpots [camSpot].position) / camSmoothening);

transform.rotation = Quaternion.RotateTowards(transform.rotation,
camSpots [camSpot].rotation,
Quaternion.Angle(transform.rotation,camSpots [camSpot].rotation) / camSmoothening);

}

void Start () {
camSpot = initialSpot;
}
}
``````

For comparison, here’s the original code (with original post provided around it):

I have this code:

``````public class cameraInterpolator : MonoBehaviour {

public int initialSpot;
public int camSpot;
public float camSmoothening;
public Vector3[] camSpots;

public IEnumerator moveTo(Vector3 destination, float smoothen)
{
Vector3 distance;
Vector3 movement;

while (transform.position != destination) {
distance = transform.position - destination;

movement = new Vector3 (transform.position.x + (distance.x / smoothen),
transform.position.y + (distance.y / smoothen),
transform.position.z + (distance.z / smoothen));

transform.position = movement;

yield return new WaitForSeconds(0.16f);
}
}

void Update () {

if (Input.GetKeyDown (KeyCode.C))	{

if (camSpot < camSpots.Length) camSpot++;
else camSpot = initialSpot;
}
StartCoroutine(moveTo (camSpots [camSpot],camSmoothening));
}

void Start () {
camSpot = initialSpot;
}
}
``````

It seems to work as it should, with everything visibly functioning. But no matter what happens, destination.x always seems to return Infinity. I tried feeding it non-zero values in the Unity editor to no avail. What’s more, the script occasionally returns outlandish coordinates seemingly out of the blue, for reasons I can’t for the life of me figure out. Is somebody out there seeing something I’m not?

So, here’s to me buds, TobiasP and tobyfee.

One problem I notice, it may not be the cause but it will cause bad behavior: you’re testing for exact equality with transform.position != destination which isn’t going to work like you expect: this loop will never stop running. You should change this into a check of the distance between the transform position and the target, that stops when the distance is less than, say, .01f

The more I think about it, the more likely I think it is that this is the cause.

Also, I really try not to nitpick but I think this is a pretty basic issue: I’m not sure I would use a co-routine at all for this, rather I would just call all this on Update(). I’m sorry that I can’t explain the logic perfectly, but delegating motion to a co-routine just isn’t what I’m used to and seems like it could cause unexpected behavior. it really makes more sense to me to place everything inside an ‘if’ in the update function.