I have a pathfinding algorithm that does not seem to be too complicated. but it most certainly is causing frame rate issues, and <i>might</i> be working. Here's the code:
function Update(){ smallObject.transform.position=transform.position; smallObject.transform.position.y+=importantData; smallObject.transform.rotation=transform.rotation; var theposition : Vector3=GameObject.Find("First Person Controller").transform.position; theposition.x-=5; var hit : RaycastHit; var apos : pos; var positions : pos[]=new pos[360]; for (var i : int=0; i<360; i++){ if (Physics.Raycast(smallObject.transform.position, smallObject.transform.forward, 2)==true){ positions.pos=hit.point; } else{ positions.pos=(smallObject.transform.forward*2)+smallObject.transform.position; positions.pos=findHeight(apos); } if (positionDoable(apos)==true){ positions.positionSelect=true; } smallObject.transform.Rotate(0, 0, 1); } apos=positions[1]; apos.high=Vector3.Distance(theposition, apos.pos); for (i=0; i<360; i++){ if (Vector3.Distance(theposition, positions.pos)<apos.high && positions.positionSelect==true){ apos=positions; apos.high=Vector3.Distance(theposition, positions.pos); } } var yourdir : Vector3=apos.pos-smallObject.transform.position; yourdir=yourdir.normalized; GetComponent("Animation").Play("CrouchWalk"); transform.Translate(yourdir*2); }
function findHeight(positioning : pos){ smallObject.transform.position=positioning.pos; var fin : boolean; var thereal : Vector3; for (var i=0; i<100; i++){ smallObject.transform.Translate(smallObject.transform.up); if (Physics.Raycast(smallObject.transform.position, smallObject.transform.up, 1)==true){ thereal=smallObject.transform.position; fin=true; } } if (fin!=true){ for (i=0; i<100; i++){ smallObject.transform.Translate(-(transform.transform.up)); if (Physics.Raycast(smallObject.transform.position, -(smallObject.transform.up), 1)==true){ thereal=smallObject.transform.position; fin=true; } } } smallObject.transform.position=transform.position; return thereal; }
function findPosition(positioning : pos){ var thereal : pos; thereal=positioning; thereal.pos.y=positioning.high; return thereal.pos; }
function findDistance(positioning : pos){ var key : int=Vector3.Distance(positioning.pos, GameObject.Find("First Person Controller").transform.position); return key; }
function positionDoable(positioning : pos){ var positionDir : Vector3=positioning.pos-transform.position; var ahit : RaycastHit; if (Physics.Linecast(transform.position, positioning.pos)){ var positionNormal : Vector3=ahit.normal; } var projectLength : float=Vector3.Dot(positionDir, positionNormal); var coarseDirection : Vector3=positionDir-(projectLength*positionNormal); coarseDirection=coarseDirection.normalized; var theangle=Mathf.Asin(coarseDirection.y)*Mathf.Rad2Deg; if (theangle>=45){ thetruth=false; } else{ thetruth=true; } return thetruth; }
function walk(positioning : Vector3){ GetComponent("Animation").Play("CrouchWalk"); var rotx : int=transform.eulerAngles.x; var rotz : int=transform.eulerAngles.z; transform.LookAt(positioning); transform.eulerAngles.x=rotx; transform.eulerAngles.z=rotz; transform.Translate(transform.forward*mover); GetComponent("Animation").Play("CrouchWalk"); }
anyone have any idea if this is a) a plausible pathfinding algorithm and what might be causing it to take too long to execute?
The code casts a ray in front of the object, at a distance of about 2 units. Then it gets appropriate data about it (it finds if the object can climb the obstacle, the height, the distance from the target). This is done 360 times. Then, the best path is selected from the entire list, depending on which is closest to the target, and if it is possible to go forward in that direction. There's a lot of raycasting involved in general. Sorry 'bout the lack of the summary.
Over 2 units, most of those 360 rays would hit the same stuff over and over. You could get away with shooting rays at 10 degree intervals... or 30. Also, why shoot rays behind you?
Additionally, this is not even remotely "pathfinding" in the typical sense. Perhaps you are trying to come up with a "steering" solution? If so, there are tons of already perfectly viable ways of doing that.
The common go-to location is Craig Reynolds site. However, there are dozens if not hundreds of similar implementations.
Arghh! I have even more issues! It turns out that the findHeight and positionDoable functions were taking the wrong variable. They were taking an empty variable. Now I switched them to take the variable for the position being currently investigated. This does not allow the object to move. I can't figure it out! Here's the new code for the Update function:
var smallObject : GameObject; var theanimation=GameObject.Find("Soldier").GetComponent("Animation"); var mover : int; var display : Vector3; private var importantData : float=13.4-10.9;
function Update(){ smallObject.transform.position=transform.position; smallObject.transform.position.y+=importantData; smallObject.transform.rotation=transform.rotation; var theposition : Vector3=GameObject.Find("First Person Controller").transform.position; theposition.x-=5; var hit : RaycastHit; var apos : pos; var positions : pos[]=new pos[360]; for (var i : int=0; i<360; i++){ if (Physics.Raycast(smallObject.transform.position, smallObject.transform.forward, 2)==true){ positions.pos=hit.point; } else{ positions.pos=(smallObject.transform.forward*2)+smallObject.transform.position; positions.pos=findHeight(positions); } if (positionDoable(positions)==true){ positions.positionSelect=true; } else{ positions.positionSelect=false; } smallObject.transform.Rotate(0, 0, 1); } apos=positions[1]; apos.high=Vector3.Distance(theposition, apos.pos); for (i=0; i<360; i++){ if (Vector3.Distance(theposition, positions.pos)<apos.high && positions.positionSelect==true){ apos=positions; apos.high=Vector3.Distance(theposition, positions.pos); } } var yourdir : Vector3=apos.pos-smallObject.transform.position; yourdir=yourdir.normalized; GetComponent("Animation").Play("CrouchWalk"); transform.Translate(yourdir*2); }
Ok, I'm posting this because of the utter strangeness of it. So I tried to step through the code. While figuring out how that works in Unity 3d, I somehow, idiotically, I might add, managed to lose my project. So I opened another project, while cursing myself, and exported the assets of the original into the new one. I set up the new one like the old one, attached my pathfinding script, and miraculously, it began to work. It didn't work the way I expected, as always, but still, the mear fact that it worked..................I still can't figure out what's different in the code in this copy that allows it to work. I don't think there's anything different, but I've never quite seen this happen before..............anyone know what that's all about?