本文整理汇总了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);
}
}
}
示例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
}
示例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;
}
}
示例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;
}
}
}
示例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;
}
}
}
示例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);
}
}
}
示例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);
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
}
}
示例11: OnTriggerEnter2D
void OnTriggerEnter2D(Collider2D other)
{
if(other.GetComponent<ArenaOpponent>()!=null)
{
other.GetComponent<ArenaOpponent>().mAmmoRemaining = 3+PlayerPrefs.GetInt("ArenaRound");
}
}
示例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);
}
}
示例13: OnTriggerEnter2D
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player")) {
other.GetComponent<Health>().TakeDamage(damage);
Debug.Log(other.GetComponent<Character>().id);
}
}
示例14: OnTriggerStay2D
void OnTriggerStay2D( Collider2D activator )
{
if ( ( activator.GetComponent<Killable>() != null ) && (GameObject.FindGameObjectWithTag ("Player")) )
{
activator.GetComponent<Killable>().Hurt( damage );
}
}
示例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();
}
}
}
}