- Home /
Question by
andreyazbyn · Sep 09, 2015 at 06:55 PM ·
c#2d2d-platformershootingenemy ai
2d enemy ai
I want to make a enemy ai that patrols until it spots the player or it hears it shooting. if it spots the player it fires at him ( like the enemy soldiers from metal slug) this is what i have so far:
using UnityEngine;
using System.Collections;
public class EnemyPatroll : MonoBehaviour {
public float moveSpeed;
public bool moveRight = false;
public Transform wallCheck;
public float wallCeckRadius;
public LayerMask whatIsWall;
private bool hittingWall;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
hittingWall = Physics2D.OverlapCircle(wallCheck.position, wallCeckRadius, whatIsWall);
if(hittingWall)
moveRight = !moveRight;
if(moveRight ){
transform.localScale = new Vector3 (-1f, 1f,1f);
GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed,GetComponent<Rigidbody2D>().velocity.y);
}
else{
transform.localScale = new Vector3 (1f, 1f,1f);
GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed,GetComponent<Rigidbody2D>().velocity.y);
}
}
}
Comment