- Home /
Enemy AI not working?,Enemy not moving correctly
Hi, I'm trying to make an enemy move left and right within a certain time, but the enemy is only moving right and isn't playing the animations correctly. My script is shown below: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SnakeController : MonoBehaviour { public float speed = 1.0f; public Rigidbody2D rigidbody2d; Vector2 movement; public float changeTime = 3.0f;
public Animator animator;
float timer = 1.0f;
int direction = 1;
// Start is called before the first frame update
void Start()
{
timer = changeTime;
}
// Update is called once per frame
void Update()
{
timer -= Time.deltaTime;
movement.x = movement.x + Time.deltaTime * direction;
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Speed", movement.x * direction);
if (timer < 0)
{
direction = -direction;
timer = changeTime;
}
rigidbody2d.MovePosition(rigidbody2d.position + movement * speed * Time.deltaTime);
}
} Is anyone able to help?
Answer by warthos3399 · Oct 26, 2019 at 12:06 AM
Do you have a NavMesh baked?
I don't believe I do, just an animator and the script above attached to the enemy sprite.
Your answer
Follow this Question
Related Questions
How to make an enemy pace? 1 Answer
Mirroring Animation with Object Move 2 Answers
SpriteManager 2 1 Answer
Moving Player only during part of an animation 1 Answer
Character controller + animation of different meshes making one character 1 Answer