Collisions in range in a 2D space

Hi im working on a minimap to a (MMO)RPG and i’ve almost got everything to work but i have a huge problem by checking whether certain gameobjects are in range of the player (center point) atm im using Physics.OverlapSphere witch work some what okay, however since the physic is “ball” formed this gives huge problem if the gameobject is far below me or are in a wierd diagonally distance from me since i wont be inside the overlapsphere. an other thing i could do was to find all game objects and check the distance but since this is really heavy/costable to do this is not an option. what i basiclly want is a way to detect every thing that is some radius away from me in only the x and z axis and completely ignore the height between the objects. i’ve already tried to set transform Y to 0 but this will fuck up too if an enemy/trainer etc. is located high up. anotherthing i’ve tried was Capsulecollider trigger but this wont work for me what- so -ever. An idea i have was to work with a round vector space but i have no idea how this could be done. anotherthing could be if i could make an overlapcapsule instead of sphere and make it very tall but as standart i cant do this by coding. i have unity pro 3.4 and im open to try every solution :slight_smile:

EDIT: I’ve been looking into Physics.CapsuleCastAll but not sure if that works like i want.

EDIT: Since there have been some misunderstandings about the text above i try once more:

is there a alternative to Physics.OverlapSphere that can detect everything in a radius around the player in a infinity height?

https://docs.google.com/drawings/d/1FGf0BQsoUsvByW8a-XnWF7c-qPd4lFxsQ1eKJBKglyw/edit

a drawing of how i want int to work ^^ the player will be the center of that capsule and everything within (or above) should be found and returned into a array of gameobjects or coliders and lets say oval 4 and 1 have the same Z and X cords but even that the Y is A LOT higher on oval 4 will it still seems like they’re at the same spot (2D space) which is easy if there is a way to make a infinity high capsule that can return all objects it hit. hope this clear things up abit.

thanks Jackie

BURP

it’s basic pythagore stuff

if ( (x1-x2)*(x1-x2) + (z1-z2)*(z1-z2) < range*range)
{
     // object1 ( with coordinates x1, y1, z1) is in range of object2 ( with coordinates x2, y2, z2) on the x,z plane
}

No need to calculate the actual distance between the 2 objects, the square distance is enough for this kind of test.

You might want to get back to your highschool math lessons before trying to develop a (MMO)RPG or any game for that matter. This kind of problem is pretty trivial compared to what is involved in the development of any kind of game.

Bitch please,

I have a bachelor in computer science, and yet you haven’t given me any useful advise for my question, maybe you should take some primary school reading classes? :slight_smile: if you didn’t understand the question you could have asked, and FYI since English isn’t my first language and I also got a bad case of dyslexia that seems to be the case… anyhow, Take care…

Concept: Add a MiniMapTrigger GameObject to your model… It will have a huge capsule collider on it, a radius of whatever your radius is, and a height of 2x the height of your map. (make it way bigger though)

Now, This trigger simply captures any person or object that “could” be on your mini map. When someone or something leaves it, it removes it from the list. (Use the List object)

Now, whenever you draw your mini map, just get the list that is on that trigger and draw away. :wink:

ah i see so i wanna have a Physics → Capsule Collider bound to my mini map cam or player and add a trigger to it or do i misunderstand you? - last time i did this it didn’t work, but well, i might have done something wrong? if you could point me into some samples/tutorials or maybe post some basic code to it, it would help me out a lot.

Thanks a lot tho, please tell me if i understood everything right :slight_smile:

Your pretty much right on the money. Its not a hard concept.

// basic tracker
import System.Collections.Generic;

var radius : float = 50.0;
var heigth : float = 2000.0;
var tags : List.<String>;
private var list : List.<GameObject>;


function Start(){
	// basic list
	if(tags == null){
		tags = new List.<String>();
		tags.Add("Monster");
		tags.Add("Treasure");
		tags.Add("Key");
		tags.Add("Powerup");
		tags.Add("Player");
	}
	list = new List.<GameObject>();
	if(collider)DestroyImmediate(collider);
	var col : CapsuleCollider = gameObject.AddComponent(CapsuleCollider);
	
	
	col.radius = radius;
	col.height = heigth;
	col.isTrigger = true;
}

// get an array from this controller
function GetObjectsForMiniMap() : GameObject[]{
	return list.ToArray();
}

function Update(){
	// maintain list and see if anything is no longer there
	for(var i=0; i<list.Count; i++){
		if(list[i] == null){
			list.RemoveAt(i);
			i--;
		}
	}
}

function OnTriggerEnter (other : Collider) {
	// find the root object and see if we need to add it.
	var root = other.transform.root.gameObject;
	var tag = root.tag;
	if(tags.Contains(tag)){
		if(!list.Contains(root)) list.Add(root);
	}
}

function OnTriggerExit (other : Collider) {
	// if it leaves the area, remove it.
	var root = other.transform.root.gameObject;
	if(list.Contains(root)) list.Remove(root);
}

Danm you javascript!!! hehe thanks i’ll rewrite into C# and come back then i’ve tested it out thanks for you time and understanding :slight_smile: Cheers!

okay i did as you said and tested it out but the only thing it wanna add to my list i the player object only, everything else seems to be ignored (i’ve made a few “evil cubies”) seems quietly weird that it wont pick up any other items :S also this: CapsuleCollider col = gameObject.AddComponent(CapsuleCollider); give me an error

CapsuleCollider col = gameObject.AddComponent();

I think is the C# comparable

It wont work at all without that line. :wink:

Now remember, you need to add this to an “empty” gameobject and reference that game object in your minimap and get the current list like this:

TriggerCollector myObject; // assign this at editor time…

GameObject[ ] objectsForMiniMap = myObject.GetObjectsForMiniMap()

it still seems to mock me, it only show that the player is getting added even if i jump right on top of my evil cubies :S

am i doing some thing wrong?

source:

using UnityEngine;
using System.Collections.Generic;

public class MinimapScript : MonoBehaviour {

    public Transform target;
    public bool rotateWithTarget = true;
    Quaternion q;
    List<Collider> cList = new List<Collider>();

	void Start ()
    {
        if (rigidbody)
        {
            rigidbody.freezeRotation = rotateWithTarget;
        }
        q = transform.rotation;
        CapsuleCollider col = this.gameObject.AddComponent<CapsuleCollider>();
        col.height = Mathf.Infinity;
        col.radius = 10;
        col.isTrigger = true;
	}
	
	void Update ()
    {
        if (target)
        {
            transform.position = target.position;
            if (rotateWithTarget)
            {
                transform.rotation = q;
            }
            else
            {
                transform.eulerAngles = new Vector3(90, 0, target.eulerAngles.y * -1);
            }
        }
	}

    void OnTriggerEnter(Collider other)
    {
        cList.Add(other);
        Debug.Log(other.name + " Added to the list.");
    }

    void OnTriggerExit(Collider other)
    {
        cList.Remove(other);
        Debug.Log(other.name + " Removed from the list.");
    }
}

woops forgot the isTrigger above. Added it…

I dont ahve the time to test the code, but here it is with the other stuff embedded.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MinimapScript : MonoBehaviour {
 
	public float radius = 50.0f;
	public float height = 2000.0f;
	public Transform target;
	public bool rotateWithTarget = true;
	Quaternion q;
	List<GameObject> list = new List<GameObject>();
	List<string> list = new List<string>();
 
	void Start (){
		// tags list
		if(tags == null){
			tags = new List<String>();
			tags.Add("Monster");
			tags.Add("Treasure");
			tags.Add("Key");
			tags.Add("Powerup");
			tags.Add("Player");
		}
		if (rigidbody)
			Destroy(rigidbody);
		q = transform.rotation;
		list = new List<GameObject>();
		if(collider)DestroyImmediate(collider);
		CapsuleCollider col = this.gameObject.AddComponent<CapsuleCollider>();
		col.height = height;
		col.radius = radius;
		col.isTrigger = true;
	}
	
	void Update(){
		// maintain list and see if anything is no longer there
		for(int i=0; i<list.Count; i++){
			if(list[i] == null){
				list.RemoveAt(i);
				i--;
			}
		}
	}
	
	void LateUpdate (){
		if (target){
			transform.position = target.position;
			if (rotateWithTarget)
				transform.rotation = q;
			else
				transform.eulerAngles = new Vector3(90, 0, target.eulerAngles.y * -1);
		}
	}
 
	void OnTriggerEnter(Collider other)
	{
		cList.Add(other);
		Debug.Log(other.name + " Added to the list.");
	}
 
	void OnTriggerExit(Collider other)
	{
		cList.Remove(other);
		Debug.Log(other.name + " Removed from the list.");
	}
	
	void OnTriggerEnter (Collider other) {
		// find the root object and see if we need to add it.
		GameObject root = other.transform.root.gameObject;
		string tag = root.tag;
		if(tags.Contains(tag)){
			if(!list.Contains(root)) list.Add(root);
		}
	}

	void OnTriggerExit (Collider other) {
		// if it leaves the area, remove it.
		GameObject root = other.transform.root.gameObject;
		if(list.Contains(root)) list.Remove(root);
	}
}

i need to add an rigidbody to the cube and it kinda worked its still very messy an trigger exit not working and only its going to be anoying to add rigidbodies to every object :S

EDIT: Never mind still not working, or well it works but only once in a while… :frowning:

double burp

Derp