Vector3.MoveTowards

Hi

I am currently trying to work out how to move an object (player) to another object once a mouse button has been clicked.

Moving the player using the following script works when no mouse click detect is required.

	using UnityEngine;
	using System.Collections;
	
	public class MoveToTarget : MonoBehaviour 
{
	public Transform target;
	public float speed;
		
	void Update()
		{

					float step = speed * Time.deltaTime;
					transform.position = Vector3.MoveTowards (transform.position, target.position, step);

		}
}

However when I add detecting mouse-click the player does move in the direction of the object but stops, waiting for another click before it moves again.

	using UnityEngine;
	using System.Collections;
	
	public class MoveToTarget : MonoBehaviour 
{
	public Transform target;
	public float speed;
		
	void Update()
		{
			if (Input.GetMouseButtonDown(0))
			    {
					float step = speed * Time.deltaTime;
					transform.position = Vector3.MoveTowards (transform.position, target.position, step);
				}
		}
}

Should I be using something other than void update?

Can someone point me in the right direction with regards to seeing what options are available other than void update?

Thanks

Try using GetMouseButton which returns true for a given button being held down, GetMouseButtonDown only returns true during a frame and the button is pressed.

@Landern

Cool, thanks. Good to know yet another trick :slight_smile: When I hold the button down it does indeed move the player to the target’s position. However to clarify (my apologies for not doing so previously) I would like to click and release the mouse button once and have the player travel to its new location. Currently if I release the mouse button the player stops.
Thanks