checking if any objects are within a certain range

Hi there,

how can i check if any objects are within a certain range of other objects?? Do i need to cycle thru all active objects and check it “manually” or is there some builtin function which returns objects within that range??

thanks in advance,
cheers,
Ello

You could use a spherical collider.

Try that out.

thank you very much

CheckSphere is pretty cool, I need to go through the docs and see what else I have missed.

i still have an issue… seems like CheckSphere also reacts to its own Collider… i am using this to test

if (Physics.CheckSphere (transform.position, 1.0f, kDefaultRaycastLayers))	baseScale*= 0.9f;

but my objects are all scaling down as if there where everywhere colliders.

any ideas?

cheers,
Ello

CheckSphere does not require a collider at all. It can be done from an entirely different script/gameObject.

If you have a SphereCollider already you should probably use JRavey’s idea, and set that Collider to IsTrigger, and then use OnTriggerEnter to set events.

thanky for your input… i still have no success with it.

here is my code for creating the objects:

using UnityEngine;
using System.Collections;

public class createLifeforms : MonoBehaviour {

	private GameObject s;
	private ArrayList myNodes;
	int _w = 200;
	int _number = 100;
	void Start ()
	{
		
		for (int i = 0; i < _number; i++)
		{
			myNodes = new ArrayList();
			s = GameObject.CreatePrimitive(PrimitiveType.Sphere);
			s.transform.position = new Vector3 ((Random.value*_w)-_w/2,0,  (Random.value*_w)-_w/2);
			s.AddComponent("lifeform");
			myNodes.Add(s);
		}
	}

	// Update is called once per frame
	void Update () {
		Destroy(this);
		}
}

and this is the code for each object:

using UnityEngine;
using System.Collections;

public class lifeform : MonoBehaviour {

	float offset;
	int _maxScale = 10;
	float baseScale;
	
	void OnTriggerEnter() {
		Destroy(this);
	}
	// Use this for initialization
	void Start () {
		offset = Random.value*360.0f;
		float _s = (Random.value*_maxScale)+1;
		baseScale = _s;
		collider.isTrigger = true;
	}
	
	float _speedX = 0.1f*(Random.value*5-2.5f);
	float _speedZ = 0.1f*(Random.value*5-2.5f);
	// Update is called once per frame
	void Update () {
		Color col = renderer.material.color;
		col.r = 1.5f + 1.5f*Mathf.Sin(Time.time + offset);
		col.g = 0.0f;
		col.b = 0.0f;
		renderer.material.color = col; 
		_speedX += 0.2f*Random.value-0.1f;
		_speedZ += 0.2f*Random.value-0.1f;
		_speedX = Mathf.Clamp(_speedX,-2.5f,2.5f);
		_speedZ = Mathf.Clamp(_speedZ,-2.5f,2.5f);
		transform.Translate((0.1f*_speedX), 0,  (0.1f*_speedZ));
		Vector3 position = transform.position;
		position.x = Mathf.Clamp(transform.position.x, -200f,200f);
		position.z = Mathf.Clamp(transform.position.z, -200f,200f);
		transform.position = position; 
		Vector3 scale = transform.localScale;
		scale.x = scale.y = scale.z = baseScale*(0.5f+col.r*0.25f);
		transform.localScale = scale;
		if (position.x<-100) _speedX += 0.1f*Random.value;
		if (position.z<-100) _speedZ += 0.1f*Random.value;
		if (position.x>100) _speedX -= 0.1f*Random.value;
		if (position.z>100) _speedZ -= 0.1f*Random.value;
	}
}

i guess its just some beginner fault on my side…

cheers,
Ello