SlideShare a Scribd company logo
1 of 2
/*This Script is a crack at a "Homing Attack"where this
* object looks for the closest enemyanddash towards it for an attack
* This script is not perfect but is gets the job done.
*/
public class HomingAttack : MonoBehaviour{
public static HomingAttack homingAttack;
public TransformthisPlayer;
public bool closestExist;
public bool striking;
privateCircleCollider2D castTrigger;
public List<Transform> enemyList;
public List<Collider2D> enemyColliders;
public TransformclosestEnemy;
RaycastHit2D hit2D;
float enemyDistance;
// Use this for initialization
voidStart () {
homingAttack = this;
//find my collider component to detect nearby enemys withing 7 meter radius
castTrigger = gameObject.GetComponent<CircleCollider2D> ();
//Find closest target every .25secs
InvokeRepeating ("FindClosestEnemy", 1, .25f);
}
voidFindClosestEnemy()
{
//print ("Hey");
//find closest active objectin list
if(enemyList.Count > 0 && closestEnemy== null)
{
//default radius for castTrigger
enemyDistance = 7;
//iterate throughenemyList
for(int i = 0; i < enemyList.Count; i++)
{
//checked if enemies are active andtouchingthe castTrigger
if(enemyList[i].gameObject.activeInHierarchy &&enemyColliders[i].IsTouching(castTrigger))
{
//temporary distance to check whichenemy in list is closer
float tempDist = Vector3.Distance(transform.position, enemyList [i].position);
//if closer enemy exists
if (tempDist < enemyDistance)
{
enemyDistance = tempDist;
closestEnemy= enemyList [i];
//print ("Hey");
}
}
}
}
}
public voidHomingStrike(bool isOn)
{
//only cast line when there is a closest enemy
if(closestEnemy != null)
hit2D = Physics2D.Linecast (thisPlayer.position, closestEnemy.position,1 << LayerMask.NameToLayer("Enemy"));
//enemy close enoughperform homingattack
if(hit2D.distance< 5)
{
striking= isOn;
}
//enemy far enough throw knife
else if(hit2D.distance > 5 && hit2D.distance < 7 )
{
Debug.Log("Tofar throwknife");
}
//play hominganimation
thisPlayer.GetComponent<Animator> ().SetTrigger ("Homming");
}
voidFixedUpdate()
{
//only call in closest Enemy exists and strikingis true
if (closestEnemy!= null && striking) {
//smoothingmove from point A to point B in x sec. x =.35f
Vector2 endPosition= Vector2.Lerp(thisPlayer.position, closestEnemy.position,.55f);
//take the lerped pos and havethe player move towards it
thisPlayer.position = Vector2.MoveTowards(thisPlayer.position,endPosition,.25f);
//cast ray durring attack for debugpurposes but i just think its fancy
RayCasting(thisPlayer.position,endPosition);
}
}
voidRayCasting(Vector2 start, Vector2 end)
{
// draw a line from start point to endpoint
Debug.DrawLine(start,end, Color.green);
}
}

More Related Content

Viewers also liked

RDC 211 classificação cosméticos
RDC 211 classificação cosméticosRDC 211 classificação cosméticos
RDC 211 classificação cosméticosRenata Jorge Tiossi
 
Sistema de controle acadêmico
Sistema de controle acadêmicoSistema de controle acadêmico
Sistema de controle acadêmicoDiogo Sousa
 
NÚMERO DE ALUMNOS QUE RECIBEN FORMACIÓN RELIGIOSA Y MORAL CATÓLICA EN LA ESCU...
NÚMERO DE ALUMNOS QUE RECIBEN FORMACIÓN RELIGIOSA Y MORAL CATÓLICA EN LA ESCU...NÚMERO DE ALUMNOS QUE RECIBEN FORMACIÓN RELIGIOSA Y MORAL CATÓLICA EN LA ESCU...
NÚMERO DE ALUMNOS QUE RECIBEN FORMACIÓN RELIGIOSA Y MORAL CATÓLICA EN LA ESCU...MaestroPedro .
 
Hatonuevo – Fonseca
Hatonuevo – FonsecaHatonuevo – Fonseca
Hatonuevo – FonsecaYamid Murillo
 

Viewers also liked (8)

Violín 18
Violín 18Violín 18
Violín 18
 
RDC 211 classificação cosméticos
RDC 211 classificação cosméticosRDC 211 classificação cosméticos
RDC 211 classificação cosméticos
 
Sistema de controle acadêmico
Sistema de controle acadêmicoSistema de controle acadêmico
Sistema de controle acadêmico
 
Imagenes
ImagenesImagenes
Imagenes
 
NÚMERO DE ALUMNOS QUE RECIBEN FORMACIÓN RELIGIOSA Y MORAL CATÓLICA EN LA ESCU...
NÚMERO DE ALUMNOS QUE RECIBEN FORMACIÓN RELIGIOSA Y MORAL CATÓLICA EN LA ESCU...NÚMERO DE ALUMNOS QUE RECIBEN FORMACIÓN RELIGIOSA Y MORAL CATÓLICA EN LA ESCU...
NÚMERO DE ALUMNOS QUE RECIBEN FORMACIÓN RELIGIOSA Y MORAL CATÓLICA EN LA ESCU...
 
Münich
MünichMünich
Münich
 
Atlas3ed
Atlas3edAtlas3ed
Atlas3ed
 
Hatonuevo – Fonseca
Hatonuevo – FonsecaHatonuevo – Fonseca
Hatonuevo – Fonseca
 

Recently uploaded

Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 

Recently uploaded (20)

Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 

A_Basic_Homing_Attack_Class

  • 1. /*This Script is a crack at a "Homing Attack"where this * object looks for the closest enemyanddash towards it for an attack * This script is not perfect but is gets the job done. */ public class HomingAttack : MonoBehaviour{ public static HomingAttack homingAttack; public TransformthisPlayer; public bool closestExist; public bool striking; privateCircleCollider2D castTrigger; public List<Transform> enemyList; public List<Collider2D> enemyColliders; public TransformclosestEnemy; RaycastHit2D hit2D; float enemyDistance; // Use this for initialization voidStart () { homingAttack = this; //find my collider component to detect nearby enemys withing 7 meter radius castTrigger = gameObject.GetComponent<CircleCollider2D> (); //Find closest target every .25secs InvokeRepeating ("FindClosestEnemy", 1, .25f); } voidFindClosestEnemy() { //print ("Hey"); //find closest active objectin list if(enemyList.Count > 0 && closestEnemy== null) { //default radius for castTrigger enemyDistance = 7; //iterate throughenemyList for(int i = 0; i < enemyList.Count; i++) { //checked if enemies are active andtouchingthe castTrigger if(enemyList[i].gameObject.activeInHierarchy &&enemyColliders[i].IsTouching(castTrigger)) { //temporary distance to check whichenemy in list is closer float tempDist = Vector3.Distance(transform.position, enemyList [i].position); //if closer enemy exists if (tempDist < enemyDistance) { enemyDistance = tempDist; closestEnemy= enemyList [i]; //print ("Hey"); } } } } }
  • 2. public voidHomingStrike(bool isOn) { //only cast line when there is a closest enemy if(closestEnemy != null) hit2D = Physics2D.Linecast (thisPlayer.position, closestEnemy.position,1 << LayerMask.NameToLayer("Enemy")); //enemy close enoughperform homingattack if(hit2D.distance< 5) { striking= isOn; } //enemy far enough throw knife else if(hit2D.distance > 5 && hit2D.distance < 7 ) { Debug.Log("Tofar throwknife"); } //play hominganimation thisPlayer.GetComponent<Animator> ().SetTrigger ("Homming"); } voidFixedUpdate() { //only call in closest Enemy exists and strikingis true if (closestEnemy!= null && striking) { //smoothingmove from point A to point B in x sec. x =.35f Vector2 endPosition= Vector2.Lerp(thisPlayer.position, closestEnemy.position,.55f); //take the lerped pos and havethe player move towards it thisPlayer.position = Vector2.MoveTowards(thisPlayer.position,endPosition,.25f); //cast ray durring attack for debugpurposes but i just think its fancy RayCasting(thisPlayer.position,endPosition); } } voidRayCasting(Vector2 start, Vector2 end) { // draw a line from start point to endpoint Debug.DrawLine(start,end, Color.green); } }