- Home /
Question by
Endercry365 · May 23, 2019 at 06:39 PM ·
movement2d gameenemy aipixel
Enemy Is not moving
In my 2D pixel RPG, I have a slime enemy that i am currently just trying to get it to move in random directions. For some reason, it is not moving at all. if you can help me fix this problem i would appreciate it greatly.
Here is my code:
public float moveSpeed;
private Rigidbody2D myRigidbody;
private bool moving;
public float timeBetweenMove;
private float timeBetweenMoveCounter;
public float timeToMove;
private float timeToMoveCounter;
private Vector3 moveDirection;
// Start is called before the first frame update
void Start() {
myRigidbody = GetComponent<Rigidbody2D>();
timeBetweenMoveCounter = timeBetweenMove;
timeToMoveCounter = timeToMove;
}
// Update is called once per frame
void Update() {
if (moving) {
timeToMoveCounter -= 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);
}
}
}
}
Comment
Answer by highpockets · May 23, 2019 at 08:57 PM
Doesn’t look like you are setting the bool “moving” to true at the start which I think you meant.
Your answer
Follow this Question
Related Questions
Simple 2D Enemy AI 3 Answers
How to make a object move right automatically in 2d. 2 Answers
Aceleration 2D 0 Answers
Why does this piece of code work with gravity and this one dosent 2 Answers