I'm really new to Unity, and I'm trying to make a 2.5D platform game. I want to make my enemies walk on a platform and turn when they reach the end of the platform so I placed two triggers at the beginning and at the end of it to change the direction of the enemy.
public class BanditScript : EnemyScript{
// Start is called before the first frame update
private Rigidbody rb;
private Vector3 moveDir;
private bool left;
void Start(){
left = true;
speed = 1;
player = GameObject.FindWithTag("Player");
animator = GetComponent<Animator>();
minDistance = 1;
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update(){
IsNear();
if (near) {
//attack
animator.SetBool("isAttacking", true);
animator.SetBool("isMoving", false);
}
else {
MoveOnPlat();
animator.SetBool("isAttacking", false);
animator.SetBool("isMoving", true);
}
}
void MoveOnPlat() {
animator.SetBool("isMoving", true);
if (left)
moveDir = transform.forward;
else {
moveDir = -transform.forward;
}
rb.velocity = moveDir * speed;
}
private void OnTriggerEnter(Collider other) {
if (other.tag == "platEndL") {
left = false;
transform.rotation = Quaternion.Euler(0, 90, 0);
}
if (other.tag == "platEndR") {
left = true;
transform.rotation = Quaternion.Euler(0, -90, 0);
}
}
}
This is my class, the issue is that the enemy keeps moving in the same direction despite everything. Can you help me?