I have a 3d tile based map. I am trying to get an enemy to follow the wall of a room (either the wall on it's left or right consistently per enemy).
![Path for enemy][1]
[1]: /storage/temp/111068-path.jpg
The yellow line above is only indicative of direction and movement, it should still move to the tiles and trace around the outer walls.
The code I have will turn around ok when it hits a wall, but will still cut across the room even if there is a path around the outside.
void Update() {
transform.LookAt(moveDestination);
if(transform.position.x == moveDestination.x && transform.position.z == moveDestination.z) {
isMoving = false;
} else {
isMoving = true;
}
if(isMoving == false) {
RaycastHit hit;
if(Physics.Raycast(transform.position,transform.forward,out hit,scale)) {
if(Physics.Raycast(transform.position,-transform.right,out hit,scale)) {
transform.LookAt(transform.position + (-transform.right * scale));
} else {
transform.LookAt(transform.position + (-transform.right * scale));
}
} else {
moveDestination = transform.position + (transform.forward * scale);
}
}
}
void FixedUpdate() {
transform.position = Vector3.MoveTowards(transform.position,moveDestination,moveSpeed * Time.deltaTime);
}
↧