public override void Attack() { if (targetPlayer.GetComponent <MonoBehaviour>() is IDamageable) { GameObject g = ObjectsPool.Spawn(bulletPrefab, Vector3.zero, bulletPrefab.transform.rotation); Rocket r = g.GetComponent <MonoBehaviour>() as Rocket; r.OwnerScript = this; r.gameObject.transform.localScale = new Vector3(1f, 1f, 1f); g.name = "RangedRocket"; g.transform.position = new Vector3(transform.position.x, transform.position.y + 0.8f, transform.position.z); g.transform.rotation = Quaternion.LookRotation(transform.forward); r.Damage = MeleeAttackDamage; if (Mathf.Abs(targetPlayer.position.x - transform.position.x) <= minimumRange && Mathf.Abs(targetPlayer.position.z - transform.position.z) <= minimumRange) { Vector3 direction = (gameObject.transform.forward * minimumRange); Vector3 targetPos = gameObject.transform.position + direction; r.Shoot(new Vector3(targetPos.x, targetPos.y - 1f, targetPos.z)); } else { r.Shoot(new Vector3(targetPlayer.position.x, transform.position.y - 1f, targetPlayer.position.z)); } if (anim != null) { anim.SetTrigger("Attack"); } } }
/// <summary> /// Detects if a player bullet entered. /// </summary> /// <param name="other"></param> protected void OnTriggerEnter(Collider other) { if (other.tag == "Bullet") { if (other != null) { // Size tween transform.localScale = originalScale * 0.3f; LeanTween.scale(gameObject, originalScale, 0.5f).setEase(AnimCurveContainer.AnimCurve.pingPong); Vector3 forward = other.transform.forward; Vector3 position = other.transform.position; Vector3 reflectedVector = Vector3.Reflect(forward, transform.parent.transform.forward); // Create bullet GameObject g = ObjectsPool.Spawn(enemyBullet, Vector3.zero, enemyBullet.transform.rotation); Bullet b = g.GetComponent <MonoBehaviour>() as Bullet; b.OwnerScript = this; g.name = "RangedBullet"; g.transform.position = new Vector3(position.x, 0.6f, position.z); g.transform.rotation = Quaternion.LookRotation(reflectedVector); b.Damage = b.Damage; b.Shoot(reflectedVector, b.BulletSpeed); } } }
public void Death() { GameMaster.CountKill(); transform.localScale = Vector3.one; ObjectsPool.Spawn(GameMaster.enemyFX, GameMaster.idPool_enemyDeathFX, transform.position, transform.rotation).GetComponent <AudioSource>().Play(); ObjectsPool.Despawn(GameMaster.idPool_enemies, gameObject); }
IEnumerator drawLine() { yield return(new WaitForSeconds(0.01f)); //Draws the line if (thisTransform.position.x > (lastx + 0.02f)) { if (!GameController.isGameOver) { skipLine -= 1; } ObjectsPool.Spawn(Line, thisTransform.position, Quaternion.identity); lastx = thisTransform.position.x; } //If we not drawn enough lines, keep drawing. Else, pause for a bit then start drawing again if (skipLine > 0) { StartCoroutine(drawLine()); } else { yield return(new WaitForSeconds(0.45f)); StartCoroutine(drawLine()); skipLine = Random.Range(150, 250); } }
/// <summary> /// The Shoot mechanic of the machine gun. /// </summary> public override void Use() { if (shootIsAllowed) { base.Use(); GameObject prefab = ChooseRandomPrefab(); GameObject g = ObjectsPool.Spawn(prefab, Vector3.zero, prefab.transform.rotation); Bullet bullet; if (g != null && g.GetComponent <MonoBehaviour>() is Bullet) { bullet = g.GetComponent <MonoBehaviour>() as Bullet; bullet.OwnerScript = this.OwnerScript; bullet.name = "TetrisBullet"; bullet.Damage = this.WeaponDamage; bullet.transform.position = transform.position; bullet.transform.rotation = Quaternion.LookRotation(transform.forward); //bullet.transform.Rotate(Vector3.forward, Random.Range(0, 360f)); float speed = bullet.BulletSpeed; // Random rotation Quaternion rotation = Quaternion.Euler(0, Random.Range(-weaponAccuracy, weaponAccuracy), 0); Vector3 v = transform.forward; Vector3 rotationVector = rotation * v; bullet.Shoot(rotationVector, speed); bulletRounds.Emit(1); } shootIsAllowed = false; StartCoroutine(WaitForNextShot()); } }
public virtual void ExplodePartial(int start) { for (int submesh = 0; submesh < M.subMeshCount; submesh++) { int[] indices = M.GetTriangles(submesh); for (int i = start; i < indices.Length - grandStep - 2; i += grandStep) { for (int n = 0; n < 3; n++) { int index = indices[i + n]; newVerts[n] = verts[index]; newUvs[n] = uvs[index]; newNormals[n] = normals[index]; } mesh = new Mesh(); mesh.vertices = newVerts; mesh.normals = newNormals; mesh.uv = newUvs; mesh.triangles = triangles; GO = ObjectsPool.Spawn(pooledObjectName, Vector3.zero, Quaternion.identity); if (GO != null) { GO.SetActive(true); Deactivator deactivator = GO.GetComponent <Deactivator>(); GO.layer = LayerMask.NameToLayer(layerName); deactivator.attachedRenderer.material = MR.materials[submesh]; deactivator.attachedFilter.mesh = mesh; GO.transform.position = transform.position; GO.transform.rotation = transform.rotation; GO.transform.localScale = new Vector3(MR.transform.localScale.x * scaleFactor, MR.transform.localScale.y, MR.transform.localScale.z * scaleFactor); if (changeForwardVector) { GO.transform.forward = transform.up; } GO.AddComponent <BoxCollider>(); deactivator.attachedRigid.AddExplosionForce(explosionForce, explosionOrigin, 50, upwardsModifier); deactivator.attachedRigid.useGravity = useGravity; deactivator.attachedRigid.drag = drag; deactivator.attachedRigid.angularDrag = angularDrag; deactivator.TriggerDeactivation(Random.Range(minimumAliveTime, maximumAliveTime)); } } } Destroy(this); }
void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "Enemy") { ObjectsPool.Spawn(GameMaster.bulletFX, GameMaster.idPool_bulletFX, transform.position, transform.rotation).GetComponent <AudioSource>().Play(); collision.GetComponent <Enemy>().Death(); DisableObj(); } }
IEnumerator Shoot() { while (shoot) { GameObject bullet = ObjectsPool.Spawn(bulletPrefab, GameMaster.idPool_bullet, transform.position, transform.rotation, transform); bullet.transform.localPosition = new Vector2(bullet.transform.localPosition.x, bullet.transform.localPosition.y + 0.8f); bullet.GetComponent <Bullet>().speed = speed; shootSong.Play(); yield return(new WaitForSeconds(fireRate)); } yield return(null); }
private IEnumerator RandomSpawning() { while (true) { for (int i = 0; i < prefabs.Length; i++) { Vector3 randomPos = Random.insideUnitSphere * 10; ObjectsPool.Spawn(prefabs[i], randomPos, Quaternion.identity); yield return(new WaitForSeconds(0.1f)); } yield return(new WaitForSeconds(0.2f)); } }
public override void Use() { if (shootIsAllowed) { base.Use(); //GameObject g = Instantiate(bulletPrefab) as GameObject; GameObject g = ObjectsPool.Spawn(bulletPrefab, Vector3.zero, bulletPrefab.transform.rotation); Bullet bullet; Transform spawn; if (g != null && g.GetComponent <MonoBehaviour>() is Bullet) { bullet = g.GetComponent <MonoBehaviour>() as Bullet; bullet.OwnerScript = this.OwnerScript; bullet.name = "PistolBullet"; bullet.Damage = this.WeaponDamage; // Alternate the spawnpoint of the dual pistol if (alternateSpawn) { spawn = bulletSpawns[1]; bulletRounds.Emit(1); } else { spawn = bulletSpawns[2]; rightBulletRounds.Emit(1); } alternateSpawn = !alternateSpawn; bullet.transform.position = spawn.position; bullet.transform.rotation = Quaternion.LookRotation(spawn.forward); float speed = bullet.BulletSpeed; // Random rotation Quaternion rotation = Quaternion.Euler(0, Random.Range(-weaponAccuracy, weaponAccuracy), 0); Vector3 v = spawn.forward; Vector3 rotationVector = rotation * v; bullet.Shoot(rotationVector, speed); } shootIsAllowed = false; StartCoroutine(WaitForNextShot()); } }
/// <summary> /// The Shoot mechanic of the shotgun. /// </summary> public override void Use() { if (shootIsAllowed) { base.Use(); // The angle between 2 bullets. float angleBetween = spreadAngle / numberOfBullets; //The start angle of the calculation. float currentAngle = -(spreadAngle / 2f); // Instantiate the bullets for (int i = 0; i < numberOfBullets; i++) { GameObject g = ObjectsPool.Spawn(bulletPrefab, Vector3.zero, bulletPrefab.transform.rotation); //GameObject g = Instantiate(bulletPrefab) as GameObject; Bullet bullet; if (g != null && g.GetComponent <MonoBehaviour>() is Bullet) { bullet = g.GetComponent <MonoBehaviour>() as Bullet; bullet.OwnerScript = this.OwnerScript; bullet.name = "ShotgunBullet"; bullet.Damage = this.WeaponDamage; bullet.transform.position = transform.position; bullet.transform.rotation = Quaternion.LookRotation(transform.forward); float speed = bullet.BulletSpeed; // Calculate the bullet direction Quaternion rotation = Quaternion.Euler(0, Random.Range(currentAngle - weaponAccuracy, currentAngle + weaponAccuracy), 0); Vector3 v = transform.forward; Vector3 rotationVector = rotation * v; // Shoot bullet bullet.Shoot(rotationVector, speed); currentAngle += angleBetween; } } bulletRounds.Emit(1); shootIsAllowed = false; StartCoroutine(WaitForNextShot()); } }
void Spawn() { //If the camera object has gone far enough start spawning different weights randomly if (CameraObj.position.x > 22) { int randm = Random.Range(0, 2); if (randm == 0) { ObjectsPool.Spawn(Weight, thisTransform.position, Quaternion.identity); } if (randm == 1) { ObjectsPool.Spawn(WeightHeavy, thisTransform.position, Quaternion.identity); } } //Invoke spawn at random time interval between min and max Invoke("Spawn", Random.Range(spawnMin, spawnMax)); }
/// <summary> /// Creates a bullet and spawns it. /// </summary> /// <param name="e">Reference to the boss enemy.</param> private void CreateBullet(BossEnemy e) { // Angle between two bullets float angleBetween = e.SpreadAngle / e.NumberOfBullets; // Start angle of the calculation float currentAngle = -(e.SpreadAngle / 2f); for (int i = 0; i < e.NumberOfBullets; i++) { //GameObject g = GameObject.Instantiate(e.RangedBullet); GameObject g = ObjectsPool.Spawn(e.RangedBullet, Vector3.zero, e.RangedBullet.transform.rotation); BossBullet bullet; if (g != null && g.GetComponent <MonoBehaviour>() is BossBullet) { bullet = g.GetComponent <BossBullet>(); bullet.OwnerScript = e; bullet.name = "BossBullet"; bullet.Damage = bullet.Damage; bullet.transform.position = e.transform.position + new Vector3(0, 1, 0); Vector3 playerDirection = (e.TargetPlayer.position - bullet.transform.position).normalized; // Eliminate y playerDirection = new Vector3(playerDirection.x, 0f, playerDirection.z); bullet.transform.rotation = Quaternion.LookRotation(playerDirection); bullet.transform.Rotate(90, 0, 0); // Bullet rotation Quaternion rotation = Quaternion.Euler(0, currentAngle, 0); Vector3 v = playerDirection; Vector3 rotationVector = rotation * v; // Shoot bullet.Shoot(rotationVector, bullet.BulletSpeed); currentAngle += angleBetween; } } }
IEnumerator Spawning() { while (availableSpawn) { int side = Random.Range(0, 2) == 0 ? -1 : 1; int axis = Random.Range(0, 2); if (axis == 0) { float width = Random.Range(-screenSize.x, screenSize.x); positionEnemy = new Vector2(width, screenSize.y * side); } else { float height = Random.Range(-screenSize.y, screenSize.y); positionEnemy = new Vector2(screenSize.x * side, height); } positionEnemy += (Vector2)GameMaster.Player.transform.position + margin * side; ObjectsPool.Spawn(enemyPrefab, GameMaster.idPool_enemies, positionEnemy, transform.rotation, transform); yield return(new WaitForSeconds(spawnEnemyRate)); } yield return(null); }
/// <summary> /// Spawns the bullet at a specific spawn point. /// </summary> /// <param name="spawnPoint">Spawn point of the meteor.</param> protected void SpawnBullet(Vector3 spawnPoint) { if (meteorBullet != null) { //GameObject g = GameObject.Instantiate(meteorBullet); GameObject g = ObjectsPool.Spawn(meteorBullet, Vector3.zero, meteorBullet.transform.rotation); MeteorBullet bullet; if (g.GetComponent <MonoBehaviour>() is MeteorBullet) { bullet = g.GetComponent <MeteorBullet>(); bullet.OwnerScript = ownerScript; bullet.name = "MeteorBullet"; bullet.transform.position = spawnPoint; Vector3 direction = Vector3.down + destinationOffset; bullet.transform.rotation = Quaternion.LookRotation(direction.normalized); bullet.Shoot(direction, bullet.BulletSpeed); } } }
public override void Attack() { if (targetPlayer.GetComponent <MonoBehaviour>() is IDamageable) { //GameObject g = (GameObject) Instantiate(bulletPrefab); GameObject g = ObjectsPool.Spawn(bulletPrefab, Vector3.zero, bulletPrefab.transform.rotation); Bullet b = g.GetComponent <MonoBehaviour>() as Bullet; b.OwnerScript = this; g.name = "RangedBullet"; g.transform.position = new Vector3(transform.position.x, 0.6f, transform.position.z); g.transform.rotation = Quaternion.LookRotation(transform.forward); //Debug.Log("Attack - " + g.name + " " + g.transform.position); b.Damage = MeleeAttackDamage; b.Shoot(transform.forward, b.BulletSpeed); if (anim != null) { anim.SetTrigger("Attack"); } } }
public static void Spawn(this GameObject prefabToSpawn, Vector3 pos, Quaternion rot) { ObjectsPool.Spawn(prefabToSpawn, pos, rot); }
private void ExplodePartial(int start) { for (int submesh = 0; submesh < M.subMeshCount; submesh++) { int[] indices = M.GetTriangles(submesh); GetTriangles(indices); for (int i = triList.Count - 1; i >= 0; i -= grandStep) { Tri tri = triList[i]; Vector3 direction1 = Vector3.Normalize(-normals[tri.x]) * extrudeFactor; Vector3 direction2; int[] triangles = new int[0]; mesh = new Mesh(); if (GetMatchingTri(tri.x, tri.y, tri.z)) { newVerts = new Vector3[8]; newNormals = new Vector3[8]; newUvs = new Vector2[8]; direction2 = Vector3.Normalize(-normals[matchedIndex]) * extrudeFactor; newVerts[0] = verts[tri.x]; newUvs[0] = uvs[tri.x]; newNormals[0] = normals[tri.x]; newVerts[1] = verts[tri.y]; newUvs[1] = uvs[tri.y]; newNormals[1] = normals[tri.y]; newVerts[2] = verts[tri.z]; newUvs[2] = uvs[tri.z]; newNormals[2] = normals[tri.z]; newVerts[3] = verts[matchedIndex]; newUvs[3] = uvs[matchedIndex]; newNormals[3] = normals[matchedIndex]; newVerts[4] = newVerts[0] + direction1; newUvs[4] = uvs[tri.x]; newNormals[4] = newNormals[0]; newVerts[5] = newVerts[1] + direction1; newUvs[5] = uvs[tri.y]; newNormals[5] = newNormals[1]; newVerts[6] = newVerts[2] + direction1; newUvs[6] = uvs[tri.z]; newNormals[6] = newNormals[2]; newVerts[7] = newVerts[3] + direction2; newUvs[7] = uvs[matchedIndex]; newNormals[7] = newNormals[3]; triangles = new int[] { 0, 1, 2, 3, 1, 0, /*up tris*/ 6, 4, 2, 0, 2, 4, /*side1 */ 5, 2, 1, 2, 5, 6, /*side2*/ 0, 4, 7, 7, 3, 0, /*side3*/ 7, 5, 1, 7, 1, 3, /*side4*/ 6, 5, 4, 4, 5, 7 /*down*/ }; } else { newVerts = new Vector3[6]; newNormals = new Vector3[6]; newUvs = new Vector2[6]; newVerts[0] = verts[tri.x]; newUvs[0] = uvs[tri.x]; newNormals[0] = normals[tri.x]; newVerts[1] = verts[tri.y]; newUvs[1] = uvs[tri.y]; newNormals[1] = normals[tri.y]; newVerts[2] = verts[tri.z]; newUvs[2] = uvs[tri.z]; newNormals[2] = normals[tri.z]; newVerts[3] = newVerts[0] + direction1; newUvs[3] = uvs[tri.x]; newNormals[3] = newNormals[0]; newVerts[4] = newVerts[1] + direction1; newUvs[4] = uvs[tri.y]; newNormals[4] = newNormals[1]; newVerts[5] = newVerts[2] + direction1; newUvs[5] = uvs[tri.z]; newNormals[5] = newNormals[2]; triangles = new int[] { 0, 1, 2, /*up*/ 4, 1, 0, 0, 3, 4, /*side1*/ 0, 2, 5, 1, 4, 5, /*side2*/ 5, 4, 3, 5, 3, 0, /*side3*/ 5, 2, 1 /*down*/ }; } mesh.vertices = newVerts; mesh.normals = newNormals; mesh.uv = newUvs; mesh.triangles = triangles; //GO = pool.getPooledObject(); GO = ObjectsPool.Spawn(pooledObjectName, Vector3.zero, Quaternion.identity); if (GO != null) { GO.SetActive(true); Deactivator deactivator = GO.GetComponent <Deactivator>(); GO.layer = LayerMask.NameToLayer("Fragments"); GO.transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z); GO.transform.rotation = transform.rotation; GO.transform.localScale = new Vector3(scaleFactor.x, scaleFactor.y, scaleFactor.z); deactivator.attachedRenderer.material = MR.materials[submesh]; deactivator.attachedFilter.mesh = mesh; deactivators.Add(deactivator); GO.AddComponent <BoxCollider>(); } } } // Spawn PowerUp SpawnPowerUp(); MR.enabled = false; GetComponent <Rigidbody>().isKinematic = true; GetComponent <Collider>().enabled = false; if (GetComponent <NavMeshObstacle>() != null) { GetComponent <NavMeshObstacle>().enabled = false; } if (respawn) { Invoke("TriggerRespawn", timeTillRespawn); } }
private void SplitInTwo() { int above = 0; int below = 0; Vector3[] vertices = M.vertices; for (int submesh = 0; submesh < M.subMeshCount; submesh++) { int[] subMeshIndices = M.GetIndices(submesh); for (int i = 0; i < subMeshIndices.Length; i += 3) { above = 0; below = 0; if (vertices[subMeshIndices[i]].y <= cutHeight) { below++; } else { above++; } if (vertices[subMeshIndices[i + 1]].y <= cutHeight) { below++; } else { above++; } if (vertices[subMeshIndices[i + 2]].y <= cutHeight) { below++; } else { above++; } if (above == 3 || below == 3) { if (above == 3) { upIndices[submesh].Add(subMeshIndices[i]); upIndices[submesh].Add(subMeshIndices[i + 1]); upIndices[submesh].Add(subMeshIndices[i + 2]); upIndices[upIndices.Length - 1].Add(subMeshIndices[i + 2]); upIndices[upIndices.Length - 1].Add(subMeshIndices[i + 1]); upIndices[upIndices.Length - 1].Add(subMeshIndices[i]); } else { lowIndices[submesh].Add(subMeshIndices[i]); lowIndices[submesh].Add(subMeshIndices[i + 1]); lowIndices[submesh].Add(subMeshIndices[i + 2]); lowIndices[lowIndices.Length - 1].Add(subMeshIndices[i + 2]); lowIndices[lowIndices.Length - 1].Add(subMeshIndices[i + 1]); lowIndices[lowIndices.Length - 1].Add(subMeshIndices[i]); } } } } DetermineVertexPositions(vertices); if (upperVertices != 0) { upper = ObjectsPool.Spawn(pooledObjectName, Vector3.zero, Quaternion.identity); upper.layer = LayerMask.NameToLayer("Fragments"); upper.transform.position = transform.position; upper.transform.rotation = SMR.transform.rotation; upper.transform.localScale = transform.localScale; upper.SetActive(true); upper.name = "upper"; Mesh mesh = new Mesh(); mesh.SetVertices(upperVertexList); mesh.subMeshCount = upIndices.Length; for (int i = 0; i < upIndices.Length; i++) { mesh.SetIndices(ApplyIndexChange(upIndices[i], vertexPosChange), MeshTopology.Triangles, i); } mesh.SetNormals(upNormals); mesh.SetUVs(0, upperUVs); Deactivator deactivator = upper.GetComponent <Deactivator>(); deactivator.TriggerDeactivation(deactivateTime); Destroy(upper.GetComponent <MeshFilter>()); Destroy(upper.GetComponent <MeshRenderer>()); SkinnedMeshRenderer upSMR = upper.AddComponent <SkinnedMeshRenderer>(); upSMR.sharedMesh = mesh; upSMR.sharedMaterials = upMaterials; upSMR.sharedMesh.RecalculateBounds(); upSMR.rootBone = upper.transform; BoxCollider box = upper.AddComponent <BoxCollider>(); box.center = upSMR.sharedMesh.bounds.center; box.size = upSMR.sharedMesh.bounds.size; deactivator.attachedRigid.AddForceAtPosition(upper.transform.forward * 0.5f, upper.transform.position, ForceMode.Impulse); } if (lowerVertices != 0) { lower = ObjectsPool.Spawn(pooledObjectName, Vector3.zero, Quaternion.identity); lower.layer = LayerMask.NameToLayer("Fragments"); lower.transform.position = transform.position; lower.transform.rotation = SMR.transform.rotation; lower.transform.localScale = transform.localScale; lower.SetActive(true); lower.name = "lower"; Mesh meshLow = new Mesh(); meshLow.SetVertices(lowerVertexList); meshLow.subMeshCount = lowIndices.Length; for (int i = 0; i < lowIndices.Length; i++) { meshLow.SetIndices(ApplyIndexChange(lowIndices[i], vertexPosChange), MeshTopology.Triangles, i); } meshLow.SetNormals(lowNormals); meshLow.SetUVs(0, lowUVs); Deactivator deactivatorLow = lower.GetComponent <Deactivator>(); deactivatorLow.TriggerDeactivation(deactivateTime); Destroy(lower.GetComponent <MeshFilter>()); Destroy(lower.GetComponent <MeshRenderer>()); SkinnedMeshRenderer lowSMR = lower.AddComponent <SkinnedMeshRenderer>(); lowSMR.sharedMaterials = lowMaterials; lowSMR.sharedMesh = meshLow; lowSMR.sharedMesh.RecalculateBounds(); BoxCollider boxLow = lower.AddComponent <BoxCollider>(); boxLow.center = lowSMR.sharedMesh.bounds.center; boxLow.size = lowSMR.sharedMesh.bounds.size * 0.95f; deactivatorLow.attachedRigid.AddForceAtPosition(lower.transform.forward * 0.5f, lower.transform.position, ForceMode.Impulse); } }