I created a new project with 3 spheres.
The code just moves one of the spheres back and forth between the other two.
I has a blurry/ ghost like movement. Sometimes it jerks.
I used Unity 2020.2.4f1.
My monitors are 144Hz. Unfortunately, I don’t a way to test otherwise.
Profiler is showing update is running at 4 to 1 ms (250 to 1000 FPS).
Any suggestions for troubleshooting or settings?
[SerializeField] float adjustableVar;
public Transform LeftSphere, RightSphere, myT;
public bool goLeft = false;
bool isMoving = false;
void Update()
{
if (LeftSphere == null) LeftSphere = GameObject.Find("/LeftSphere").transform;
if (RightSphere == null) RightSphere = GameObject.Find("/RightSphere").transform;
if (myT == null) myT = transform;
if ((RightSphere.position - myT.position).magnitude <= 1) goLeft = true;
if ((LeftSphere.position - myT.position).magnitude <= 1) goLeft = false;
// Looks terrible, even at 5f.
//if (goLeft)
// myT.position = Vector3.MoveTowards(myT.position, LeftSphere.position, adjustableVar * Time.deltaTime); // Last var is max move
//else
// myT.position = Vector3.MoveTowards(myT.position, RightSphere.position, adjustableVar * Time.deltaTime);
// Performance is better, but still looks terrible.
if (goLeft)
StartCoroutine(moveToX(myT, LeftSphere.position, adjustableVar)); // Last variable is time duration
else
StartCoroutine(moveToX(myT, RightSphere.position, adjustableVar));
}
private void FixedUpdate()
{
// Same issue as update.
//if (goLeft)
// myT.position = Vector3.MoveTowards(myT.position, LeftSphere.position, adjustableVar * Time.fixedDeltaTime);
//else
// myT.position = Vector3.MoveTowards(myT.position, RightSphere.position, adjustableVar * Time.fixedDeltaTime);
}
IEnumerator moveToX(Transform fromPosition, Vector3 toPosition, float duration)
{
if (isMoving) yield break; //exit if this is still running
isMoving = true;
float counter = 0;
Vector3 startPos = fromPosition.position;
while (counter < duration)
{
fromPosition.position = Vector3.Lerp(startPos, toPosition, counter / duration);
counter += Time.deltaTime;
yield return null;
}
isMoving = false;
}