Reputation: 15
im trying to make a game where you have to merge 2 gameObjects to get the next one and so one. First i tried so that when the gameobject is released and it's touching another gameobject it deletes both of those and then instantiates the next one. But this didn't quite work bc of the triggers "covering" each other or some other wierd but. So i thought i could detect the collision so that every gameobject that touches the trigger gets added to a list and then removed on exit. What i'm not trying to figure out is how to calculate the distance between the gameObjects and if they are close enough (mergingthreshold) then they both get destryed and the next gameobject gets instantiated, but i can't quite figure out how to get this distance calculating and returning of what gameObject that is. So all help would be appreciated! (don't leave out any details, cause I'm quite the beginner) Thanks!
Here's the code I've gotten so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Merging : MonoBehaviour
{
List<GameObject> NearGameobjects = new List<GameObject>();
void Start()
{
}
void Update()
{
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == gameObject.tag)
{
Debug.Log("Enter!");
NearGameobjects.Add(col.gameObject);
}
}
void OnTriggerExit2D(Collider2D col)
{
if (col.gameObject.tag == gameObject.tag)
{
Debug.Log("Exit!");
NearGameobjects.Remove(col.gameObject);
}
}
}
Upvotes: 0
Views: 9341
Reputation: 90862
You could just order your list by the distance using Linq OrderBy
:
using SystemLinq;
...
public void CheckNearest()
{
if(NearGameobjects.Count == 0) return;
// This orders the list so the closest object will be the very first entry
var sorted = NearGameobjects.OrderBy(obj => (col.transform.position - transform.position).sqrMagnitude);
// currently closest
var closest = sorted.First();
if (Vector3.Distance(this.gameObject.transform.position, closest.transform.position) < mergingThreshold)
{
Destroy(gameObject);
Destroy(closetsObject);
Instantiate(NextPF, transform.position, Quaternion.identity);
}
}
private void OnTriggerEnter2D(Collider2D col)
{
// Rather use CompareTag
if (col.CompareTag(tag))
{
Debug.Log("Enter!");
NearGameobjects.Add(col.gameObject);
}
}
private void OnTriggerExit2D(Collider2D col)
{
if (col.CompareTag(tag))
{
Debug.Log("Exit!");
NearGameobjects.Remove(col.gameObject);
}
}
Upvotes: 1
Reputation: 15
For anyone wondering, this was the total code that worked perfectly thanks to CubeCrafter360!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Timeline;
public class Merging : MonoBehaviour
{
List<GameObject> NearGameobjects = new List<GameObject>();
GameObject closetsObject;
private float oldDistance = 9999;
public GameObject NextPF;
public float mergingThreshold = 0.3f;
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == gameObject.tag)
{
Debug.Log("Enter!");
NearGameobjects.Add(col.gameObject);
}
}
void OnTriggerExit2D(Collider2D col)
{
if (col.gameObject.tag == gameObject.tag)
{
Debug.Log("Exit!");
NearGameobjects.Remove(col.gameObject);
}
}
public void CheckNearest()
{
foreach (GameObject g in NearGameobjects)
{
float dist = Vector3.Distance(this.gameObject.transform.position, g.transform.position);
if (dist < oldDistance)
{
closetsObject = g;
oldDistance = dist;
}
}
if (Vector3.Distance(this.gameObject.transform.position, closetsObject.transform.position) < mergingThreshold)
{
Destroy(gameObject);
Destroy(closetsObject);
Instantiate(NextPF, transform.position, Quaternion.identity);
}
}
}
And then i just called the function in my movement script:
private void OnMouseUp()
{
GetComponent<Merging>().CheckNearest();
}
Upvotes: 0
Reputation: 192
If you're going to use the list and have added all objects into this list you could use a for each loop and check the distance between your main object and all the other objects in the list if its closer add that to the closets object
public class NewBehaviourScript : MonoBehaviour {
List<GameObject> NearGameobjects = new List<GameObject>();
GameObject closetsObject;
private float oldDistance = 9999;
private void Something()
{
foreach (GameObject g in NearGameobjects)
{
float dist = Vector3.Distance(this.gameObject.transform.position, g.transform.position);
if (dist < oldDistance)
{
closetsObject = g;
oldDistance = dist;
}
}
}
}
Upvotes: 5