Hey! Probably the best (and easiest) type of random motion here would be where the NPC's keep on walking until they almost collide into something, at which point they turn away. To do this in Unity you'd attach a collider to the NPC which is larger than the actual NPC (to create a kind of range around it). You'd then set this collider so it doesn't actually use physics (so it doesn't prevent the NPC from moving) but instead is a **trigger** - something which lets some code know that a collision happened (take a look at onTriggerEnter :) ). To get the NPC to just move forward, you'd attach a script to it which uses the Update routine.
A really simple version of that would be this (Javascript):
var useX:boolean=true;
function Update(){
if(usex){
transform.position.x+=Time.deltaTime*speed;
}else{
transform.position.z+=Time.deltaTime*speed;
}
}
function OnTriggerEnter(other:Collider){
//something came in range - flip the direction
usex=!usex;
}
To make this a little more affective (It would cause the NPC to sort of zigzag across in the positive x/z direction), you could instead use a rotation to represent the direction the NPC is travelling in, then change that when the NPC get's close to something :)
In terms of dynamically generating the dungeons, it's best to generate 1 full Mesh object rather than lots of smaller ones. There is a function which can merge them together (see Mesh.Combine) which could help you with that :)
Hope that helps!
Luke
↧