Hey,
i created a script for an enemy ai and it works… A little bit…
It makes the enemy moving in random directions and change them after a short time.
The problem is that it does this once and then keeps the direction.
What have i done wrong?
MY SCRIPT
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PrototypeBoss1 : MonoBehaviour {
public float moveSpeed;
private Rigidbody2D myRigidbody;
private bool moving;
public float timeBetweenMove;
private float timeBetweenMoveCounter;
public float timeToMove;
private float timeToMoveCounter;
private Vector3 moveDirection;
// Use this for initialization
void Start () {
myRigidbody = GetComponent();
timeBetweenMoveCounter = timeBetweenMove;
timeToMoveCounter = timeToMove;
}
// Update is called once per frame
void Update () {
if (moving)
{
timeBetweenMoveCounter -= Time.deltaTime;
myRigidbody.velocity = moveDirection;
if(timeToMoveCounter < 0f)
{
moving = false;
timeBetweenMoveCounter = timeBetweenMove;
}
}
else
{
timeBetweenMoveCounter -= Time.deltaTime;
myRigidbody.velocity = Vector2.zero;
if(timeBetweenMoveCounter < 0f)
{
moving = true;
timeToMoveCounter = timeToMove;
moveDirection = new Vector3(Random.Range(-1f, 1f) * moveSpeed, Random.Range(-1f, 1f) * moveSpeed, 0f);
}
}
}
}