Enemy Run

tell me what is wrong in the script? I wanted to make the enemy start running only when I appear in the frame, but he always runs.

private bool onGround;
public Transform groundSensor;

private bool cliffAhead;
public Transform cliffSensor;

public LayerMask ground;

private Rigidbody2D rb;
private Animator myAnimator;
bool isVisible = false;
public float speed;
public float jumpHeight;

private bool reacted;

// Use this for initialization
void Start () {

myAnimator = GetComponent();
rb = GetComponent();
}

// Update is called once per frame
void Update () {

if (isVisible = true) {

float x = Input.GetAxis (“Horizontal”);

Vector3 move = new Vector3 (speed * transform.localScale.x, rb.velocity.y);

rb.velocity = move;

}

}
}

if (isVisible = true)
This is a syntax error, it should be either:

  • if (isVisible == true)

  • if (isVisible)

i was write like there, but its not wokred

you should change isVisible depended on visiblity. Maybe with events

void OnBecameVisible() {
isVisible = true;
}

void OnBecameInvisible() {
isVisible = false;
}