Currently having a problem with my instantiated prefabs.
0:09, the already released orb no longer collides due to a newly instantiated orb.
(I do not own the background, it is a placeholder)
If I hold the key S, i will instantiate a clone prefab that is a child to my player and all my clones will be able to ignore collision with all layers. On releasing the Key S, all my clones will no longer ignore collision, and that specific clone will no longer be a child. However, I only want the newly instantiated clone, while holding my key to ignore collision. Any other clone of that prefab that is not a child will not be affected.
I attach this script to my player.
in my update I call DarkOrbChannel();
Line 52 of DarkOrbchannel, Get Key down, I call Spawn ();
Line 63 of DarkOrbChannel(); I call Get Key up which calls Launch();,
Line 84 of Spawn(); makes my ignore true
Line 142 of Launch(); makes my ignore false
I use ignore on my second Script on lines 28, and 38.
using UnityEngine;
using System.Collections;
public class DarkOrbPlayer : MonoBehaviour
{
Animator anim;
public Rigidbody2D darkOrb;
public Transform channelStart;
public bool instantiated = true; //to check if orb is created
public float cooldown = 0f; //individual cooldown on keydown
public float attackspeed = 3f; //attack speed is part of cooldown
public float checker = 0; //used to turn float to int, for Mathf
public float xsize = 15f; //x scale of orb
public float ysize = 15f; //y scale of orb
public float transition = 1f; //color of player, transition = 1 = white
public bool transitioning = false; //if alpha of orb > 1
public float initialcost = 10f;
public AudioClip eerieend;
public AudioClip eeriesound;
public bool release = false;
public bool ignore;
public Rigidbody2D orb;
void Start()
{
anim = gameObject.GetComponent<Animator> ();
}
void Update()
{
DarkOrbChannel ();
foreach (SpriteRenderer sr in GetComponentsInChildren<SpriteRenderer>())
sr.color = new Color(transition, transition, transition);
if(!transitioning && transition != 1)
{
transition += 0.0015f;
if (transition > 1)
{
transition = 1;
}
}
}//End Update
void DarkOrbChannel()
{
if(Input.GetKeyDown(KeyCode.S))
{
WizardHealth wh = gameObject.GetComponent<WizardHealth>();
Playerscript ps = gameObject.GetComponent<Playerscript> ();
if( ps.isIdl && Time.time > cooldown && wh.mana > 10)
{
Spawn();
}
}
if(Input.GetKey(KeyCode.S) && orb != null && instantiated)
{
Playerscript ps = gameObject.GetComponent<Playerscript> ();
Size ();
Alpha();
ps.moving = 2;
}
if(Input.GetKeyUp(KeyCode.S))
{
Launch();
}
}//End DarkOrbChannel
void Spawn()
{
WizardHealth wh = gameObject.GetComponent<WizardHealth>();
Playerscript ps = gameObject.GetComponent<Playerscript> ();
release = false;
anim.SetBool ("DarkOrbChannel", true);
ps.isChannel = true;
wh.mana -= 10;
orb = Instantiate(darkOrb , channelStart.position, Quaternion.identity) as Rigidbody2D;
orb.transform.parent = transform;
cooldown = Time.time + attackspeed;
//audio.PlayOneShot (eerieend);
audio.clip = eeriesound;
audio.Play ();
instantiated = true;
ignore = true;
}//End Spawn
void Size()
{
orb.transform.localScale = new Vector3(xsize, ysize, 0);
xsize -= 0.01f;
ysize -= 0.01f;
if (ysize <= 2f || xsize <= 2f)
{
xsize = 2f;
ysize = 2f;
}
}//End Size
void Alpha()
{
WizardHealth wh = gameObject.GetComponent<WizardHealth>();
DarkOrb dorb = transform.Find("Darkorb(Clone)").GetComponent<DarkOrb> ();
dorb.alpha += 0.00025f;
if(dorb.alpha > 1)
{
transitioning = true;
transition -= 0.002f;
foreach (SpriteRenderer sr in GetComponentsInChildren<SpriteRenderer>())
sr.color = new Color(transition, transition, transition);
if(transition <= 0.5f)
{
transition = 1f;
}
}
if(dorb.alpha <= 1)
{
checker += 2 * Time.deltaTime;
wh.mana -= Mathf.FloorToInt(checker);
if(checker >=1)
{
checker =0;
}
}
if(dorb.alpha >1.8f || wh.mana == 0)
{
Launch();
}
}//End Alpha
void Launch()
{
release = true;
Playerscript ps = gameObject.GetComponent<Playerscript> ();
ps.moving = 4;
ps.isChannel = false;
anim.SetBool ("DarkOrbChannel", false);
transitioning = false;
xsize = 15f;
ysize = 15f;
orb.transform.parent = null;
ignore = false;
if(instantiated && orb != null)
{
audio.Stop();
audio.PlayOneShot (eerieend);
instantiated = false;
if(ps.facingRight)
{
orb.rigidbody2D.AddForce(Vector3.right * 400);
}
else
{
orb.rigidbody2D.AddForce(-Vector3.right * 400);
}
}
}//End Launch
public int darkBallExp = 0;
public int darkBallMaxExp = 500;
public int darkBallLevel =1;
}
this one I attach to my public Rigidbody2D darkOrb prefab on my first script
Line 28, if ignore is true, I ignore these layers
Line 38 else, I do not ignore these layers.
using UnityEngine;
using System.Collections;
public class DarkOrb : MonoBehaviour
{
public float lifeTime =3f;
public float alpha = 0.3f;
private float alphadamage = 0f;
public static float damage = 2f;
public bool isEnemyDarkBall = false;
void Start()
{
alpha = 0.3f;
Physics2D.IgnoreLayerCollision (11, 12);
Physics2D.IgnoreLayerCollision (13, 12);
Physics2D.IgnoreLayerCollision (9, 12);
Physics2D.IgnoreLayerCollision (10, 12);
Physics2D.IgnoreLayerCollision (14, 12);
Physics2D.IgnoreLayerCollision (15, 12);
}
void Update ()
{
damage = Mathf.FloorToInt(alphadamage);
DarkOrbPlayer dop = GameObject.Find ("Wizard").GetComponent<DarkOrbPlayer> ();
if(dop.ignore) //if child
{
Debug.Log ("iamchild");
Physics2D.IgnoreLayerCollision (11, 12);
Physics2D.IgnoreLayerCollision (13, 12);
Physics2D.IgnoreLayerCollision (9, 12);
Physics2D.IgnoreLayerCollision (10, 12);
Physics2D.IgnoreLayerCollision (14, 12);
Physics2D.IgnoreLayerCollision (15, 12);
}
else //if not a child
{
alphadamage = 12 * alpha;
if (alphadamage > 12)
{
alphadamage = 12;
}
Physics2D.IgnoreLayerCollision (13, 12,false);
Physics2D.IgnoreLayerCollision (9, 12,false);
Physics2D.IgnoreLayerCollision (10, 12,false);
Physics2D.IgnoreLayerCollision (14, 12,false );
Physics2D.IgnoreLayerCollision (15, 12,false );
}
SpriteRenderer spriterenderer = GetComponent<SpriteRenderer> ();
spriterenderer.color = new Color (1f, 1f, 1f, alpha);
transform.Rotate (0 , 0, 720 * Time.deltaTime);
Playerscript ps = GameObject.Find("Wizard").GetComponent<Playerscript>();
if(dop.release)
{
Destroy(gameObject , lifeTime);
}
}
void OnTriggerEnter2D(Collider2D OtherCollider)
{
HealthScript healthscript = OtherCollider.gameObject.GetComponent<HealthScript> ();
if (healthscript != null)
if (isEnemyDarkBall = healthscript.isEnemy ())
{
if (isEnemyDarkBall == true)
{
healthscript.redtime = Time.time + 1 * healthscript.redduration;
healthscript.red = 8;
healthscript.Damage (Playerscript.DO);
}
}
}
}
Update: The damage of my prefab depends on how long I’ve held down my Key. I decided to remove the ignore collision, and the damage scales off how long I held on my newly instantiated orb, rather than the previous.