0
\$\begingroup\$

So, in my game, the Powerup (capsule) has a RigidBody and a Box Collider. I’ve set the Box Collider to «is trigger». When the player hits the powerup, it disappears - just as it’s supposed to.

Is it possible to let the enemies collide with the powerup (bump into it) instead of just passing through it? From the Enemy script:

    void Start()
{

        enemyRb = GetComponent<Rigidbody>();
        gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
        player = GameObject.Find("Player");

}

// Update is called once per frame
void Update()
{

    if (player != null && gameManager.isGameActive)
    {
        enemyRb.AddForce((player.transform.position - transform.position).normalized * speed);
    }

    if (transform.position.y < -4)
    {
        Destroy(gameObject);
        gameManager.UpdateScore(pointValue);
    }
}

From the PlayerController script:

private void OnTriggerEnter(Collider other)
{
        if (other.CompareTag("Powerup"))
        {
            hasPowerup = true;
            Destroy(other.gameObject);
        }
}
\$\endgroup\$
3
  • \$\begingroup\$ To be honest i didn't got your question exactly, sorry for that, But what i think is that you want to disable the power up so the player will not collide or trigger with it while enemies still collide or trigger with it? if i am right then you should use layers. If i am right then i will write the complete answer if needed. \$\endgroup\$
    – Swati
    Nov 20, 2019 at 10:34
  • \$\begingroup\$ Sorry, my English is almost as bad as my skills in c#. When the player collides with the powerup, everything works just fine. The player gets a boost for 5 seconds, and the powerup disappears. The problem is that I'd like the enemies to collide with the powerups without triggering them. So when an enemy hits the powerup it acts just like a solid object without any triggers. I'll check out the layers functionality. \$\endgroup\$
    – Senseless
    Nov 20, 2019 at 10:42
  • \$\begingroup\$ We had another Q&A thread about a similar topic recently, which may give you some ideas \$\endgroup\$
    – DMGregory
    Nov 20, 2019 at 10:43

1 Answer 1

1
\$\begingroup\$

Firstly, i would recommend to move OnTriggerEnter from PlayerController to PowerupController:

private void OnTriggerEnter(Collider other)
{
        if (other.CompareTag("Player"))
        {
            other.GetComponent<PlayerController>().HasPowerup = true;
            Destroy(gameObject);
        }
}

It is not character's job to detect and destroy powerups; it would be better not to clutter characters scripts with additional logic when possible.

Now to collisions.

The best way is to use layers: https://docs.unity3d.com/Manual/LayerBasedCollision.html

Simply attach another collider as a child gameobject to powerup and set it physics layer to intersect with enemies and not intersect with the player.

... Or you could use OnCollisionEnter(Collision other) instead of triggers, but using triggers almost always is better idea.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .