Can anyone tell me what’s wrong with this code?
using UnityEngine;
using System.Collections;
public class MoveUnits : MonoBehaviour
{
public float speed;
private Vector3 target;
public bool unitIsSelected = false;
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, Mathf.Infinity))
{
if (hit.transform.tag == "Unit")
{
Projector proj = GetComponent<Projector>();
unitIsSelected = true;
{
if (Input.GetMouseButtonDown(1))
{
target = new Vector3(hit.point.x, 0.5f, hit.point.z);
}
if(target != null)
{
Vector3 difference = target - transform.position;
Vector3 direction = difference.normalized;
float moveAmount = speed * Time.deltaTime;
if(difference.magnitude < moveAmount)moveAmount = difference.magnitude;
{
transform.Translate(direction * moveAmount, Space.World);
}
}
}
}
}
}
}
}