smooth transition between two points

Hi,

I have A problem with my code.
I have one centerblock with two handles on each side. When I move the centerblock with Get axis (Horizontal) the handles move with the block.
At a certain moment, I want to hit my Spacebar and pull the handles to the left and the right.
I kept in mind that the objects cannot cross each other and that there is a wall on the left as well as on the right.
For so far the script is doing that.

The handles now go for either the first position or the last position. (on/off idea)
The thing I would like to add in this script is: I would like to make this transition between the positions smoothly.

I tried to use Lerp: Unity - Scripting API: Vector3.Lerp
and moveTowards Unity - Scripting API: Vector3.MoveTowards.
The gif is showing the movements to give a clear picture.
See the script below.

![using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Uber_control : MonoBehaviour {
	public float xMin, xMax;
	public float moveSpeed = -3f;
	private Rigidbody rb;
	public float HandleSpeed = 1f;
	public GameObject Uber;
	public GameObject leftside;
	public GameObject rightside;
	public GameObject Block_left;
	public GameObject Block_right;




	void Start()
	{
		//Start position handles
		leftside.transform.position = (Uber.transform.position + new Vector3 (5, 0,0)) ; 
		rightside.transform.position = (Uber.transform.position - new Vector3 (5, 0,0)); 

		rb = GetComponent<Rigidbody>();

	}

	void FixedUpdate ()
	{
		//move the centerblock (helper)
		float moveHorizontal = Input.GetAxis ("Horizontal");
		Vector3 movement = new Vector3 (moveHorizontal, 0.0f, 0.0f);
		rb.velocity = movement * moveSpeed;
		rb.position = new Vector3 (
			Mathf.Clamp (rb.position.x, xMin, xMax),
			0.0f,
			0.0f
		);
	}
		void Update ()
		{


	leftside.transform.position = (Uber.transform.position + new Vector3 (5, 0,0)) ; 
	rightside.transform.position = (Uber.transform.position - new Vector3 (5, 0,0)); 
	
		if (Input.GetKey ("space")) {
			//leftside.transform.position = (Uber.transform.position + new Vector3 (5, 0,0)) ; 
			//rightside.transform.position = (Uber.transform.position - new Vector3 (5, 0,0)); 

		  
			leftside.transform.position = (Block_left.transform.position); 
			rightside.transform.position = (Block_right.transform.position); 
		}
				
	}

}][1]	

Thanks in advance for your help.

You can use AnimationCurves, they’re great for adding ease to anything that makes some kind of transition. Something like this :

position = Vector3.Lerp(position1, position2, curve.Evaluate(value));

Also, note it is advised not to assign a rigidbody’s velocity, but rather add it force, using Rigidbody.AddForce(). Just saying…

Hi thank you, unforunatly I could not get it to work as expected.
first thing I did: rb.velocity = movement * moveSpeed; messed up my controlls It stopped stopping when hitting my wall. also it when slower on the end and faster in the beginning so I kept the rb.Velocity.

Second point:I played arround with the AnimationsCurves. I tried different options. also once, default loop ect.
Using the Curve makes the handles get back on the position were it got released. So if I stop pressing the spacebar the handle springs back to the old position. when pressing spacebar agian it does not start over it starts where the first time stopped. When reaching the end the Curve action doenst work any more so it does only work once.
Maybe I did something wrong or need to approach iit different…?
@UnityCoach