My script to move a gameobject to the closest tagged gameobject doesn't work
I have 2 tagged gameobjects, and I would like a gameobject (which has the script below attached to it) to move to the closest gameobject that has the tag. However, it doesn't. Please can someone kindly tell me why? Thanks`enter code here`
using UnityEngine;
using System.Collections;
public class moveBomb : MonoBehaviour
{
public GameObject object1;
private GameObject[] objects1;
private GameObject nearestTarget;
private float distance;
private float curDistance;
private Vector3 pos;
private Vector3 diff;
private float speed = 1.5f;
// Use this for initialization
void Start()
{
nearestTarget = null;
distance = Mathf.Infinity;
pos = transform.position;
objects1 = GameObject.FindGameObjectsWithTag("target");
FindClosesttarget();
}
void FindClosesttarget()
{
foreach (GameObject obj in objects1)
{
diff = obj.transform.position - pos;
curDistance = diff.sqrMagnitude;
if(curDistance < distance)
{
nearestTarget = obj;
object1 = nearestTarget;
distance = curDistance;
}
}
}
// Update is called once per frame
void Update()
{
if (object1 != null)
{
transform.position = Vector3.MoveTowards (transform.position, object1.transform.position, speed);
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "target")
{
Destroy(transform.gameObject);
}
}
}
Answer by Kade Brown · Oct 18, 2015 at 03:08 AM
Try using this:
using UnityEngine;
using System.Collections;
public class moveBomb : MonoBehaviour
{
private GameObject[] objects1;
private GameObject nearestTarget;
private float distance;
private float curDistance;
private Vector3 pos;
private float speed = 1.5f;
// Use this for initialization
void Start()
{
nearestTarget = null;
distance = Mathf.Infinity;
pos = transform.position;
objects1 = GameObject.FindGameObjectsWithTag("target");
FindClosesttarget();
}
void FindClosesttarget()
{
foreach (GameObject obj in objects1)
{
curDistance = (obj.transform.position - pos).magnitude;
if(curDistance < distance)
{
nearestTarget = obj;
distance = curDistance;
}
}
}
// Update is called once per frame
void Update()
{
if (nearestTarget != null)
{
transform.position += nearestTarget.transform.position * speed * Time.deltaTime;
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "target")
{
Destroy(transform.gameObject);
}
}
}
Try to manually move it rather than using functions that move it for you because if you don't know exactly how those functions work, you may not get the result you expected. Also try to cut down on unnecessary code. For instance:
diff = obj.transform.position - pos;
curDistance = diff.sqr$$anonymous$$agnitude;
is the same as:
curDistance = (obj.transform.position - pos).magnitude;
Your answer
Follow this Question
Related Questions
Movement Unit (RTS) 0 Answers
Car from Unity's standard assets giving me of null reference errors 0 Answers
How do I resolve, get_isActiveAndEnabled can only be called from the main thread error 1 Answer
How to make an object move in the direction another object is facing on 2 axis? 1 Answer
Spawn added objects into a circle 1 Answer