MoveTowards does not want to work

When I press the key, the project does not want to move to Target, why?

using UnityEngine;
using System.Collections;

public class LerpSpeed : MonoBehaviour {

	// The target marker.
	public Transform target;

	// Speed in units per sec.
	public float speed;

	void OnTriggerStay(Collider other)
	{
		// The step size is equal to speed times frame time.
		float step = speed * Time.deltaTime;

		if (Input.GetKeyDown (KeyCode.E)) {
			transform.SetParent(other.transform);

		}
		if (Input.GetKeyDown (KeyCode.E)) {

			transform.position = Vector3.MoveTowards (transform.position, target.position, step);
		}
	}



}

I see problems

  1. GetKeyDown returns true only at the frame when the key is pressed down, not when it’s held down
  2. GetKeyDown won’t work if called from OnTriggerStay, as it’s not called every frame, but every physics step (and GetKeyDown returns true only in the frame when the key was pressed)
  3. You are using Time.deltaTime in a physics-bound message, where Time.deltaTime is irrelevant. I’d say use fixedDeltaTime, but:

Instead, I’d recommend to detect enter and exit using OnTriggerEnter and OnTriggerExit, and doing the actual motion in Update or FixedUpdate (depends on whether you want to sync with physics or not)

change GetKeyDown to GetKey