I decided to make a new thread for this, since the last thread’s title was very specific, and I didn’t want to confuse new readers.
Like I said in my previous thread, I’m following this tutorial (by now I’ve finished following it), and I had some errors. I got those solved, but there are things I still need to to with it.
Basically I need to integrate finding a closet object into it, for which I’ve been following this tutorial. The tutorial looks at 3D, and I need to have it work for 2D.
Here are both my scripts (altered from the tutorial ones):
using System.Collections;
using UnityEngine;
public class FindClosestObject : IComparer {
private Transform compareTransform;
public FindClosestObject (Transform compTransform) {
compareTransform = compTransform;
}
public int Compare (object x, object y) {
var xCollider = x as Collider2D;
var yCollider = y as Collider2D;
Vector3 offset = xCollider.transform.position - compareTransform.position;
float xDistance = offset.sqrMagnitude;
offset = yCollider.transform.position - compareTransform.position;
float yDistance = offset.sqrMagnitude;
return xDistance.CompareTo(yDistance);
}
}
using System;
using UnityEngine;
public class Sense : MonoBehaviour {
public Vector3 boxSize = new Vector3 (0.6875f, 2.25f, 5f);
public LayerMask checkLayers;
private void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
Collider2D[] colliders = Physics2D.OverlapBoxAll(transform.position, boxSize, checkLayers);
Array.Sort(colliders, new FindClosestObject(transform));
foreach (Collider2D item in colliders) {
Debug.Log(item.name);
}
}
}
private void OnDrawGizmos() {
Gizmos.DrawCube(transform.position, boxSize);
}
}
The person in the tutorial doesn’t talk much, and doesn’t comment the code btw.
The way I understand it, is it gets the transform or collider of objects within the “Overlap” object, and also checks that the targeted object is in a specific Layer. It sorts them within the array in order of range to the object holding the Sense script. And then prints the objects’ names to the console.
Before trying to convert it to 2D, I put in some 3D objects in the scene, and attached the Sense script to a cube, and it works exactly like in the video.
But I need it to work for 2D colliders.
I tried converting it by simply changing all Collider into Collider2D, Physics into Physics2D and changing from an OverlapSphere to OverlapBoxAll (I did All because otherwise it’s not compatible with the “Collider2D” array for some reason, and gives me an error saying “Cannot convert from UnityEngine.Collider2D to UnityEngine.Collider2D[ ]”).
It does seem to work somewhat, but instead of outputting only the objects in the layer I set (which is “Interactible”), it outputs a very specific set of any objects. What I mean by that is, I have a lot of sprite elements in my scene (all with 2D colliders), some have scripts attached and some don’t. And they are all set out in multiple sorting layers (of the Sprite Renderer) like Background, Foreground, and Midground.
And it doesn’t seem to only detect ones with either scripts or no scripts, or ones on specific Sorting Layers. It also doesn’t detect random objects.
To make this clearer, here’s a list of some of the objects it detects:
- LeftDoor
- House(Outside)
- House(Inside)
- BackDoor
- Stairs
- Shelf
All these objects are different, in that they aren’t all in the same Sorting Layer, neither are they all in the same Layer. And only some of them have scripts attached (not the two scripts I put above, but just regular scripts that have to do with their functionality in the game). And of course, there are some objects it doesn’t detect, which are also all in different layers and such.
So even though it seems to have picked a random assortment of objects, it’s not completely random. And I have no idea why it’s happening.