Hello, So i am currently making a game about a teleporting monster chasing the player (yes a slender clone…) I want the monster to be able to teleport around the player, but I don’t want the player to see the teleportation, so I have made a empty game object that I plan to teleport ahead of the monster, then when it has found a spot where it is not seen by the player, the monster will teleport to that location.
Problem? Well the “finding a place where the player can’t see it” is the problem, kind of makes unity crash. Here’s the script:
using UnityEngine;
using System.Collections;
public class Chase : MonoBehaviour {
GameObject Marker;
Renderer MarkerVis;
bool Visibel;
NavMeshAgent Agent;
Vector3 PlayerPos;
int Randomizer;
// Use this for initialization
void Start () {
Marker = GameObject.Find ("Teleport marker");
Agent = this.GetComponent<NavMeshAgent> ();
MarkerVis = Marker.GetComponent<Renderer> ();
Visibel = false;
}
// Update is called once per frame
void Update () {
PlayerPos = GameObject.Find ("Player").transform.position;
Agent.destination = PlayerPos;
if (Input.GetKeyDown (KeyCode.H)) {
do {
print ("started");
Randomizer = Random.Range (1, 3);
if (Randomizer == 1) {
Marker.transform.position = new Vector3 (Random.Range (PlayerPos.x, PlayerPos.x + 10), 0, Random.Range (PlayerPos.z, PlayerPos.z + 10));
if (MarkerVis.isVisible == false) {
Visibel = false;
print ("success");
}
else {Visibel = true;
}
}
else if (Randomizer == 2) {
Marker.transform.position = new Vector3 (Random.Range (PlayerPos.x, PlayerPos.x + -10), 0, Random.Range (PlayerPos.z, PlayerPos.z + -10));
if (MarkerVis.isVisible == false) {
Visibel = false;
print ("Success");
}
else {Visibel = true;
}
}
} while (Visibel == true);
}
}
}
That’s it, please ask questions if needed.
Thanks in advance.
Btw, I have tested if the gameobject can be seen in a normal not-teleporting enviroment, it can.