/// <summary> /// Instantiate a new instance of the WeaponProjectile class using the supplied parameters /// </summary> /// <param name="instance">The instance to use as the base</param> /// <param name="owner">The character that owns this projectile</param> /// <param name="launchPoint">Where to spawn the projectile</param> /// <param name="directionX">The direction to move</param> /// <returns>The new projectile</returns> public static WeaponProjectile Create(WeaponProjectile instance, Character owner, Transform launchPoint, int directionX) { WeaponProjectile projectile = GameObject.Instantiate <WeaponProjectile>(instance); projectile.Owner = owner; // Set the start position Vector2 position = launchPoint.position; projectile.transform.position = position; projectile.DirectionX = directionX; // Make sure we can't collide with the person who shot this projectile WeaponProjectile.IgnoreOwnerCollisions(projectile, owner); // Flip the sprite if necessary if (projectile.ShouldFlipDirection && directionX < 0) { Vector3 rotation = projectile.transform.localRotation.eulerAngles; rotation.y -= 180; projectile.transform.localEulerAngles = rotation; } return(projectile); }
private void LaunchProjectile(WeaponProjectile projectile) { // Create the projectile if (projectile != null) { WeaponProjectile.Create( projectile, this, this.LaunchPoint, (this.CurrentDirection == Direction.Left ? -1 : 1)); } }
protected override void Start() { base.Start(); // Get the sprite from the hand of the character Sprite weaponSprite = this.GetComponentInChildren <SpriteRenderer>().sprite; BoxCollider2D weaponBox = this.GetComponentInChildren <BoxCollider2D>(); if (this.Owner != null) { SpriteRenderer[] parts = this.Owner.GetComponentsInChildren <SpriteRenderer>(); for (int i = 0; i < parts.Length; i++) { if (parts[i].name == (this.IsMainItem ? "MainItem" : "OffItem")) { weaponSprite = parts[i].sprite; weaponBox = parts[i].gameObject.GetComponent <BoxCollider2D>(); break; } } } // Update our sprite to match the one we are throwing if (weaponSprite != null) { SpriteRenderer sr = this.GetComponentInChildren <SpriteRenderer>(); if (sr != null) { sr.sprite = weaponSprite; } } // Update our collision box based on the one we are throwing if (weaponBox != null) { BoxCollider2D bc = this.GetComponentInChildren <BoxCollider2D>(); if (bc != null) { bc.offset = weaponBox.offset; bc.size = weaponBox.size; } // Since we updated the collider we need to re-ignore the collisions WeaponProjectile.IgnoreOwnerCollisions(this, this.Owner); this.RenderTransform.localPosition = -weaponBox.offset; this.RenderTransform.RotateAround(this.transform.position, Vector3.forward, this.StartRotation * this.DirectionX); } }
protected static void IgnoreOwnerCollisions(WeaponProjectile projectile, Character owner) { // Prevent hitting the player who cast it if (owner != null) { Collider2D[] colliders = owner.GetComponentsInChildren <Collider2D>(); Collider2D[] projectileColliders = projectile.GetComponentsInChildren <Collider2D>(); for (int i = 0; i < colliders.Length; i++) { for (int j = 0; j < projectileColliders.Length; j++) { Physics2D.IgnoreCollision(colliders[i], projectileColliders[j]); } } } }