Moving Pooled Object Towards - Lerp

Hello everyone…

The idea is that the user clicks on the screen, get those coordenates and send the created pooled object towards that position.
It’s all good, the object is created and it moves but there’s a couple of issues.

Now, here’s what have:

When user clicks

			playerPosition = player.transform.position;

			//Get users input position(where they shoot);
			userInputPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);


			Debug.Log ("User clicked here:" + userInputPosition);
		Debug.Log ("Here's The player:" + playerPosition);

		projectile = ObjectPoolManager.CreatePooled( object2Spawn, transform.position, player.transform.rotation ).GetComponent<bulletActions>();

So, all here goes well and here goes moveTowards

public void moveTowards (Vector3 direction) 

		transform.position = Vector3.Lerp(transform.position, direction, moveSpeed);
		Debug.Log ("Direction is :" + direction);
		Debug.Log ("Position is" + transform.position);

So. First issue.

-The created object isn’t moving towards where the user clicked.(what am I not seeing?);
-The created object just moves instantly towards the point but cuts short to the distance it should do.

-Why is the object not moving towards the point im giving?
-Instead of moving to the exact location the user inputed(which is…not doing) move towards that location but keep going(the same trajectory) if the coords are reached.
-How could I slow down the movement of the object.

Here’s a couple of debugs:

User clicked here:(1.2, 0.8, -10.0)

Here's The player:(1.0, 0.4, 0.0)

Direction is :(1.2, 0.8, -10.0)

Position is(1.1, 0.6, -5.0)

..Second test

User clicked here:(-0.3, -1.3, -10.0)

Direction is :(-0.3, -1.3, -10.0)

Position is(0.4, -0.5, -5.0)

Here's The player:(1.0, 0.4, 0.0)

Instant movement issues:

There are two problems with this code. The first is that for an object to move over time, you need to move it just a bit each frame. So your movement line of code (second script line 4), needs to be executed repeatedly. This means you need to either call ‘moveTowards’ repeatedly in Update() or you need to turn moveTowards() in to a coroutine and exectue line 4 repeatedly until done. The second problem is the final parameter you are passing to the Lerp. Used this way, it needs to be a fraction of the total distance. For smooth movement, usually the last parameter would be ‘Time.deltaTime * moveSpeed’. Putting the two issues together, you might recode line 4 as:

while (Distance(transform.position, direction) > 0.05) {
    transform.position = Vector3.Lerp(transform.position, direction, moveSpeed * Time.deltaTime);

Note that a better name for your ‘direction’ variable here would be ‘destination’.

As for your second issue, you need to read up on Camera.ScreenToWorldPoint(). For most uses (especially for a Perspective cameras), you need to pass the distance in front of the camera for the calculation as the ‘z’ parameter to the point passed. The way you use it here will always return the position of the camera for a Perspective camera.