Hi guys,
So I have the question: How can I create a first person character after my algorithm made a maze?
I already created the algorithm to make a maze, but can I create a first person character after the maze is created? So that the person will be at point (X,Z) and that you have to solve the maze?
I hope that someone can help me.
thanks very much
hopper
October 2, 2014, 3:22am
2
Try this. And for the ‘character’ variable, set it to a Prefab so that it should spawn the character with all of the scripts that you want.
#pragma strict
var character : Transform;
var spawnPoint : Transform;
var mazeIsCreated = false; //Change this in your maze building script
function Update ()
{
if(mazeIsCreated)
{
var player = Instantiate(character, spawnPoint.position, spawnPoint.rotation);
mazeIsCreated = false;
return;
//If you have any other things you want to do to the character (add animations, call functions, etc.), put them here...
}
}
Thanks very much, it worked. But when I spawn my character it just falls through the maze?
This is the script:
#pragma strict
#pragma downcast
//* -> Assigned in Inspector
public var CellPrefab : Transform; //*
public var GridSize : Vector3; //*
public var GridArr : Transform[,];
public var Set = new Array();
public var CompletedSet = new Array();
public var character : Transform;
function Start () {
CreateGrid();
SetStart(0,0);
yield WaitForSeconds(5);
var player = Instantiate(character, Vector2(0,0), Quaternion.identity);
}
function CreateGrid(){
var maxXZ : int = Mathf.Max(GridSize.x, GridSize.z);
Camera.mainCamera.transform.position = Vector3(maxXZ/2f, maxXZ, maxXZ/8f);
var x : int = GridSize.x;
var z : int = GridSize.z;
GridArr = new Transform[x,z];
for(var ix = 0; ix < GridSize.x; ix++)
{
for(var iz = 0; iz < GridSize.z; iz++)
{
var newCell : Transform =
Instantiate(CellPrefab, Vector3(ix,0,iz), Quaternion.identity);
newCell.name = "("+ix+",0,"+iz+")";
newCell.parent = transform;
var newCellScript : CellScript = newCell.GetComponent("CellScript");
newCellScript.Position = Vector3(ix,0,iz);
GridArr[ix,iz] = newCell;
}
}
}
function SetStart(x : int, z : int){
AddToSet(GridArr[x,z]);
}
function AddToSet(toAdd : Transform){
toAdd.renderer.material.color = Color.gray;
var taScript : CellScript = toAdd.GetComponent("CellScript");
taScript.IsOpened = true;
Set.Unshift(toAdd);
}
var Directions : Vector3[]; //*
function FindNext(){
if(Set.length == 0){
Debug.Log("We're done.");
return;
}
var previous : Transform = Set[0];
var pScript : CellScript = previous.GetComponent("CellScript");
var next : Transform;
var nextScript : CellScript;
var prevX : int = pScript.Position.x;
var prevZ : int = pScript.Position.z;
var randDirection : Vector3;
var randSeed : int = Random.Range(0,4);
var nextX : int;
var nextZ : int;
var counter : int;
do{
do{
randDirection = Directions[randSeed];
nextX = prevX+randDirection.x;
nextZ = prevZ+randDirection.z;
randSeed = (randSeed+1) % 4;
counter++;
if(counter > 4){
Set.RemoveAt(0);
previous.renderer.material.color = Color.black;
yield WaitForEndOfFrame();
return;
}
}while(nextX < 0 || nextZ < 0 || nextX >= GridSize.x || nextZ >= GridSize.z);
next = GridArr[nextX,nextZ];
nextScript = next.GetComponent("CellScript");
}while(nextScript.IsOpened);
AddToSet(next);
DrawDebugLines(10, previous, next);
ClearWalls(previous, next);
yield WaitForEndOfFrame();
}
function DrawDebugLines(lines :int, p : Transform, n : Transform)
{
for(var i = 0; i < lines; i++)
{
Debug.DrawLine(p.position, n.position, Color.magenta, 100);
}
}
function ClearWalls(p : Transform, n : Transform)
{
var hitInfo : RaycastHit[];
hitInfo = Physics.RaycastAll(p.position + Vector3.up, n.position - p.position, 1);
for(var i : int = 0; i < hitInfo.length; i++) {
Destroy(hitInfo*.transform.gameObject);*
function Update ()