Moving gameObject forward

Hey,

I’m trying to move my gameobject forward for specific vector by using MouseOver.When MouseExit it should move back.
Momently my object moves forward… just forward with speed. I don’t know how to say how far it should move and how to go back…?

void OnMouseOver() {
	gameObject.renderer.material.color = Color.cyan;
	gameObject.transform.position = new Vector3(transform.position.x,   transform.position.y, transform.position.z + 0.1f);
}
    
void OnMouseExit(){
	gameObject.renderer.material.color = Color.white;
}

Thanks for taking a look

As with most things in Unity, there are multiple approaches and solutions. Here is one solution:

using UnityEngine;
using System.Collections;

public class Bug25b : MonoBehaviour {

	public float speed = 2.0f;  // Units per second 

	private Vector3 startPos;
	private Vector3 forwardPos;
	private Vector3 toPos;

	void Start() {
		startPos = transform.position;
		toPos = startPos;
		forwardPos = transform.position + transform.forward * 1000000.0f;
	}

	void Update() {
		transform.position = Vector3.MoveTowards(transform.position, toPos, speed * Time.deltaTime);
	}

	void OnMouseEnter() {
		gameObject.renderer.material.color = Color.cyan;
		toPos = forwardPos;
	}
	
	void OnMouseExit(){
		gameObject.renderer.material.color = Color.white;
		toPos = startPos;
	}
}

public Vector3 orgPos,fwdPos;
void Start()
{
orgPos=transform.position;
fwdPos=(transform.position.x,transform.position.y,transform.position.z-0.1f);
}
void OnMouseEnter()
{
transform.position=fwdPos;
}
void OnMouseExit()
{
transform.position=orgPos;
}