Auto-Movement help

Hey guys,
I’m a little stuck on some code I’m trialing out. Basically I’m trying to get my character to move to 3 areas (Left, Right and back to the Middle). I can get it to move the player directly but I’m having trouble trying to get them to move gradually to each spot.
Below is what I’ve managed to work out- well follow instructions, as I’m just begging my journey into programming.

using UnityEngine;
using System.Collections;

public class Movements : MonoBehaviour {
	
	public float currentSpeed;
	

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
		if( Input.GetKey( "a" ) ) // left
	{				
		transform.position = new Vector3(-6, 1.23f, 0);	
	}
		
		if( Input.GetKey( "s" ) ) // right
	{
			
		transform.velocity = new Vector3(0,0,0);
	}
			
		if( Input.GetKey( "d" ) ) // right
	{
		rigidbody.velocity = new Vector3(6,0,0);	
	//	transform.position = new Vector3(6, 1.23f, 0);
	}
		
		
}
}

Thanks to anyone

Here’s an example of a simple smooth movement. The basic principle is to have a Vector3 member that contains the position where the object should go towards.

In the Update, your targetPosition can be set, but the object also gets moved towards the targetPosition. Notice that the lower the smoothing facter, the slower the object will reach its destination. Hope this helps you on your way, Cheers :wink:

public class Movements : MonoBehaviour {

    public float smoothing = 1.0f;
    private Vector3 targetPosition;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () 
    {
        if( Input.GetKey( "a" ) ) // left
        {          
           targetPosition = new Vector3(-6, 1.23f, 0);  
        }

        if( Input.GetKey( "s" ) ) // right
        {
            targetPosition = new Vector3(0,0,0);
        }

        if( Input.GetKey( "d" ) ) // right
        {
            targetPosition = new Vector3(6, 1.23f, 0);
        }

        //Lerp the object position towards the target Position
        transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothing);
    }
}

ps: please mark as answered if this helped you, greetz

works perfectly! The only issue is that the player now bobs up and down say 0.02f. Other than that it work exactly how i wanted it to.