Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

c# - Where to attach collision detection script?

I watched Basic Platformer Game tutorial for Unity where presenter created coin pick-up script that he attached to Coin prefab. I want to know if the pick-up script should be attached to the Player or Coin GameObject.

Let's say we have a game with pick-upable objects. They do nothing more than incrementing the score (or affecting the player in another way) and destroy themselves on collision.

I was wondering that what is the preferred approach to this problem.

I've come up with two approaches:


Approach A

Have one ObjectPickup script on the player game object. This script would do whatever is required depending on the type of collided object.

private void OnTriggerEnter2D(Collider2D other)
{
    if (other.gameObject.CompareTag("Coin"))
    {
        IncrementScore();
        Destroy(other.gameObject);
    }
    else if (other.gameObject.CompareTag("SuperSpeed"))
    {
        IncreasePlayerSpeed();
        Destroy(other.gameObject);
    }
}

Approach B

Have CoinPickup script on every coin and SuperSpeedPickup script on every super speed powerup.

private void OnTriggerEnter2D(Collider2D other)
{
    if (other.gameObject.CompareTag("Player"))
    {
        other.gameObject.IncrementScore();
        Destroy(gameObject);
    }
}

And other script:

private void OnTriggerEnter2D(Collider2D other)
{
    if (other.gameObject.CompareTag("Player"))
    {
        other.gameObject.IncreasePlayerSpeed();
        Destroy(gameObject);
    }
}

Is Approach A more efficient then Approach B? If so what is the rough number of GameObjects when Approach A becomes preferable?

Or is it always better to work with Approach B as it seems to be cleaner (when we have a number of different pick-upable objects)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your example, "approach a" would be more efficient than the other because the OnTriggerEnter2D is called once. With "approach b", OnTriggerEnter2D is called twice on two different scripts. It's slower for Unity to make callback calls let alone on two different scripts + the player. Also, OnTriggerEnter2D is sent to disabled MonoBehaviours which makes "approach a" more efficient choice. The main reason for the slow event call is because the call is made from native C++ side of the code to the Unity's C# API side and this is costly.

Note that it really doesn't matter that much. This is just an attempt to answer your question for which one is more efficient but what you're doing is a micro-optimization.


The thing that should play big role in this decision is the behavior of the coin after the trigger.

If the coins behave the-same then use "approach a" and do the detection on the player's side.

If the coins have different behavior like coins with different score values, color and action(animation) when they are collected then do the detection on the coins side and handle each coin. This is the reason why scripts are attached to GameObjects in the first place. You don't need different tags for each different coin. Use enum to distinguish their action in the OnTriggerEnter2D function. It would be awful to use "approach a" to handle this in this case.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...