コード例 #1
0
    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.collider.gameObject.layer == playerLayer && collision.relativeVelocity.magnitude > velocityThreshold)
        {
            PlatformerCharacter2D playerScript = collision.gameObject.GetComponent <PlatformerCharacter2D>();
            playerScript.AdjustHP(-collision.relativeVelocity.magnitude * damageMult);
            Debug.Log("hit");
            for (int i = 0; i < debrisToSpawn; i++)
            {
                GameObject smallDebris = (GameObject)Instantiate(smallerDebris, transform.position, transform.rotation);
                Vector2    newVelocity = new Vector2(Random.Range(-debrisVelocity, debrisVelocity), Random.Range(0, debrisVelocity));
                smallDebris.GetComponent <Rigidbody2D>().velocity = newVelocity;
            }
            Destroy(gameObject);
        }

        else if (collision.collider.gameObject.layer == terrainLayer)
        {
            for (int i = 0; i < debrisToSpawn; i++)
            {
                GameObject smallDebris = (GameObject)Instantiate(smallerDebris, transform.position, transform.rotation);
                Vector2    newVelocity = new Vector2(Random.Range(-debrisVelocity, debrisVelocity), Random.Range(0, debrisVelocity));
                smallDebris.GetComponent <Rigidbody2D>().velocity = newVelocity;
            }
            Destroy(gameObject);
        }
    }
コード例 #2
0
 void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.layer == playerLayer && collision.gameObject.GetComponent <Rigidbody2D>().velocity.y < -fallthreshold && collision.GetType() == typeof(CircleCollider2D))
     {
         PlatformerCharacter2D playerScript = collision.gameObject.GetComponent <PlatformerCharacter2D>();
         playerScript.AdjustHP(-damage);
         Debug.Log("hit");
     }
 }
コード例 #3
0
 void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.layer == playerLayer)
     {
         PlatformerCharacter2D playerScript = collision.gameObject.GetComponent <PlatformerCharacter2D>();
         playerScript.AdjustHP(-damage);
         Debug.Log("hit");
     }
 }