(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);
}
}