So when i click the object with the tag building it will move towards it, but it doesn’t it just jitters?
It doesn’t move Y As well.
Code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Attack : MonoBehaviour {
public float speed;
public Scrollbar Health;
private Vector3 target;
void Start()
{
target = transform.position;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = transform.position.z;
RaycastHit hitInfo = new RaycastHit();
bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
if (hit)
{
Debug.Log("Hit " + hitInfo.transform.gameObject.name);
if (hitInfo.transform.gameObject.tag == "Building")
{
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
}
}
}
}
}