Problem with Vector3.MoveTowards

Please help me.
I translated a code from javascript to c# here
and the previous code worked.
Now when i leftMouseClick somewhere i want the character to walk to there.
code:

using UnityEngine;
using System.Collections;

public class characterMovementScript : MonoBehaviour {


	public float smooth = 1f;

	private Vector3 targetPosition;

	public GameObject Target;

	void Awake(){
		targetPosition = transform.position;
	}
	void  Update (){
		if(Input.GetMouseButton(0))
		{
			Plane playerPlane = new Plane(Vector3.up, transform.position);
			Ray ray= Camera.main.ScreenPointToRay (Input.mousePosition);
			float hitdist= 0.0f;

			if (playerPlane.Raycast (ray,out hitdist)) {
				Vector3 targetPoint= ray.GetPoint(hitdist);
				targetPosition = ray.GetPoint(hitdist);
				Quaternion targetRotation= Quaternion.LookRotation(targetPoint - transform.position);
				transform.rotation = targetRotation;
			}
		}
			Target.transform.position = Vector3.MoveTowards (Target.transform.position, targetPosition, Time.deltaTime * smooth);
	}
}

Please help me.
Thanks in advance.
Ethan.

UPDATE:
When i run the game i get the following error:

UnassignedReferenceException: The variable Target of characterMovementScript has not been assigned.
You probably need to assign the Target variable of the characterMovementScript script in the inspector.
characterMovementScript.Update () (at Assets/scripts/characterMovementScript.cs:27)

@Cepheid

The error message that you edited into your question simply tells you that you haven’t assigned a gameObject to the variable Target in the inspector.

~

EDIT: think I found your problem…

So you’re creating a Plane called playerPlane at transform.position

Then you’re creating a ray which goes to where the mouse is clicking with an origin of transform.position (the camera’s position)

Then you’re setting hitdist using the function playerPlane.Raycast(ray, out hitdist)

And therefore the reason why hitdist is 0 every time is because the Plane.Raycast(Ray, out float) function sets the ‘out float’ to the distance between the Ray’s Origin and where the Ray intersects the Plane. In your case, the Ray’s Origin and the Plane’s Origin were both initialized to transform.position which means that there is 0 distance between them - hence why hitdist is 0 and then your Target is not moving.

What you should be using instead of a Plane is Physics.Raycast() - and you should use this variation of the function public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal); (it’s the one at the bottom of the Docs page). You don’t need to worry about the last 2 parameters, only the first 3 - so pass it your Ray, then a RaycastHit variable which will return information about the object that it hit, and then, of course, your maxDistance.

Also, you have an unnecessary variable Vector3 targetPoint in your inner IF statement which you should remove as you’ve already got the global variable Vector3 targetPosition

Hope all that made sense :stuck_out_tongue:

Your script works perfectly fine when I paste it into an empty project. The error you are getting hints at targetPosition being null, however as a public variable it should be exposed in the Inspector. This does not seem to be the case on your end, though.

You may be able to temporarily fix this issue by adding the following function to your characterMovementScript class:

void Awake()
{
     targetPosition = transform.position;
}