x


Best approach for a distance based destroyer

I am creating a game which the player collect coins and keeps going up. I need this coins to disappear when the user is too far.

What I am doing right now is adding a component to the coins with the following:

public class PlayerDistanceBasedDestroyer : MonoBehaviour
{
    private Transform target;

    void Awake()
    {
       this.target = GameObject.Find("Player").transform;
    }

    void LateUpdate ()
    {
       if ( (this.target.position - this.transform.position).y > PlayerFalling.FALLING_WARNING_DISTANCE )
       {
         ObjectPoolManager.DestroyPooled( gameObject );
       }
    }
}

Since the profiler showed that it was called a lot I changed this to the following:

public class PlayerDistanceBasedDestroyer : MonoBehaviour
{
    private Transform target;

    void Awake()
    {
       this.target = GameObject.Find("Player").transform;
    }

    void onEnable()
    {
       StartCoroutine("DestroyLogic");

    }

    void onDisable()
    {
       StopCoroutine("DestroyLogic");
    }


    private IEnumerator DestroyLogic()
    {
       while (true)
       {
         if ( (this.target.position - this.transform.position).y >
             PlayerFalling.FALLING_WARNING_DISTANCE )
         {
          ObjectPoolManager.DestroyPooled( gameObject );
         }

         yield return new WaitForSeconds(1f);
       }
    }
}

While testing this second implementation it felt slower (Nothing is said in the profiler). Do you think there is a better approach?

more ▼

asked May 16 '12 at 05:30 PM

Macarse gravatar image

Macarse
52 2 5 7

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

First off, I'm unfamiliar with the Object Pool Manager use, but for each one, why not just use Destroy(gameObject)? Also, try testing out a single script, possibly a Coins script or something, iterating through the coins each frame, instead of each coin having a script itself that is called every frame.

No matter what, you'll need to check every frame for each coin, it just may be easier to do something like this:

List<GameObject> toDestroy = new List<GameObject>();
foreach (GameObject coin in coins)
{
    if (Vector3.Distance(playerRef.transform.position, coin.transform.position) > maxDistance)
        toDestroy.Add(coin);
}
foreach (GameObject destroyCoin in toDestroy)
{
    coins.Remove(destroyCoin);
    Destroy(destroyCoin.gameObject);
}

The reason for the second list thingy is because you can't be iterating through a list and adjust its members. This is just a rough guess and may not suite you, but it would keep all your necessary code in one spot for coin removal.

more ▼

answered May 16 '12 at 08:11 PM

Befall gravatar image

Befall
121 11 12 14

También, si nunca has usado un List antes, tienes que incluir "using System.Collections.Generic" al principio de su code.

May 16 '12 at 08:13 PM Befall
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x764
x664
x155

asked: May 16 '12 at 05:30 PM

Seen: 257 times

Last Updated: May 16 '12 at 08:13 PM