Moving an Object to a precise spot

I’m trying to write a coroutine to move my blocks on the X axis at a certain speed until they reach a precise spot. I can’t use Lerp because it isn’t precise and I would like to base the animation on speed and not time.

My math sucks and I tried a lot of different ways, but floats are making my life really difficult.

Can someone help me?

using UnityEngine;
using System;
using System.Collections;

public class QuadB : MonoBehaviour 
{
	[SerializeField] Transform A = null;
	[SerializeField] Transform B = null;
	[SerializeField] float speed = 10;

	void Start () 
	{
		StartCoroutine("me");
	}

	IEnumerator me()
	{
		float t = Mathf.Abs(transform.position.x - B.position.x);
		float direction = (t) / (Mathf.Abs(t));
		Debug.Log ("direction " + direction);
		float x = -8;

		while (x != 8)
		{
			float i = (float)Math.Round((Time.deltaTime * speed) / t, 2);
			x += i;
			x = (float)Math.Round(x, 2);
			transform.position = new Vector3(x, transform.position.y);
			Debug.Log("i " + i + " x " + x);
			yield return null;
		}
	}
}

Edit: Corrected code formatting

You’d probably be best served by finding a tweening engine that will do this for you. HOTween is fantasic.

Your code has lots of strange stuff going on though. I just hacked this together but it might help:

float distance = transform.position.x - B.position.x;
float remainingDistance = Mathf.Abs(distance);

while (remainingDistance > 0F) {
	float i = Time.deltaTime * speed * distance;
	remainingDistance -= Mathf.Abs(i);
	if (remainingDistance < 0F) {
		if (i < 0) {
			i += remainingDistance;
		}
		else {
			i -= remainingDistance;
		}		
	}
	transform.position = new Vector3(transform.position.x - i, transform.position.y, transform.position.z);
	yield return null;
}

Are you aware of the Mathf.MoveTowards function? It does exactly what you want

IEnumerator me()
{
    Vector3 pos = transform.position;
    float target = B.position.x;
    
    while (pos.x != target)
    {
        pos.x = Mathf.MoveTowards(pos.x,target, Time.deltaTime * speed);
        transform.position = pos;
        yield return null;
    }
}