here is my script the error is on line 78 Vector3 CheckGround (Vector3 pos) {
I have tried several ways to correct this but still coming up with the same issue - how do I go about writing this code so I can fix this error or what do I need to change in the script so this can work properly - I tried using a debugger but of course it wouldn’t tell me a solution on how to fix the issue. - if anyone can help me I would appreciate it
thanks in advance
public class EnemyAI : MonoBehaviour {
public float gravity;
public Vector2 velocity;
public bool isWalkingLeft = true;
LayerMask floorMask;
private bool grounded = false;
private enum EnemyState {
walking,
falling,
dead
}
private EnemyState state = EnemyState.falling;
// Use this for initialization
void Start () {
enabled = false;
fall ();
}
// Update is called once per frame
void Update () {
UpdatedEnemyPosition ();
}
void UpdatedEnemyPosition () {
if (state != EnemyState.dead) {
Vector3 pos = transform.localPosition;
Vector3 scale = transform.localScale;
if (state == EnemyState.falling) {
pos.y += velocity.y * Time.deltaTime;
velocity.y -= gravity * Time.deltaTime;
}
if (state == EnemyState.walking) {
if (isWalkingLeft) {
pos.x -= velocity.x * Time.deltaTime;
scale.x = -1;
} else {
pos.x += velocity.x * Time.deltaTime;
scale.x = 1;
}
}
if (velocity.y <= 0)
pos = CheckGround (pos);
transform.localPosition = pos;
transform.localScale = scale;
}
}
Vector3 CheckGround (Vector3 pos) {
Vector2 originLeft = new Vector2 (pos.x - 0.5f + 0.2f, pos.y - .05f);
Vector2 originMiddle = new Vector2 (pos.x, pos.y - .5f);
Vector2 originRight = new Vector2 (pos.x + 0.5f + 0.2f, pos.y - .05f);
RaycastHit2D groundLeft = Physics2D.Raycast (originLeft, Vector2.down, velocity.y * Time.deltaTime, floorMask);
RaycastHit2D groundMiddle = Physics2D.Raycast (originMiddle, Vector2.down, velocity.y * Time.deltaTime, floorMask);
RaycastHit2D groundRight = Physics2D.Raycast (originRight, Vector2.down, velocity.y * Time.deltaTime, floorMask);
if (groundLeft.collider != null || groundMiddle.collider != null || groundRight.collider != null) {
RaycastHit2D hitRay = groundLeft;
if (groundLeft) {
hitRay = groundLeft;
} else if (groundMiddle) {
hitRay = groundMiddle;
} else if (groundRight) {
hitRay = groundRight;
}
pos.y = hitRay.collider.bounds.center.y + hitRay.collider.bounds.size.y / 2 + .5f;
grounded = true;
velocity.y = 0;
state = EnemyState.walking;
} else {
if (state != EnemyState.falling) {
fall ();
}
}
}
void OnBecameVisible () {
enabled = true;
}
void fall () {
velocity.y = 0;
state = EnemyState.falling;
grounded = false;
}
}