Automated Movement And Collisions

Greetings! Total c# noob.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class GreenWayfinder : MonoBehaviour
{
    public float speed;
    private Rigidbody2D greenHero;

    public bool isWalking;
    public float walkTime;
    private float walkCounter;
    public float waitTime;
    private float waitCounter;
    public int walkDirection;


    void Start()
    {
        greenHero = GetComponent<Rigidbody2D>();
        waitCounter = waitTime;
        walkCounter = walkTime;

        ChooseDirection();
    }


    void Update()
    {
               
        //Movement
        if (isWalking)
        {
            walkCounter -= Time.deltaTime;

            switch (walkDirection)
            {
                case 0:
                    greenHero.velocity = new Vector2(0, speed);
                    break;

                case 1:
                    greenHero.velocity = new Vector2(0, -speed);
                    break;

                case 2:
                    greenHero.velocity = new Vector2(speed, 0);
                    break;

                case 3:
                    greenHero.velocity = new Vector2(-speed, 0);
                    break;
            }
       

            if (walkCounter <= 0)
            {
                isWalking = false;
                waitCounter = waitTime;
            }
        }
        else
        {
            waitCounter -= Time.deltaTime;

            greenHero.velocity = Vector2.zero;

            if (waitCounter < 0)
            {
                ChooseDirection();
            }
        }
    }

    //Colision detection and functionality
    public void OnControllerColliderHit(ControllerColliderHit hit)
    {
        switch (hit.gameObject.tag)
        {
            case "Door":
                transform.position = hit.transform.GetChild(0).position;
                break;
        }
    }

    //Random direction selection
    public void ChooseDirection()
    {
        walkDirection = Random.Range (0, 4);
        isWalking = true;
        walkCounter = walkTime;
    }


}

I set up a script that causes the enemy character (greenHero) to move in random directions. The issue I am having is that when the enemy collides with a door, I want to teleport them to the other side. They tend to get stuck in hallways otherwise.

The issue I am having is that I cannot get the character to actually teleport. It seems like it is only obeying the random movement, and ignoring what it should do when hitting the door colider. Seems like I need to do some kind of check in the movement part, but I am not sure exactly how to do that.

You may have to normalize the position. “hit.transform.GetChild(0).position” might be relative to the hit, not the global space.

Barring that, you should just debug. Does OnControllerColliderHit get called? Is the tag really “Door?” If so, what’s the position before and after the change?

Thanks for the reply!
I was able to fix this with some additional noodling. It looks like I had to use OnCollision2D since the door is using a 2D colider. Doing that (and doing some other tweaks), it looks like it works now.