Hey Guys,
So I started coding the movement for my first enemy character.
The enemy is a square....and I want him to roll on his side every 1 seconds or so as he move towards the player.
But currently, he just moves straight toward the player.
I tried putting in some move intervals but navigation seems to just ignore that and go straight toward the player.
I have an animation set up to rotate 45 degrees as long as he is moving.
How I can make it look like the enemy is just rolling towards the enemy every other second? start, stop, start, stop.
using UnityEngine;
using System.Collections;
public class AI_CubeEnemy : MonoBehaviour {
public Animator anim;
public float moveTimer = 0;
public float rollTimer = 0;
public float moveInterval = 2;
public bool isWalkingLeft = false;
public bool isWalkingRight = false;
private Rigidbody cubeEnemy;
Transform player;
NavMeshAgent nav;
void Awake ()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
nav = GetComponent<NavMeshAgent>();
anim = gameObject.GetComponent<Animator>();
cubeEnemy = gameObject.GetComponent<Rigidbody>();
}
void Update()
{
anim.SetBool("isWalkingRight", isWalkingRight);
anim.SetBool("isWalkingLeft", isWalkingLeft);
moveTimer += Time.deltaTime;
if (moveTimer >= moveInterval)
{
if (rollTimer == 0)
{
while (rollTimer <= 2)
{
if (player.transform.position.x > transform.position.x)
{
isWalkingRight = true;
isWalkingLeft = false;
}
if (player.transform.position.x < transform.position.x)
{
isWalkingLeft = true;
isWalkingRight = false;
}
nav.SetDestination(player.position);
rollTimer += Time.deltaTime;
}
moveTimer = 0;
nav.speed = 0;
rollTimer = 0;
}
}
}
}
with the nav.speed = 0; the enemy doesn't move at all, when I comment it out, it moves towards the player.
It's like it ignores all my conditional statements.