- Home /
Maze navigation
Hey this script is meant to navigate an enemy yhrough a maze to the character it works outside the maze but when i put them inside nothing happens. Can anyone help??
function Update(){
var target = GameObject.FindWithTag("Player");
var right = transform.TransformDirection(Vector3.right);
var left = transform.TransformDirection(Vector3.left);
var fwd = transform.TransformDirection(Vector3.forward);
if (!Physics.Raycast(transform.position, fwd, 20) & !Physics.Raycast(transform.position, right, 40) & !Physics.Raycast(transform.position, left, 40)){
transform.Translate(Vector3.zero);
transform.LookAt(target.transform.position);
transform.Translate(Vector3.forward * 15 * Time.deltaTime);
}
else if (!Physics.Raycast(transform.position, fwd, 20)) {
transform.Translate(Vector3.zero);
transform.Translate(Vector3.forward * 15 * Time.deltaTime);
}
else if (!Physics.Raycast(transform.position, right, 40)) {
transform.Translate(Vector3.zero);
transform.Rotate(0,25,0);
}
else if (!Physics.Raycast(transform.position, left, 40)) {
transform.Translate(Vector3.zero);
transform.Rotate(0,25,0);
}
}
Are the raycasts hitting anything? You might add some Debug.Log() calls to follow the program's execution, or use $$anonymous$$onoDevelop's built in debugger to step through the code as it runs.
I'll have a look but it works outside of the maze so that's what confuses me
Well a maze is a pretty tight environment so it could just get stuck I'd guess, especially if it wasn't perfectly rotated in the sense of the maze, all of those ray casts could hit causing it to do nothing.
so could i fix that in any way or do i have to use a new statement??
Yes the fix can be compared to "how to fix my paper-airplane so i can fly with it over the ocean". The answer is: "build a real airplane".
All you have there is a very (very) simple collision avoidance system which is far from being a navigation logic. For navigating a maze you need something like a pathfinding algorithm (A* for example which is the most common and simplest one). There is no easy "fix" for your problem.
Answer by TonyLi · May 17, 2013 at 03:19 PM
http://u3d.as/content/angry-ant/path/1zq
Asset Store description:
Path By AngryAnt, Free: The Path project is an easy to use, powerful, waypoint based pathfinding solution for Unity.
Its primary goal is to allow the user to quickly and easily add pathfinding to a scene and immediately take advantage of it through a super simple API. At the same time, the before mentioned API also holds powerful accessors for runtime throttling of the co-routine based seekers, custom weights, path recalculation and more.
Documentation is available at http://angryant.com/path
Your answer
![](https://koobas.hobune.stream/wayback/20220613102414im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Any recommendations for having randomized obstacles for a nav mesh? 1 Answer
Ai Upgrade?? Possible 1 Answer
NavMeshAgent not moving 0 Answers
Ai Zombie Melee Attack script. 5 Answers