2
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ Don't forget to use CompareTag() instead of .tag == for efficiency. \$\endgroup\$ – DMGregory Jul 19 '19 at 16:41
2
\$\begingroup\$

The issue you're having comes from the fact that you change the direction of your enemy twice, effectively canceling the effect you're after.

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);
    }
}

Here,

    if (left)
        moveDir = transform.forward;
    else {
        moveDir = -transform.forward;
    }
    rb.velocity = moveDir * speed;

changes the direction in which is going, while

    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);
    }

changes the orientation.

Assuming the enemy walks "forward", it's as if you asked it to "turn around" and then "walk in reverse".

To fix this, you need to drop one of the other.

I suggest you keep the transform.rotation = and drop the moveDir part, making it as if your object would always go forward:

void MoveOnPlat() {
    animator.SetBool("isMoving", true);
    rb.velocity = transform.forward * speed;
}
|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

If your making this in 2D your "OnTriggerEnter" needs to be "OnTriggerEnter2D, otherwise it won't work. This could be your problem.

Hope this helps

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.