Hello, Im simply trying to get a click move feature to work, I have followed 2 YouTube videos and both times I have the same result , whilst theirs works perfectly of course.
Here is the script , the script results in the object moving to the same corner of the canvas each time, regardless of the click position, once there clicking again does nothing.
Im thinking it has to just be something stupid that I have missed at this point so any help would be very much appreciated !
alt text
private float speed = 40;
private Vector3 targetPosition;
private bool isMoving = false;
void Update()
{
if (Input.GetMouseButton(0))
{
SetTargetPosition();
}
if (isMoving)
{
Move();
}
}
void SetTargetPosition()
{
targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
targetPosition.z = transform.position.z;
isMoving = true;
}
void Move()
{
transform.rotation = Quaternion.LookRotation(Vector3.forward, targetPosition);
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
if (transform.position == targetPosition)
{
isMoving = false;
}
}
}