I am moving a player object around an arena with mouse clicks, but the object stops when it gets to the position the mouse was clicked at. How can I make it continue moving even after that? What I am looking for is a “move in the direction of X position” Here’s my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EzrealCode : MonoBehaviour
{
[SerializeField] float heartPoints = 6.00f;
[SerializeField] float manaPoints = 8.00f;
[SerializeField] float speed = 5f;
Vector2 lastClickedPos;
bool moving;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
lastClickedPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
moving = true;
}
if (moving && (Vector2)transform.position != lastClickedPos)
{
float step = speed * Time.deltaTime;
transform.position = Vector2.MoveTowards(transform.position, lastClickedPos, step);
}
else
{
moving = false;
}
}
}