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.