AI-Enemy[move-jump attack Player-up and down stairs]Unity 2D
AI_Enemy in Unity 2D:
- Move
- Jump attack Player
- Up and down stairs
C#:
1.enemy_controller:
[SerializeField] Transform left;
[SerializeField] Transform right;
[SerializeField] Transform enemy_Tranform;
[SerializeField] float speed;
[SerializeField] Animator anim;
[SerializeField] float idleDuration;
float idleTime;
Vector3 initScale;
bool movingLeft;
void Awake()
{
this.initScale = this.enemy_Tranform.localScale;
}
private void OnDisable() {
anim.SetBool("run",false);
}
void Update()
{
if(this.movingLeft)
{
if(this.enemy_Tranform.position.x >= this.left.position.x)
MoveInDirection(-1);
else
DirectionChange();
}
else
{
if(this.enemy_Tranform.position.x <= this.right.position.x)
MoveInDirection(1);
else
DirectionChange();
}
}
private void DirectionChange()
{
anim.SetBool("run",false);
idleTime += Time.deltaTime;
if(idleTime > idleDuration)
movingLeft = !movingLeft;
}
void MoveInDirection(int _direction)
{
idleTime = 0;
anim.SetBool("run",true);
//face
this.enemy_Tranform.localScale = new Vector3(this.initScale.x * _direction,
this.initScale.y,this.initScale.z);
//move
this.enemy_Tranform.position = new Vector3(this.enemy_Tranform.position.x +
Time.deltaTime *_direction * this.speed,this.enemy_Tranform.position.y,
this.enemy_Tranform.position.z);
}
2.layderdemo:
[SerializeField] enemy_demo enemy;
private void OnTriggerEnter2D(Collider2D other)
{
if(other.GetComponent<ladders>())
{
enemy.climbinAllowed = true;
}
}
private void OnTriggerExit2D(Collider2D other) {
if(other.GetComponent<ladders>())
{
enemy.climbinAllowed = false;
}
}
3.enemy_demo:
[SerializeField]public bool climbinAllowed; public bool oninsland2; Rigidbody2D rb; Animator anim; [SerializeField]float dirY; [SerializeField] Transform point; [SerializeField] Transform point2; [SerializeField] GameObject[] ladderd;
// attack [SerializeField] BoxCollider2D boxCollider; [SerializeField] LayerMask playermask; [SerializeField] float attackCooldown; [SerializeField] float range; [SerializeField] float cooldownTimer = Mathf.Infinity;// attack now // rotaion float value = 3f; //attack enemy_controller enemy_Contro; [SerializeField] float jumpHeight; [SerializeField]public Transform player; bool facingRight=false; void Awake() { this.rb = GetComponent<Rigidbody2D>(); this.anim = GetComponent<Animator>(); this.enemy_Contro = GetComponentInParent<enemy_controller>(); } void Start() { this.ladderd[0].SetActive(true); this.ladderd[1].SetActive(false); } // Update is called once per frame void Update() { //attack this.cooldownTimer += Time.deltaTime; //Attack only when player in sight if(PlayerInSight()) { FlipEnemy(); if(this.cooldownTimer >= this.attackCooldown) { this.cooldownTimer = 0; float distanceFromPlayer = this.player.position.x - this.transform.position.x; this.rb.AddForce(new Vector2(distanceFromPlayer,jumpHeight),ForceMode2D.Impulse); } } //move up the stairs if( Vector2.Distance(this.transform.position,this.point.position)<0.2f) { this.ladderd[0].SetActive(true); this.ladderd[1].SetActive(false); this.oninsland2=false; this.rb.AddForce(new Vector2(0,jumpHeight),ForceMode2D.Impulse); } //move down the stairs else if( Vector2.Distance(this.transform.position,this.point2.position)<0.2f) { this.ladderd[1].SetActive(true); this.ladderd[0].SetActive(false); this.oninsland2=true; } else { if(this.climbinAllowed==true) { this.enemy_Contro.enabled=false; } else { this.enemy_Contro.enabled=true; } // stop moving, attack the player if(this.enemy_Contro != null ) { this.enemy_Contro.enabled = !PlayerInSight(); } } } //when colliding with a player private bool PlayerInSight() { RaycastHit2D hit = Physics2D.BoxCast(this.boxCollider.bounds.center + this.transform.right * this.range * this.transform.localScale.x,this.boxCollider.bounds.size,0,Vector2.left,0,this.playermask); return hit.collider != null; } //ladders private void FixedUpdate() { if(this.climbinAllowed) { this.anim.SetBool("run",true); this.rb.isKinematic = true; if(this.oninsland2==true) { this.rb.velocity = new Vector2(0,-this.dirY); } else { this.rb.velocity = new Vector2(0,this.dirY); }
} else { this.rb.isKinematic = false; this.rb.velocity = new Vector2(0,this.rb.velocity.y); //get up after falling if(this.transform.rotation.z >0 ||this.transform.rotation.z <0) { if(this.value >=0) this.value -= Time.deltaTime; else { this.transform.rotation = Quaternion.Euler(0, 0, 0); this.value = 3f; } } } } void FlipEnemy() { if(this.transform.position.x < this.player.position.x && facingRight) { flip(); } else if(this.transform.position.x > this.player.position.x && !facingRight) { flip(); } } void flip() { Vector3 thescale = this.transform.localScale; thescale.x *= -1; this.transform.localScale = thescale; this.facingRight = !this.facingRight; }
[SerializeField]public bool climbinAllowed;
public bool oninsland2;
Rigidbody2D rb;
Animator anim;
[SerializeField]float dirY;
[SerializeField] Transform point;
[SerializeField] Transform point2;
[SerializeField] GameObject[] ladderd;
// attack
[SerializeField] BoxCollider2D boxCollider;
[SerializeField] LayerMask playermask;
[SerializeField] float attackCooldown;
[SerializeField] float range;
[SerializeField] float cooldownTimer = Mathf.Infinity;// attack now
// rotaion
float value = 3f;
//attack
enemy_controller enemy_Contro;
[SerializeField] float jumpHeight;
[SerializeField]public Transform player;
bool facingRight=false;
void Awake()
{
this.rb = GetComponent<Rigidbody2D>();
this.anim = GetComponent<Animator>();
this.enemy_Contro = GetComponentInParent<enemy_controller>();
}
void Start()
{
this.ladderd[0].SetActive(true);
this.ladderd[1].SetActive(false);
}
// Update is called once per frame
void Update()
{ //attack
this.cooldownTimer += Time.deltaTime;
//Attack only when player in sight
if(PlayerInSight())
{
FlipEnemy();
if(this.cooldownTimer >= this.attackCooldown)
{
this.cooldownTimer = 0;
float distanceFromPlayer = this.player.position.x - this.transform.position.x;
this.rb.AddForce(new Vector2(distanceFromPlayer,jumpHeight),ForceMode2D.Impulse);
}
}
//move up the stairs
if( Vector2.Distance(this.transform.position,this.point.position)<0.2f)
{
this.ladderd[0].SetActive(true);
this.ladderd[1].SetActive(false);
this.oninsland2=false;
this.rb.AddForce(new Vector2(0,jumpHeight),ForceMode2D.Impulse);
}
//move down the stairs
else if( Vector2.Distance(this.transform.position,this.point2.position)<0.2f)
{
this.ladderd[1].SetActive(true);
this.ladderd[0].SetActive(false);
this.oninsland2=true;
}
else
{
if(this.climbinAllowed==true)
{
this.enemy_Contro.enabled=false;
}
else
{
this.enemy_Contro.enabled=true;
}
// stop moving, attack the player
if(this.enemy_Contro != null )
{
this.enemy_Contro.enabled = !PlayerInSight();
}
}
}
//when colliding with a player
private bool PlayerInSight()
{
RaycastHit2D hit = Physics2D.BoxCast(this.boxCollider.bounds.center +
this.transform.right * this.range * this.transform.localScale.x,
this.boxCollider.bounds.size,0,Vector2.left,0,this.playermask);
return hit.collider != null;
}
//ladders
private void FixedUpdate() {
if(this.climbinAllowed)
{
this.anim.SetBool("run",true);
this.rb.isKinematic = true;
if(this.oninsland2==true)
{
this.rb.velocity = new Vector2(0,-this.dirY);
}
else
{
this.rb.velocity = new Vector2(0,this.dirY);
}
}
else
{
this.rb.isKinematic = false;
this.rb.velocity = new Vector2(0,this.rb.velocity.y);
//get up after falling
if(this.transform.rotation.z >0 ||this.transform.rotation.z <0)
{
if(this.value >=0)
this.value -= Time.deltaTime;
else
{
this.transform.rotation = Quaternion.Euler(0, 0, 0);
this.value = 3f;
}
}
}
}
void FlipEnemy()
{
if(this.transform.position.x < this.player.position.x && facingRight)
{
flip();
}
else if(this.transform.position.x > this.player.position.x && !facingRight)
{
flip();
}
}
void flip()
{
Vector3 thescale = this.transform.localScale;
thescale.x *= -1;
this.transform.localScale = thescale;
this.facingRight = !this.facingRight;
}
Post a Comment