void Start()
{
targetPosition = transform.position;
plane = new Plane(Vector3.up, new Vector3(0, height, 0));
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out float distance))
{
targetPosition = ray.GetPoint(distance);
}
}
float distanceToDestination = Vector3.Distance(transform.position, targetPosition);
if (distanceToDestination > (Time.deltaTime * speed))
{
transform.position += (targetPosition - transform.position).normalized * speed * Time.deltaTime;
}
else
{
transform.position = targetPosition;
}
}
So far it moves towards mouse click, but I would like it to rotate first. I would think if I attach an empty (which represents the front of the Object) and then it rotates towards where the mouse was clicked. It also needs to stay at a height of y = 40 and rotate only on the y axis.