当前位置: 首页>>代码示例>>C#>>正文


C# Collider2D.GetComponent方法代码示例

本文整理汇总了C#中UnityEngine.Collider2D.GetComponent方法的典型用法代码示例。如果您正苦于以下问题:C# Collider2D.GetComponent方法的具体用法?C# Collider2D.GetComponent怎么用?C# Collider2D.GetComponent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在UnityEngine.Collider2D的用法示例。


在下文中一共展示了Collider2D.GetComponent方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: OnTriggerEnter2D

    void OnTriggerEnter2D(Collider2D other)
    {
        // Obstacles and Butter behave the same way, except Obstacles add negative Butter
        if (other.tag == "Pickup")
        {
            Pickup p = other.GetComponent<Pickup>();

            if (p.lane == currentLane)
            {
                butterMeter.AddButter(p);

                // Add to score if the pickup has a positive value
                if (p.butterAmount > 0)
                {
                    ScoreTracker s = FindObjectOfType<ScoreTracker>();
                    s.score += (int)(p.butterAmount * 1000);
                }

                Destroy(other.gameObject);
            }
        }

        // Dog caught up to the player, Game Over!
        else if (other.tag == "Dog")
        {
            Dog d = other.GetComponent<Dog>();

            if( d.currentState != DogState.EATING )
            {
                d.Stop();
                eaten = true;
                stateManager.SetGameState(StateManager.GameplayState.END);
            }
        }
    }
开发者ID:wooffull,项目名称:ToastMessiah,代码行数:35,代码来源:Player.cs

示例2: OnTriggerEnter2D

    void OnTriggerEnter2D(Collider2D other)
    {
        // If player collided with item Sword
        if (other.tag == "Player") {

            stat=other.GetComponent<StatCollectionClass>();

            Up = other.GetComponent<ItemUpgrade>();

            Destroy(this.gameObject);

            // set item sword unlocked
            stat.itemSword = true;

            Up.SwordLevel++;

            Up.SwordDamage+=100f;

            //if no equipment now just equip sword to player
            if(stat.ArmorEquip==false &&stat.BowEquip==false)
            {

                //add player attack damage with SwordDamage
                stat.damage+=Up.SwordDamage;

                //set sword equip to true
                stat.SwordEquip= true;
            }

            }

            // Destroy the item
    }
开发者ID:hew351,项目名称:hew351,代码行数:33,代码来源:ItemSword.cs

示例3: OnTriggerStay2D

 void OnTriggerStay2D(Collider2D coll)
 {
     if (coll.transform.tag == "Object" && coll.GetComponent<MovableObject>() != null)
     {
         bool isOnRightSide = coll.transform.position.x > transform.position.x;
         if (isRightHeading == isOnRightSide && coll.gameObject.GetComponent<MovableObject>().weight <= maxWeight)
         {
             pushingObject = coll.gameObject;
             // Debug.Log("Pushing!!!");
         }
         if (pushingObject!= null)
         {
             MovableObject target = pushingObject.GetComponent<MovableObject>();
             if (target.pusher != null)
                 return;
             target.pusher = gameObject;
             isPushing = true;
         }
     }
     else if (coll.GetComponent<Water>()!=null)
     {
         Collider2D body = GetComponents<Collider2D>().First(c=>!c.isTrigger);
         float tall = body.bounds.size.y;
         float sink = Mathf.Clamp(coll.bounds.max.y - body.bounds.min.y,0,tall);
         float buoyancy = sink/tall*GetComponent<Rigidbody2D>().gravityScale*Physics2D.gravity.y;
         GetComponent<Rigidbody2D>().AddForce(Vector2.down * buoyancy * GetComponent<Rigidbody2D>().mass, ForceMode2D.Force);
         //GetComponent<Rigidbody2D>().velocity = GetComponent<Rigidbody2D>().velocity * 0.99f;
     }
 }
开发者ID:yulcat,项目名称:BeyondTheCity,代码行数:29,代码来源:DogBehavior.cs

示例4: OnTriggerEnter2D

    void OnTriggerEnter2D(Collider2D other)
    {
        if(gameObject.name == "Sasuke" && sasuke.isChargingChidori)
        {
            if(!sasuke.chidoriStrike)
            {
                if(other.GetComponent<Rigidbody2D>() != null)
                {
                    playerRigidBody = other.GetComponent<Rigidbody2D>();
                }

                if(other.gameObject != null)
                {
                    if (other.transform.position.x < transform.position.x)
                    {
                        if(playerRigidBody == null)
                        {
                            return;
                        }
                        else
                        {
                            playerRigidBody.velocity = new Vector2(-12.0f, 12.0f);
                        }

                    }
                    else
                    {
                        playerRigidBody.velocity = new Vector2(12.0f, 12.0f);
                    }
                }
                sasuke.chidoriStrike = true;
            }
        }
    }
开发者ID:d4rkz3r0,项目名称:Shattered-Realms,代码行数:34,代码来源:EnemyAttack.cs

示例5: OnTriggerEnter2D

	void OnTriggerEnter2D(Collider2D coll)
	{
		if (coll.gameObject.tag == "Player") 
		{
			Destroy(this.gameObject);
			playerAttack = coll.GetComponent<PlayerAttackScript>();
			playerAudioSource = coll.GetComponent<AudioSource>();
			playerAudioSource.PlayOneShot(playerAttack.pickUpSound);
			if (thisPower == "Freeze")
			{
				playerAttack.freezePowerReady = true;
			}
			else if (thisPower == "Wind")
			{
				playerAttack.windPowerReady = true;
			}
			else if (thisPower == "Speed")
			{
				playerAttack.speedPowerReady = true;
			}
			else if (thisPower == "Shield")
			{
				playerAttack.shieldPowerReady = true;
			}

		}
	}
开发者ID:fmcfar200,项目名称:Team-15-Repo,代码行数:27,代码来源:PickupScript.cs

示例6: OnTriggerStay2D

    void OnTriggerStay2D(Collider2D other)
    {
        if (other.GetComponent<Collectables>() && Input.GetButtonDown("Action Player "+ playerString))
        {
            other.GetComponent<Collectables>().Collected();
             if (other.tag == "Pills")
            {
                if (GetComponent<PlayerGetPills>())
                    GetComponent<PlayerGetPills>().GotIt();
                audioSource.PlayOneShot (collectableSFX);
            }

            if (other.tag == "Crown")
            {
                if (GetComponent<PlayerGetCrown>())
                    GetComponent<PlayerGetCrown>().GotIt();
                audioSource.PlayOneShot (collectableSFX);
            }

            if (other.tag == "Piece")
            {
                if (GetComponent<PlayerCollectPiece>())
                    GetComponent<PlayerCollectPiece>().GotIt();
                audioSource.PlayOneShot (collectableSFX);
            }
        }
    }
开发者ID:Zyow,项目名称:GameJam_P3D_2015,代码行数:27,代码来源:PlayerAction.cs

示例7: OnTriggerEnter2D

    /// <summary>
    /// Causes damage to any collided object (if appropriate)
    /// </summary>
    /// <param name="collider">Collided object</param>
    //    [Server]
    void OnTriggerEnter2D(Collider2D collider)
    {
        if (!base.isServer)
            return;
        SpaceshipController ship = collider.GetComponent<SpaceshipController> ();
        if (ship != null && (ship.GetId () == this.id || ship.GetId () == 0)) {
            Debug.Log("Avoided collision between " + ship.GetId() + " and " + this.id);
            return;
        }

        if (ship!=null) {
            Debug.Log("Shot Collision with: " + collider + "(" + ship.GetId() + ", " + this.id + ")");
        }

        HealthSystem healthSystem = collider.GetComponent<HealthSystem>();
        if(healthSystem != null && isServer){
            healthSystem.DamageHealth(this.damage);
        }
        // TODO: BETTER EXCEPTION METHOD FOR BLACKHOLES
        if (collider.GetComponent<BlackHoleBehaviour> () != null) {
            return;
        }
        SpaceStationController st = collider.GetComponent<SpaceStationController> ();
        if (st != null && this.id == st.id) {
            return;
        }

        MakeExplosion(this.transform.position, this.transform.rotation);
        Destroy(this.gameObject);
    }
开发者ID:RoryCharlton,项目名称:DECO3801-2015-sem-2,代码行数:35,代码来源:SimpleShot.cs

示例8: OnTriggerEnter2D

	void OnTriggerEnter2D(Collider2D other)
	{
		if(!(gameObject.tag == "eShot" && other.gameObject.tag == "enemy") && !(gameObject.tag == "pShot" && other.gameObject.tag == "player") && gameObject.tag != other.gameObject.tag){
			//If this shot is a pShot and hits an enemy
			if (gameObject.tag == "pShot" && other.gameObject.tag == "enemy") {
				other.GetComponent<eScript> ().health -= bulletDmg;
			}
			
			//If this shot is an eShot and hits a player
			else if (gameObject.tag == "eShot" && other.gameObject.tag == "player") {
				other.GetComponent<pScript> ().health -= bulletDmg;
			}
			
			//If this shot hits a wall
			else if (other.gameObject.tag == "wall")
			{
				Destroy (gameObject);
			}
			
			//If this bullet hits anything, always decrement its health
			bulletHealth--;
			
			//If the bullet has no health, destroy it
			if (bulletHealth <= 0) Destroy (gameObject);
		}
	}
开发者ID:Sir-Batman,项目名称:QuackHacksGame,代码行数:26,代码来源:shotScript.cs

示例9: OnTriggerEnter2D

    private Vector2 _startPosition; // the initial spawn position of this GameObject

    #endregion Fields

    #region Methods

    /*
    * @param other, the other GameObject colliding with this GameObject
    * Function that handles what happens on collision.
    */
    public void OnTriggerEnter2D(Collider2D other)
    {
        // Does nothing if other is not a projectile
        if (other.GetComponent<Projectile>() == null)
            return;

        // If other is an instance of a SimpleProjectile
        var projectile = other.GetComponent<SimpleProjectile>();

        // Checks to see if the owner of the projectile is the player
        if (projectile != null && projectile.Owner.GetComponent<Player>() != null)
        {
            // Handles projectile effects
            if (ProjectileSpawnEffect != null)
                Instantiate(ProjectileSpawnEffect, ProjectileFireLocation.transform.position, ProjectileFireLocation.transform.rotation);

            // Sound
            if (BlockProjectileSound != null)
                AudioSource.PlayClipAtPoint(BlockProjectileSound, transform.position);

            DestroyObject(other); // destroys the projectile

            // Instantiates the projectile, and initilializes the speed, and direction of the projectile
            projectile = (SimpleProjectile)Instantiate(Projectile, ProjectileFireLocation.position, ProjectileFireLocation.rotation);
            projectile.Initialize(gameObject, _direction, _controller.Velocity);
        }
    }
开发者ID:JeremiahParkhurst,项目名称:CapstoneGameCreation,代码行数:37,代码来源:ReflectProjectiles.cs

示例10: OnTriggerEnter2D

    void OnTriggerEnter2D(Collider2D coll)
    {
        if (coll.GetComponent <Actor>()) {
            if(coll.gameObject != activator && !coll.transform.IsChildOf(activator.transform))
            {
                print (coll.gameObject.name + " colliding with" + gameObject.name);
                if (knockback > 0){
                    coll.GetComponent<Actor> ().isKnockedBack = true;
                    coll.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (activator.GetComponent<Fighter> ().facing * knockback, knockback), ForceMode2D.Impulse);
                }
                coll.gameObject.SendMessage("Damage", damage);
                activator.GetComponent<Fighter>().dPause();
                activator.GetComponent<Fighter>().ShakeFunction(coll.gameObject, damage);
                activator.GetComponent<Fighter>().PlaySound(hitSound, activator.GetComponent<Fighter>().SFX);
                if(this.name == "HitBox_Heavy" && coll.GetComponent<Fighter>().stars>0){

                    coll.gameObject.GetComponent<Fighter>().StarLoss();
                    if(activator.GetComponent<Fighter>().stars<activator.GetComponent<Fighter>().starMax){
                        activator.GetComponent<Fighter>().stars++;
                    }

                }
                if(coll.GetComponent <Fighter>())
                {
                    coll.gameObject.SendMessage("ArmorDamage", armorbreak);
                }
            }
        }
    }
开发者ID:Studio12,项目名称:AstralClashNew,代码行数:29,代码来源:DamHitbox.cs

示例11: OnTriggerEnter2D

	void OnTriggerEnter2D(Collider2D other) 
	{
		if(other.GetComponent<ArenaOpponent>()!=null)
		{
			other.GetComponent<ArenaOpponent>().mAmmoRemaining = 3+PlayerPrefs.GetInt("ArenaRound");
		}
	}
开发者ID:AdamMatheny,项目名称:GlobalGameJam2016,代码行数:7,代码来源:TomatoStand.cs

示例12: OnTriggerStay2D

    void OnTriggerStay2D(Collider2D other)
    {
        if (other.gameObject.layer == LayerMask.NameToLayer("Pickup"))
        {
            Rigidbody2D partBody = other.GetComponent<Rigidbody2D>();
            Transform partCenter = other.transform.GetChild(1);

            float magnitude = 0;
            if(this.transform.position.x >= partCenter.position.x) {
                magnitude = -25f;
            }
            else {
                magnitude = 25f;
            }

            partBody.AddForce(new Vector2(magnitude, 0), ForceMode2D.Force);
        }

        if (other.gameObject.layer == LayerMask.NameToLayer("Player"))
        {
            Rigidbody2D playerBody = other.GetComponent<Rigidbody2D>();
            Transform partCenter = other.transform.GetChild(1);

            float magnitude = 0;
            if(this.transform.position.x >= partCenter.position.x) {
                magnitude = -100f;
            }
            else {
                magnitude = 100f;
            }
            playerBody.AddForce(new Vector2(magnitude, 0), ForceMode2D.Force);
        }
    }
开发者ID:atymisk,项目名称:Boulder,代码行数:33,代码来源:BodyDetector.cs

示例13: OnTriggerEnter2D

 private void OnTriggerEnter2D(Collider2D other)
 {
     if (other.CompareTag("Player")) {
         other.GetComponent<Health>().TakeDamage(damage);
         Debug.Log(other.GetComponent<Character>().id);
     }
 }
开发者ID:stephdim,项目名称:UniJam,代码行数:7,代码来源:Attack.cs

示例14: OnTriggerStay2D

 void OnTriggerStay2D( Collider2D activator )
 {
     if ( ( activator.GetComponent<Killable>() != null ) && (GameObject.FindGameObjectWithTag ("Player")) )
     {
         activator.GetComponent<Killable>().Hurt( damage );
     }
 }
开发者ID:RachelClayton,项目名称:ZombieShooter,代码行数:7,代码来源:DeathTrigger.cs

示例15: OnTriggerEnter2D

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.GetComponent<PlayerManager>())
        {
            if (!player)
            {
                player = other.GetComponent<PlayerManager>();
            }

            if(player.GetNumOfCarbons() >= carbonsNeeded)
            {

                if (nextLayer == 3)
                {
                    player.BecomeDiamond();
                }
                else
                {
                    player.GetComponent<HoldInPlace>().enabled = true;
                    player.GetComponent<FlyTo>().setTarget(transform.position);
                    player.GetComponent<FlyTo>().enabled = true;
                    GameManager.art.SwitchLayer(nextLayer);
                    GameManager.music.moveDownLayer();
                }
            }
        }
    }
开发者ID:kenfehling,项目名称:World-of-Carbon,代码行数:27,代码来源:PressureZone.cs


注:本文中的UnityEngine.Collider2D.GetComponent方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。