//we will only try to avoid colliding into 1 type of zombie: the shambler, because classic zombies can't change lanes. //However, since classic zombies have a max speed greater than that of the shambler's, if a raycast FORWARD hits a shambler, it will slow down to v/2 to match the shambler //only for the duration that the shambler hits the classic zombie's raycast. void AvoidZombieCollision() { //if the raycast doesn't hit a shambler, let speed be v (the normal max speed) bool hitShambler = false; //these lines of code will shoot the raycast, and then will try to change lanes if raycast hits. RaycastHit front; Ray frontRay = new Ray(transform.position + transform.forward * .5f + transform.up * .5f, transform.forward); Debug.DrawRay(transform.position + transform.forward * .5f + transform.up * .5f, transform.forward, Color.green); if (Physics.Raycast(frontRay, out front, 1.5f)) { if (front.collider.gameObject.tag == "Zombie") { //this code will check if the zombie is of type shambler. ShamblerZombie script = front.collider.gameObject.GetComponent <ShamblerZombie>(); if (script != null) { aSpeed = ZombieConstants.v / 2; hitShambler = true; } //this code will check if the zombie is of type shambler. ClassicZombie classicScript = front.collider.gameObject.GetComponent <ClassicZombie>(); if (classicScript != null) { aSpeed = classicScript.aSpeed; hitShambler = true; } } } //here the speed is returned to normal as soon as the raycast stops hitting a shambler if (hitShambler == false) { aSpeed = ZombieConstants.v; } }
void spawnZombies() { hardZombies = (int)(n * r / (1 + r)); int[] dir = new int[3]; //iterate to spawn n zombies for (int i = 0; i < n; i++) { //starting and ending waypoints on lane int startingWpt = 0; int endWpt = 0; //choose random lane int lane = Random.Range(0, 3); //set direction if (dir[lane] == 0) { dir[lane] = Random.Range(1, 3); } startingWpt = Random.Range(0, 4); //choose random starting point if (dir[lane] == 1) { //left direction in lane //make sure no zombie spawns in survivor area at first startingWpt = Random.Range(0, 4); while (startingWpt == 1) { startingWpt = Random.Range(0, 4); } switch (startingWpt) { case 0: endWpt = 3; break; case 1: endWpt = 0; break; case 2: endWpt = 1; break; case 3: endWpt = 2; break; } } else if (dir[lane] == 2) { //right direction in lane //make sure no zombie spawns in survivor area at first startingWpt = Random.Range(0, 4); while (startingWpt == 0) { startingWpt = Random.Range(0, 4); } switch (startingWpt) { case 0: endWpt = 1; break; case 1: endWpt = 2; break; case 2: endWpt = 3; break; case 3: endWpt = 0; break; } } /*initialize zombies*/ Vector3 s = Vector3.zero; Vector3 e = Vector3.zero; switch (lane) { case 0: s = innerPoints[startingWpt].position; e = innerPoints[endWpt].position;; break; case 1: s = middlePoints[startingWpt].position; e = middlePoints[endWpt].position;; break; case 2: s = outerPoints[startingWpt].position; e = outerPoints[endWpt].position;; break; } s = Vector3.Lerp(s, e, Random.value); //Check if there is a zombie already if (Physics.CheckSphere(s, 0.49f)) { i--; continue; } if (i < hardZombies) { //spawn a hard zombie if (Random.value > 0.5f) { //modern ModernZombie z = (ModernZombie)Instantiate(modernPrefab) as ModernZombie; z.Initialize(s, e, startingWpt, endWpt, lane, v * 2.0f); //speed = 2v zombies.Add(z); z.transform.position = s; z.isEasy = false; z.transform.tag = "Zombie"; z.name = "ModernZombie"; } else { //phone PhoneZombie z = (PhoneZombie)Instantiate(phonePrefab) as PhoneZombie; z.Initialize(s, e, startingWpt, endWpt, lane, v * 2.0f); //speed = 2v zombies.Add(z); z.transform.position = s; z.isEasy = false; z.transform.tag = "Zombie"; z.name = "PhoneZombie"; } } else { //spawn easy if (Random.value > 0.5f) { //classic zombie instanciation ClassicZombie z = (ClassicZombie)Instantiate(classicPrefab) as ClassicZombie; z.Initialize(s, e, startingWpt, endWpt, lane, v); zombies.Add(z); z.transform.position = s; z.isEasy = true; z.transform.tag = "Zombie"; z.name = "ClassicZombie"; } else { //shambler ShamblerZombie z = (ShamblerZombie)Instantiate(shamblerPrefab) as ShamblerZombie; z.Initialize(s, e, startingWpt, endWpt, lane, v / 2.0f); //speed = v/2 zombies.Add(z); z.transform.position = s; z.isEasy = true; z.transform.tag = "Zombie"; z.name = "ShamblerZombie"; } } } }
public void spawnOneZombie(bool easy, bool clockwiseDirection) { int nextWpt = 0; int wptI = 0; int lane = 0; Vector3 spawnPos = Vector3.zero; Vector3 endWpt = Vector3.zero; //find random unnocopied corner to spawn on try { do { wptI = Random.Range(0, 4); lane = Random.Range(0, 3); switch (lane) { case 0: spawnPos = innerPoints [wptI].position; break; case 1: spawnPos = middlePoints [wptI].position; break; case 2: spawnPos = outerPoints [wptI].position; break; } } while(Physics.CheckSphere(spawnPos, 0.49f)); } catch (System.Exception ex) { Debug.Log(ex); } //choose random direction if (!clockwiseDirection) { //counter clockwise nextWpt = (wptI + 3) % 4; } else { //clockwise nextWpt = (wptI + 1) % 4; } if (lane == 0) { endWpt = innerPoints [nextWpt].position; } else if (lane == 1) { endWpt = middlePoints [nextWpt].position; } else { endWpt = outerPoints[nextWpt].position; } if (!easy) { //spawn a hard zombie if (Random.value > 0.5f) { //modern ModernZombie z = (ModernZombie)Instantiate(modernPrefab) as ModernZombie; z.Initialize(spawnPos, endWpt, wptI, nextWpt, lane, v * 2.0f); //speed = 2v zombies.Add(z); z.transform.position = spawnPos; z.isEasy = false; z.transform.tag = "Zombie"; z.name = "ModernZombie"; } else { //phone PhoneZombie z = (PhoneZombie)Instantiate(phonePrefab) as PhoneZombie; z.Initialize(spawnPos, endWpt, wptI, nextWpt, lane, v * 2.0f); //speed = 2v zombies.Add(z); z.transform.position = spawnPos; z.isEasy = false; z.transform.tag = "Zombie"; z.name = "PhoneZombie"; } } else { //spawn easy if (Random.value > 0.5f) { //classic zombie instanciation ClassicZombie z = (ClassicZombie)Instantiate(classicPrefab) as ClassicZombie; z.Initialize(spawnPos, endWpt, wptI, nextWpt, lane, v); zombies.Add(z); z.transform.position = spawnPos; z.isEasy = true; z.transform.tag = "Zombie"; z.name = "ClassicZombie"; } else { //shambler ShamblerZombie z = (ShamblerZombie)Instantiate(shamblerPrefab) as ShamblerZombie; z.Initialize(spawnPos, endWpt, wptI, nextWpt, lane, v / 2.0f); //speed = v/2 zombies.Add(z); z.transform.position = spawnPos; z.isEasy = true; z.transform.tag = "Zombie"; z.name = "ShamblerZombie"; } } }
/* * This method will raycast to both back and front. If the front raycast is hit, it will first slow down to the front zombie's speed. * Then it will try to change lanes. We slow down so that if we cannot change lanes, it will not collide with the front zombie! * If the back raycast is hit, it will just try to change lanes without changing speed. */ void AvoidZombieCollision() { bool hitFrontZombie = false; //these lines of code will shoot the raycast, and then will try to change lanes if raycast hits. RaycastHit front; Ray frontRay = new Ray(transform.position + transform.forward * .5f + transform.up * .5f, transform.forward); Debug.DrawRay(transform.position + transform.forward * .5f + transform.up * .5f, transform.forward, Color.green); //change speeds and change lanes according to the zombie raycast hits. if (Physics.Raycast(frontRay, out front, 2)) { if (front.collider.gameObject.tag == "Zombie") { //this code will check if the zombie is of type shambler. ShamblerZombie shamblerScript = front.collider.gameObject.GetComponent <ShamblerZombie>(); if (shamblerScript != null) { aSpeed = ZombieConstants.v / 2; //change speed to v/2, since shambler has only 1 speed hitFrontZombie = true; RandomlyChangeLanes(); } ModernZombie modernScript = front.collider.gameObject.GetComponent <ModernZombie>(); if (modernScript != null) { aSpeed = modernScript.aSpeed; hitFrontZombie = true; RandomlyChangeLanes(); //Debug.Log ("BADD"); } ClassicZombie classicScript = front.collider.gameObject.GetComponent <ClassicZombie>(); if (classicScript != null) { aSpeed = classicScript.aSpeed; hitFrontZombie = true; RandomlyChangeLanes(); } } } //if back raycast hits, just try to change lanes (not speed) RaycastHit back; Ray backRay = new Ray(transform.position + -transform.forward * .5f + transform.up * .5f, -transform.forward); //Debug.DrawRay(transform.position + transform.forward * .5f + transform.up * .5f , transform.forward, Color.green); if (Physics.Raycast(backRay, out back, 2)) { if (back.collider.gameObject.tag == "Zombie") { RandomlyChangeLanes(); } } //if there is nothing in front of the zombie, resume normal max speed. if (!hitFrontZombie) { aSpeed = ZombieConstants.v * 2; } }
//this will spawn a zombie at the opposite corner. the lane will be random public void TrySpawn(string corner) { //first instantiate a zombie based on p string zombieType = "none"; //a bullshit way of telling which zombie is instantiated, coz unity is dumb. string lane = "none"; string dir = "none"; GameObject zombieSpawn; int rand = Random.Range(0, 100); PhoneZombie phone = null; ModernZombie modern = null; ClassicZombie classic = null; ShamblerZombie shambler = null; DetectSurvivor detect = null; if (rand < hardRatio) //instantiate a hard zombie (50 % a phone zombie) { int isPhone = Random.Range(0, 2); if (isPhone == 1) //create a phone zombie { zombieSpawn = Instantiate(phonePrefab) as GameObject; zombieType = "phone"; phone = zombieSpawn.GetComponent <PhoneZombie>(); } else //create a modern zombie { zombieSpawn = Instantiate(modernPrefab) as GameObject; zombieType = "modern"; modern = zombieSpawn.GetComponent <ModernZombie>(); } } else //create easy zombie (50% prob for each) { int isClassic = Random.Range(0, 2); if (isClassic == 1) //create a classic zombie { zombieSpawn = Instantiate(classicPrefab) as GameObject; zombieType = "classic"; classic = zombieSpawn.GetComponent <ClassicZombie>(); } else //create a shambler { zombieSpawn = Instantiate(shamblerPrefab) as GameObject; zombieType = "shambler"; shambler = zombieSpawn.GetComponent <ShamblerZombie>(); } } int off = Random.Range(0, 3); //the offset, always going IN TO OUT!! //this if chain will determine the lane that the zombie will be spawned in if (off == 0) { lane = "in"; } else if (off == 1) { lane = "mid"; } else if (off == 2) { lane = "out"; } switch (corner) { case "tl": //instantiate in bottom right (since opposite of tl), facing west. dir = "W"; zombieSpawn.transform.Rotate(new Vector3(0, 1, 0), -90); //rotate so zombie is looking right way switch (lane) { case "in": zombieSpawn.transform.position = new Vector3(31.5f, 0, 3.5f); break; case "mid": zombieSpawn.transform.position = new Vector3(32.5f, 0, 2.5f); break; case "out": zombieSpawn.transform.position = new Vector3(33.5f, 0, 1.5f); break; } break; case "bl": //so spawn top right, facing down dir = "S"; zombieSpawn.transform.Rotate(new Vector3(0, 1, 0), 180); //rotate so zombie is looking right way switch (lane) { case "in": zombieSpawn.transform.position = new Vector3(31.5f, 0, 17.5f); break; case "mid": zombieSpawn.transform.position = new Vector3(32.5f, 0, 18.5f); break; case "out": zombieSpawn.transform.position = new Vector3(33.5f, 0, 19.5f); break; } break; case "br": //instantiate in top left (since opposite to br), facing east. //Debug.Log ("br hit!"); dir = "E"; zombieSpawn.transform.Rotate(new Vector3(0, 1, 0), 90); //rotate so zombie is looking right way switch (lane) { case "in": zombieSpawn.transform.position = new Vector3(5.5f, 0, 17.5f); break; case "mid": zombieSpawn.transform.position = new Vector3(4.5f, 0, 18.5f); break; case "out": zombieSpawn.transform.position = new Vector3(3.5f, 0, 19.5f); break; } break; case "tr": //instantiate in bottom left facing up. dir = "N"; //NO ROTATE YAY!! switch (lane) { case "in": zombieSpawn.transform.position = new Vector3(5.5f, 0, 3.5f); break; case "mid": zombieSpawn.transform.position = new Vector3(4.5f, 0, 2.5f); break; case "out": zombieSpawn.transform.position = new Vector3(3.5f, 0, 1.5f); break; } break; } //now set the lane of the zombie. switch (zombieType) { case "classic": switch (lane) { case "in": classic.aLane = ClassicZombie.lane.inside; break; case "mid": classic.aLane = ClassicZombie.lane.middle; break; case "out": classic.aLane = ClassicZombie.lane.outside; break; } if (dir == "N") { classic.dir = ClassicZombie.direction.N; } else if (dir == "E") { classic.dir = ClassicZombie.direction.E; } else if (dir == "S") { classic.dir = ClassicZombie.direction.S; } else if (dir == "W") { classic.dir = ClassicZombie.direction.W; } break; case "shambler": switch (lane) { case "in": shambler.aLane = ShamblerZombie.lane.inside; break; case "mid": shambler.aLane = ShamblerZombie.lane.middle; break; case "out": shambler.aLane = ShamblerZombie.lane.outside; break; } if (dir == "N") { shambler.dir = ShamblerZombie.direction.N; } else if (dir == "E") { shambler.dir = ShamblerZombie.direction.E; } else if (dir == "S") { shambler.dir = ShamblerZombie.direction.S; } else if (dir == "W") { shambler.dir = ShamblerZombie.direction.W; } break; case "modern": switch (lane) { case "in": modern.aLane = ModernZombie.lane.inside; break; case "mid": modern.aLane = ModernZombie.lane.middle; break; case "out": modern.aLane = ModernZombie.lane.outside; break; } if (dir == "N") { modern.dir = ModernZombie.direction.N; } else if (dir == "E") { modern.dir = ModernZombie.direction.E; } else if (dir == "S") { modern.dir = ModernZombie.direction.S; } else if (dir == "W") { modern.dir = ModernZombie.direction.W; } break; case "phone": switch (lane) { case "in": phone.aLane = PhoneZombie.lane.inside; break; case "mid": phone.aLane = PhoneZombie.lane.middle; break; case "out": phone.aLane = PhoneZombie.lane.outside; break; } if (dir == "N") { phone.dir = PhoneZombie.direction.N; } else if (dir == "E") { phone.dir = PhoneZombie.direction.E; } else if (dir == "S") { phone.dir = PhoneZombie.direction.S; } else if (dir == "W") { phone.dir = PhoneZombie.direction.W; } break; } zombieSpawn.transform.parent = GameObject.Find("GeneratedZombies").transform; //put this in a gameobject so its neat detect = Instantiate(detectPrefab) as DetectSurvivor; detect.zombieTransform = zombieSpawn.transform; detect.transform.parent = GameObject.Find("Detectors").transform; }
/* this method will populate the map with n zombies of either shamblers or classics. The zombies will face clockwise*/ void ZombieInitialize(int n) //don't make over 21!! { int isClassic; // this int will be randomly picked inside the loop. If 1, the zombie to init is a classic, other wise is a shambler. //init only on top and bottom. If i%3 = 2, outside, if = 1, mid, if 0, inside. //i/3 will correspond to the column. column will start at x = 6.5. Increment this by 3. //top z = 19.5, mid = 18.5, inner = 17.5 for (int i = 0; i < n; i++) { //determine x, z positions first bool outside = false, mid = false, inside = false; //find lane or z component float x; float z; switch (i % 3) { case 2: outside = true; z = 19.5f; break; case 1: mid = true; z = 18.5f; break; case 0: inside = true; z = 17.5f; break; default: z = -1000; Debug.Log("you ducked up"); break; } //find x component x = 3 * (i / 3) + 6.5f; isClassic = Random.Range(0, 2); if (isClassic == 0) //instatiate shambler { ShamblerZombie s = Instantiate(shamblerPrefab); s.dir = ShamblerZombie.direction.E; //face east; s.gameObject.transform.position = new Vector3(x, 0, z); s.transform.Rotate(new Vector3(0, 1, 0), 90); //rotate so zombie is looking right way if (outside) { s.aLane = ShamblerZombie.lane.outside; } else if (mid) { s.aLane = ShamblerZombie.lane.middle; } else if (inside) { s.aLane = ShamblerZombie.lane.inside; } s.transform.parent = GameObject.Find("GeneratedZombies").transform; DetectSurvivor detect = Instantiate(detectPrefab) as DetectSurvivor; detect.zombieTransform = s.transform; detect.transform.parent = GameObject.Find("Detectors").transform; } else //classic { ClassicZombie classic = Instantiate(classicPrefab); classic.dir = ClassicZombie.direction.E; //face east; classic.gameObject.transform.position = new Vector3(x, 0, z); classic.transform.Rotate(new Vector3(0, 1, 0), 90); //rotate so zombie is looking right way if (outside) { classic.aLane = ClassicZombie.lane.outside; } else if (mid) { classic.aLane = ClassicZombie.lane.middle; } else if (inside) { classic.aLane = ClassicZombie.lane.inside; } classic.transform.parent = GameObject.Find("GeneratedZombies").transform; DetectSurvivor detect = Instantiate(detectPrefab) as DetectSurvivor; detect.zombieTransform = classic.transform; detect.transform.parent = GameObject.Find("Detectors").transform; } } }
void AvoidZombieCollision() { bool hitFrontZombie = false; //these lines of code will shoot the raycast, and then will try to change lanes if raycast hits. RaycastHit front; Ray frontRay = new Ray(transform.position + transform.forward * .5f + transform.up * .5f, transform.forward); Debug.DrawRay(transform.position + transform.forward * .5f + transform.up * .5f, transform.forward, Color.green); //change speeds and change lanes according to the zombie raycast hits. if (Physics.Raycast(frontRay, out front, 2)) { if (front.collider.gameObject.tag == "Zombie") { //this code will check if the zombie is of type shambler. ShamblerZombie shamblerScript = front.collider.gameObject.GetComponent <ShamblerZombie>(); if (shamblerScript != null) { aSpeed = ZombieConstants.v / 2; //change speed to v/2, since shambler has only 1 speed hitFrontZombie = true; RandomlyChangeLanes(); } ModernZombie modernScript = front.collider.gameObject.GetComponent <ModernZombie>(); if (modernScript != null) { aSpeed = modernScript.aSpeed; hitFrontZombie = true; RandomlyChangeLanes(); } ClassicZombie classicScript = front.collider.gameObject.GetComponent <ClassicZombie>(); if (classicScript != null) { aSpeed = classicScript.aSpeed; hitFrontZombie = true; RandomlyChangeLanes(); } } } //if back raycast hits, just try to change lanes (not speed) RaycastHit back; Ray backRay = new Ray(transform.position + -transform.forward * .5f + transform.up * .5f, -transform.forward); //Debug.DrawRay(transform.position + -transform.forward * .5f + transform.up * .5f , -transform.forward, Color.green); if (Physics.Raycast(backRay, out back, 3)) { if (back.collider.gameObject.tag == "Zombie") { //this code will check if the zombie is of type shambler. ShamblerZombie shamblerScript = back.collider.gameObject.GetComponent <ShamblerZombie>(); if (shamblerScript != null) { aSpeed = ZombieConstants.v / 2; //change speed to v/2, since shambler has only 1 speed dir = (direction)System.Enum.Parse(typeof(direction), shamblerScript.dir.ToString()); //transform.rotation = back.transform.rotation; hitFrontZombie = true; RandomlyChangeLanes(); } ModernZombie modernScript = back.collider.gameObject.GetComponent <ModernZombie>(); if (modernScript != null) { aSpeed = modernScript.aSpeed; dir = (direction)System.Enum.Parse(typeof(direction), modernScript.dir.ToString()); //transform.rotation = back.transform.rotation; hitFrontZombie = true; RandomlyChangeLanes(); //Debug.Log ("BADD"); } ClassicZombie classicScript = back.collider.gameObject.GetComponent <ClassicZombie>(); if (classicScript != null) { //Debug.Log ("hit by classic"); aSpeed = classicScript.aSpeed; //dir = (direction) System.Enum.Parse(typeof(direction), classicScript.dir.ToString()); //transform.rotation = front.transform.rotation; hitFrontZombie = true; RandomlyChangeLanes(); } PhoneZombie phoneScript = back.collider.gameObject.GetComponent <PhoneZombie>(); if (phoneScript != null) { //Debug.Log ("BOOM"); aSpeed = phoneScript.aSpeed; //dir = (direction) System.Enum.Parse(typeof(direction), phoneScript.dir.ToString()); //transform.rotation = back.transform.rotation; hitFrontZombie = true; RandomlyChangeLanes(); } } } }