Travel from A to B in 3 seconds

Hi, I’m trying to make my Camera move from point A to B in 3 seconds when i press a button. It would be nice if it was smooth aswell (accelirating in the begining and slowing down at the end)
If anyone could help me with this that would be verry gald. I am using C# but if it’s written in Java I can probebly translate it

Thanks in advansed and sorry for my terrible English.

PS. I’m trying to move it to another Gameobject named HomePos.

Lots of ways to achieve this, here’s one just off the top of my head:

IEnumerator Move(Vector3 A, Vector3 B, float time, Transform target) {
  float speed = Vector3.Distance(A,B) / time; 
  Vector3 direction = (B - A).normalized;
  target.position = A;
  float elapsed = 0.0f;
  while (elapsed < time) {
    elapsed += Time.deltaTime;
    target.Translate(direction * speed * Time.deltaTime);
    yield return true;
  }
  transform.position = B;
}

Just typed in to forum so it may have errors :slight_smile:

See MoveObject, specifically the Translation function using MoveType.Time. To get the acceleration/deceleration effect, change “Vector3.Lerp(startPos, endPos, t);” to “Vector3.Lerp(startPos, endPos, Mathf.SmoothStep(0, 1, t));”

–Eric

I’m sorry but I am not familiar with IEnumerators, What am I supposed to do with this code and how do i define A,B,time and target?