Experience drops with magnet effect(Performance help)

(maybe look at the flash game ‘Pixel Purge’ if ur unsure wat im talking about)

well wat the code does is when an enemy dies, he drops a certain amount of experience orbs, and when the player is within a certain range, those orbs will travel towards the player.

one issues i can already tell is im asking each orb every frame 'is the player within range couple thousands of times; do note there will be easily over 200 orbs on the screen at once.

The experience orbs uses box collider to check collisions and all have rigidbodies
even without the magnet code on, the game seems to lag as well a little when picking up orbs.

The code works fine and all, only the travel can be imporved, im not entirely happy with it and performance wise, when there’s many orbs close to the player the game lags a fair bit, especially when im on top of an enemy and kill him big lag will occur as well, asking help to improve on that

public static var MagnetStrength : float = 3;

var target : Transform;
var smoothTime = 0.06;
private var thisTransform : Transform;
private var velocity : Vector2;

function Start()
{
	thisTransform = transform;
	target = GameObject.FindWithTag("Player").transform;
}

function Awake () {
	rigidbody.AddForce(Random.Range(-500,500), Random.Range(-500,500), 0);
}

function Update () {
	//player picking up expDrops
	var dist = Vector3.Distance(target.transform.position, thisTransform.position);
	if (dist < MagnetStrength)
	{
		thisTransform.position.x = Mathf.SmoothDamp( thisTransform.position.x, target.position.x, velocity.x, smoothTime);
		thisTransform.position.y = Mathf.SmoothDamp( thisTransform.position.y, target.position.y, velocity.y, smoothTime);
	}
}

function OnTriggerEnter (ExpDrop : Collider)
{
	if(ExpDrop.tag == "Player")
	{
    	Destroy(gameObject);
	}
}

You don’t need the distance between the player and the orb. Computing this requires a square root operation. Instead, compute the sqrMagnitude() of the vector between the player and the orb, and compare with MagnetStrength*MagnetStrength.

Use the profiler if you have bought Pro. This will tell you where exactly the slow-down comes from.

Do you need to check the distances every frame? What would happen if you tested half of the orbs one frame, and the other half in the next? Or, if you know that an orb is close enough to be picked up, test that every frame, and only check the ones that are outside this radius every other frame.

Another way to explore your problem… just pick up all the orbs without using your distance check. If the game does not slow down, then you know for sure it’s the distance check that is the cause.