I provided just the part that I thought relevant. There is Nothing attached to lanceprefab. Just a renderer, ( I guess I’ll have to attach a rigidbody2D and a collider to move it and collide with player, but this is not my problem right now).
Here is the whole code, this is my prototype AI for this type of enemy. the //out parts are mostly for testing purposes.
usingUnityEngine;
usingSystem.Collections;
publicclassGuardIA : AbstractBehavior {
publicTransformsightStart, sightEnd;
publicTransformbackStart, backEnd;
publicTransformcloseEnd, closeStart;
publicboolspottedClose;
publicboolspottedFront;
publicboolspottedBack;
publicfloatGuardSpeed;
publicboolaDroite;
publicLayerMaskPlayer;
//Walkscript;
privateAnimatoranimator;
publicfloatshotDelay;
publicTransformlanceprefab;
publicfloatlanceSpeed;
publicfloatshotTimer;
//Usethisforinitialization
voidStart()
{
animator = GetComponent<Animator> ();
//script = GameObject.Find("Player").GetComponent<Walk>(); // sionveutajouterlefaitqueguardentenduniquementsiplayercourt, ajoutercetteconditionenbasdansbehaviours
spottedFront = false;
spottedBack = false;
shotTimer = 0;
animator.SetInteger ("AnimState", 0);
}
//Updateiscalledonceperframe
voidUpdate () {
aDroite = sightStart.position.x > sightEnd.position.x;
Raycasting ();
//Behaviours ();
}
voidRaycasting()
{
Debug.DrawLine(backEnd.position, backStart.position, Color.green);
Debug.DrawLine(sightEnd.position, sightStart.position, Color.green);
spottedFront = Physics2D.Linecast(sightEnd.position, sightStart.position, 1 << LayerMask.NameToLayer("Player"));
//spottedClose = Physics2D.Linecast(closeEnd.position, closeStart.position, 1 << LayerMask.NameToLayer("Player")); Dealwiththatbehaviourfrom "coneSightscript ?"
spottedBack = Physics2D.Linecast(backEnd.position, backStart.position, 1 << LayerMask.NameToLayer("Player"));
if (!spottedFront && !spottedBack) { //attentepardéfaut, immobile
animator.SetInteger ("AnimState", 0);
}
if (spottedClose == true && aDroite) { //s'approcheàdroiteenfrappantavecarmemêlée
animator.SetInteger ("AnimState", 1);
transform.localScale = newVector3 (1f, 1f, 1f);
GetComponent<Rigidbody2D> ().velocity = newVector2 (-GuardSpeed, GetComponent<Rigidbody2D> ().velocity.y);
CloseAttack();
} else {
if (spottedClose == true) { //s'approcheàgaucheenattaquemelée
animator.SetInteger ("AnimState", 1);
transform.localScale = newVector3 (-1f, 1f, 1f);
GetComponent<Rigidbody2D> ().velocity = newVector2 (GuardSpeed, GetComponent<Rigidbody2D> ().velocity.y);
CloseAttack();
}
}
if (spottedBack == true && aDroite ) { //seretourne
animator.SetInteger ("AnimState", 1);
transform.localScale = newVector3 (-1f, 1f, 1f);
//GetComponent<Rigidbody2D> ().velocity = newVector2 (GuardSpeed, GetComponent<Rigidbody2D> ().velocity.y);
} else {
if (spottedBack == true && !aDroite) { //seretourne
animator.SetInteger ("AnimState", 1);
transform.localScale = newVector3 (1f, 1f, 1f);
//GetComponent<Rigidbody2D> ().velocity = newVector2 (-GuardSpeed, GetComponent<Rigidbody2D> ().velocity.y);
}
}
if (spottedFront && !spottedClose) {
//animator.SetInteger("AnimState", 2); TODOanim
RangeAttack();
}
}
publicvoidCloseAttack () {
Debug.Log ("Proooooooche !");
}
publicvoidRangeAttack() {
Debug.Log ("loiiiiiiiiiiiiiiiiiin !");
shotTimer += Time.deltaTime ;
//Vector2direction = body2d.transform.position - transform.position;
//direction.Normalize ();
if ( shotTimer >= shotDelay ){
Debug.Log ("TIIIIIIIIIIIIIIR !");
varclone = Instantiate (lanceprefab, transform.position, Quaternion.identity) asGameObject;
clone.transform.localScale = transform.localScale;
shotTimer=0;
}
}
//publicvoidCreateProjectile(Vector2pos){
//varclone = Instantiate (lanceprefab, pos, Quaternion.identity) asGameObject;
//clone.transform.localScale = transform.localScale;
}
as you can see, raycast is checking bool conditions, and then call my “range attack script” ^^