void OnCollisionEnter2D(Collision2D collision) { hitObject = collision.collider.gameObject; if (hitObject.GetComponent<GetDamage>()){ getDamage = hitObject.GetComponent<GetDamage>(); getDamage.StartCoroutine(getDamage.SendDamage(missleDamage,blockType)); Destroy (this.gameObject); } }
void OnCollisionEnter2D(Collision2D collision) { hitObject = collision.collider.gameObject; if (hitObject.GetComponent <GetDamage>()) { getDamage = hitObject.GetComponent <GetDamage>(); getDamage.StartCoroutine(getDamage.SendDamage(missleDamage, blockType)); Destroy(this.gameObject); } }
// Update is called once per frame void Update() { jPos = transform.position; if (char1) { p1Pos = character1.transform.position; dist2Player1 = Vector3.Distance(p1Pos, jPos); } if (char2) { p2Pos = character2.transform.position; dist2Player2 = Vector3.Distance(p2Pos, jPos); } if (dist2Player1 <= dist2Player2) { pPos = p1Pos; dist2Player = dist2Player1; } else { pPos = p2Pos; dist2Player = dist2Player2; } aDir = Vector3.Normalize(pPos - jPos); vely = rigidbody2D.velocity.y; standing = DetectFloor(); Animate(standing, aDir); //Debug.DrawLine (transform.position, transform.position + transform.localScale.x * Vector3.right * dangerZone); //////////ATTACKING SCRIPT//////////////// if (dist2Player < dangerZone && allowJump) { ready2Attack = true; // time-based if (standing) { attack = true; //single frame (and really, only within the frame because it gets reset the same frame) } } if (attack) { allowJump = false; attack = false; ready2Attack = true; jumpTimer = time2Jump; if (aDir.x > 0) { rigidbody2D.AddForce(attackVectorR * aForce); } else if (aDir.x < 0) { rigidbody2D.AddForce(attackVectorL * aForce); } } if (ready2Attack || spinAttacking || stompAttacking) { if (!spinAttacking && !stompAttacking && rigidbody2D.velocity.y > 1f) { spot1 = new Vector2(Mathf.Cos(Mathf.Deg2Rad * 45f) * transform.localScale.x, Mathf.Sin(Mathf.Deg2Rad * 45f)) * spinDist + new Vector2(transform.position.x, transform.position.y); spot2 = new Vector2(Mathf.Cos(Mathf.Deg2Rad * 80f) * transform.localScale.x, Mathf.Sin(Mathf.Deg2Rad * 80f)) * spinDist + new Vector2(transform.position.x, transform.position.y); Debug.DrawLine(transform.position, new Vector3(spot1.x, spot1.y, 0)); Debug.DrawLine(transform.position, new Vector3(spot2.x, spot2.y, 0)); aboveObjects1 = Physics2D.LinecastAll(transform.position, spot1); aboveObjects2 = Physics2D.LinecastAll(transform.position, spot2); aboveObjects = aboveObjects1.Concat(aboveObjects2).ToArray(); foreach (RaycastHit2D above in aboveObjects) { if (hurtThings.Contains(above.collider.tag)) { spinAttack = true; spinAttacking = true; spinTimer = time2Spin; break; } } } if (spinAttack) { rigidbody2D.fixedAngle = false; spinAttack = false; } if (spinAttacking) { if (spinTimer > 0) { rigidbody2D.angularVelocity = 2000f; spinTimer -= Time.deltaTime; hitObjects = Physics2D.OverlapCircleAll(transform.position, .115f); foreach (Collider2D hit in hitObjects) { if (hit.GetComponent <GetDamage>()) { getDamage = hit.GetComponent <GetDamage>(); getDamage.StartCoroutine(getDamage.SendDamage(spinDamage, blockType)); spinTimer = 0f; rigidbody2D.velocity = new Vector2(0, 0); rigidbody2D.AddForce(Vector3.Normalize(new Vector3(-(transform.localScale.x * 3), 1, 0)) * 155f); break; } else if (rigidbody2D.velocity.y < -2f) { spinTimer = 0f; } } } if (spinTimer <= 0 && rotateTimer <= 0) { ready2Attack = false; rotateTimer = time2Rotate; } if (rotateTimer > 0) { rotateTimer -= Time.deltaTime; Quaternion.Slerp(transform.rotation, Quaternion.identity, Time.deltaTime * rotoSpeed); //slerp back to upright rotation and fix angle if (rotateTimer <= 0 || (Mathf.Abs(transform.rotation.eulerAngles.z) < 2f && Mathf.Abs(rigidbody2D.angularVelocity) < 2f)) { spinAttacking = false; transform.rotation = Quaternion.identity; rigidbody2D.fixedAngle = true; recoveryTimer = time2Recover; } } } //END SPINNING ATTACK //START STOMPING ATTACK if (!stompAttacking && !spinAttacking && rigidbody2D.velocity.y < -.1f) { spot3 = new Vector2(Mathf.Cos(Mathf.Deg2Rad * 50f) * transform.localScale.x, -Mathf.Sin(Mathf.Deg2Rad * 50f)) * stompDist + new Vector2(transform.position.x, transform.position.y); spot4 = new Vector2(Mathf.Cos(Mathf.Deg2Rad * 65f) * transform.localScale.x, -Mathf.Sin(Mathf.Deg2Rad * 65f)) * stompDist + new Vector2(transform.position.x, transform.position.y); spot5 = new Vector2(Mathf.Cos(Mathf.Deg2Rad * 80f) * transform.localScale.x, -Mathf.Sin(Mathf.Deg2Rad * 80f)) * stompDist + new Vector2(transform.position.x, transform.position.y); Debug.DrawLine(transform.position, new Vector3(spot3.x, spot3.y, 0)); Debug.DrawLine(transform.position, new Vector3(spot4.x, spot4.y, 0)); Debug.DrawLine(transform.position, new Vector3(spot5.x, spot5.y, 0)); belowObjects1 = Physics2D.LinecastAll(transform.position, spot3); belowObjects2 = Physics2D.LinecastAll(transform.position, spot4); belowObjects3 = Physics2D.LinecastAll(transform.position, spot5); belowObjects = belowObjects1.Concat(belowObjects2).ToArray(); belowObjects = belowObjects.Concat(belowObjects3).ToArray(); foreach (RaycastHit2D below in belowObjects) { if (hurtThings.Contains(below.collider.tag)) { stompAttack = true; stompAttacking = true; stompTimer = time2Stomp; break; } } } if (stompAttack) { rigidbody2D.AddForce(aDir * stompForce); if (rigidbody2D.velocity.x > 0 && aDir.x < 0) { rigidbody2D.AddForce(-Vector2.right * 80f); } else if (rigidbody2D.velocity.x < 0 && aDir.x > 0) { rigidbody2D.AddForce(Vector2.right * 80f); } stompAttack = false; } if (stompAttacking) { if (stompTimer > 0) { hitObjects = Physics2D.OverlapAreaAll(botLeft, topRight); foreach (Collider2D hit in hitObjects) { if (hit.GetComponent <GetDamage>()) { getDamage = hit.GetComponent <GetDamage>(); getDamage.StartCoroutine(getDamage.SendDamage(spinDamage, blockType)); stompTimer = 0f; break; } else if (hit.CompareTag("Platform")) { stompTimer = 0f; } } } if (stompTimer > 0) { stompTimer -= Time.deltaTime; } if (stompTimer <= 0) { ready2Attack = false; stompAttacking = false; recoveryTimer = time2Recover; } } } if (recoveryTimer > 0) { recoveryTimer -= Time.deltaTime; } //END STOMP ATTACK //////END ATTACKING SCRIPT////// //Jump Around if (allowJump && standing) { allowJump = false; jumpTimer = time2Jump; if (aDir.x > 0) { rigidbody2D.AddForce(jumpVectorR * jForce); } else if (aDir.x < 0) { rigidbody2D.AddForce(jumpVectorL * jForce); } } if (standing && rigidbody2D.velocity.y < .1f) { jumpTimer -= Time.deltaTime; if (jumpTimer <= 0 && recoveryTimer <= 0) { allowJump = true; } } }
// Update is called once per frame void Update() { jPos = transform.position; if (char1) { p1Pos = character1.transform.position; dist2Player1 = Vector3.Distance (p1Pos,jPos); } if (char2){ p2Pos = character2.transform.position; dist2Player2 = Vector3.Distance (p2Pos,jPos); } if ( dist2Player1 <= dist2Player2 ){ pPos = p1Pos; dist2Player = dist2Player1; } else{ pPos = p2Pos; dist2Player = dist2Player2; } aDir = Vector3.Normalize(pPos - jPos); vely = rigidbody2D.velocity.y; standing = DetectFloor (); Animate (standing,aDir); //Debug.DrawLine (transform.position, transform.position + transform.localScale.x * Vector3.right * dangerZone); //////////ATTACKING SCRIPT//////////////// if (dist2Player < dangerZone && allowJump) { ready2Attack = true; // time-based if (standing){ attack = true; //single frame (and really, only within the frame because it gets reset the same frame) } } if (attack){ allowJump = false; attack = false; ready2Attack = true; jumpTimer = time2Jump; if (aDir.x > 0 ){ rigidbody2D.AddForce (attackVectorR * aForce); } else if (aDir.x < 0 ){ rigidbody2D.AddForce (attackVectorL * aForce); } } if (ready2Attack || spinAttacking || stompAttacking){ if (!spinAttacking && !stompAttacking && rigidbody2D.velocity.y>1f){ spot1 = new Vector2( Mathf.Cos(Mathf.Deg2Rad * 45f) * transform.localScale.x, Mathf.Sin(Mathf.Deg2Rad * 45f) ) * spinDist + new Vector2(transform.position.x,transform.position.y); spot2 = new Vector2( Mathf.Cos(Mathf.Deg2Rad * 80f) * transform.localScale.x, Mathf.Sin(Mathf.Deg2Rad * 80f) ) * spinDist + new Vector2(transform.position.x,transform.position.y); Debug.DrawLine (transform.position, new Vector3 (spot1.x,spot1.y,0)); Debug.DrawLine (transform.position, new Vector3 (spot2.x,spot2.y,0)); aboveObjects1 = Physics2D.LinecastAll(transform.position,spot1); aboveObjects2 = Physics2D.LinecastAll(transform.position,spot2); aboveObjects = aboveObjects1.Concat(aboveObjects2).ToArray(); foreach (RaycastHit2D above in aboveObjects){ if (hurtThings.Contains (above.collider.tag)){ spinAttack = true; spinAttacking = true; spinTimer = time2Spin; break; } } } if (spinAttack){ rigidbody2D.fixedAngle = false; spinAttack = false; } if (spinAttacking){ if (spinTimer>0){ rigidbody2D.angularVelocity = 2000f; spinTimer -= Time.deltaTime; hitObjects = Physics2D.OverlapCircleAll(transform.position,.115f); foreach (Collider2D hit in hitObjects){ if (hit.GetComponent<GetDamage>()){ getDamage = hit.GetComponent<GetDamage>(); getDamage.StartCoroutine(getDamage.SendDamage(spinDamage,blockType)); spinTimer = 0f; rigidbody2D.velocity = new Vector2 (0,0); rigidbody2D.AddForce ( Vector3.Normalize(new Vector3(-(transform.localScale.x * 3),1,0)) * 155f); break; } else if (rigidbody2D.velocity.y<-2f){ spinTimer = 0f; } } } if (spinTimer <= 0 && rotateTimer <= 0 ){ ready2Attack = false; rotateTimer = time2Rotate; } if (rotateTimer>0){ rotateTimer -= Time.deltaTime; Quaternion.Slerp( transform.rotation , Quaternion.identity , Time.deltaTime * rotoSpeed); //slerp back to upright rotation and fix angle if (rotateTimer<=0 || (Mathf.Abs (transform.rotation.eulerAngles.z) < 2f && Mathf.Abs (rigidbody2D.angularVelocity)<2f) ){ spinAttacking = false; transform.rotation = Quaternion.identity; rigidbody2D.fixedAngle = true; recoveryTimer = time2Recover; } } } //END SPINNING ATTACK //START STOMPING ATTACK if (!stompAttacking && !spinAttacking && rigidbody2D.velocity.y<-.1f){ spot3 = new Vector2( Mathf.Cos(Mathf.Deg2Rad * 50f) * transform.localScale.x, -Mathf.Sin(Mathf.Deg2Rad * 50f) ) * stompDist + new Vector2(transform.position.x,transform.position.y); spot4 = new Vector2( Mathf.Cos(Mathf.Deg2Rad * 65f) * transform.localScale.x, -Mathf.Sin(Mathf.Deg2Rad * 65f) ) * stompDist + new Vector2(transform.position.x,transform.position.y); spot5 = new Vector2( Mathf.Cos(Mathf.Deg2Rad * 80f) * transform.localScale.x, -Mathf.Sin(Mathf.Deg2Rad * 80f) ) * stompDist + new Vector2(transform.position.x,transform.position.y); Debug.DrawLine (transform.position, new Vector3 (spot3.x,spot3.y,0)); Debug.DrawLine (transform.position, new Vector3 (spot4.x,spot4.y,0)); Debug.DrawLine (transform.position, new Vector3 (spot5.x,spot5.y,0)); belowObjects1 = Physics2D.LinecastAll(transform.position,spot3); belowObjects2 = Physics2D.LinecastAll(transform.position,spot4); belowObjects3 = Physics2D.LinecastAll(transform.position,spot5); belowObjects = belowObjects1.Concat(belowObjects2).ToArray(); belowObjects = belowObjects.Concat(belowObjects3).ToArray(); foreach (RaycastHit2D below in belowObjects){ if (hurtThings.Contains (below.collider.tag)){ stompAttack = true; stompAttacking = true; stompTimer = time2Stomp; break; } } } if (stompAttack){ rigidbody2D.AddForce (aDir * stompForce); if (rigidbody2D.velocity.x>0 && aDir.x<0){ rigidbody2D.AddForce (-Vector2.right * 80f); } else if (rigidbody2D.velocity.x<0 && aDir.x>0){ rigidbody2D.AddForce (Vector2.right * 80f); } stompAttack = false; } if (stompAttacking){ if (stompTimer>0){ hitObjects = Physics2D.OverlapAreaAll(botLeft,topRight); foreach (Collider2D hit in hitObjects){ if (hit.GetComponent<GetDamage>()){ getDamage = hit.GetComponent<GetDamage>(); getDamage.StartCoroutine(getDamage.SendDamage(spinDamage,blockType)); stompTimer = 0f; break; } else if (hit.CompareTag("Platform")){ stompTimer = 0f; } } } if (stompTimer>0){ stompTimer -= Time.deltaTime; } if (stompTimer<=0){ ready2Attack = false; stompAttacking = false; recoveryTimer = time2Recover; } } } if (recoveryTimer>0){ recoveryTimer -= Time.deltaTime; } //END STOMP ATTACK //////END ATTACKING SCRIPT////// //Jump Around if (allowJump && standing){ allowJump = false; jumpTimer = time2Jump; if (aDir.x > 0){ rigidbody2D.AddForce (jumpVectorR * jForce); } else if (aDir.x < 0){ rigidbody2D.AddForce (jumpVectorL * jForce); } } if (standing && rigidbody2D.velocity.y<.1f){ jumpTimer -= Time.deltaTime; if (jumpTimer<=0 && recoveryTimer<=0){ allowJump = true; } } }