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);
}
}