
部队战争和球球大作战
为VR游戏中的部队agar.io战争系统构想,所有脚本都在完整描述中
查看大图orb
using UnityEngine;
public class Orb : MonoBehaviour
{
public int orbValue = 1;
public float rotateSpeed = 100f;
private void Update()
{
transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime);
}
private void OnTriggerEnter(Collider other)
{
OrbCollector collector = other.GetComponent<OrbCollector>();
if (collector != null)
{
collector.CollectOrb(orbValue);
Destroy(gameObject);
}
}
}
结束
orb collector
using UnityEngine;
public class OrbCollector : MonoBehaviour
{
[Header("Collector")]
public string collectorName;
[Header("Orb Storage")]
public int orbs = 0;
[Header("Troop Spawning")]
public GameObject troopPrefab;
public Transform troopSpawnPoint;
public int orbsPerTroop = 5;
public int troopsSpawned = 0;
public void CollectOrb(int amount)
{
orbs += amount;
Debug.Log(
collectorName +
" has " +
orbs +
" orbs");
CheckSpawn();
}
void CheckSpawn()
{
while(orbs >= orbsPerTroop)
{
SpawnTroop();
orbs -= orbsPerTroop;
}
}
void SpawnTroop()
{
if(troopPrefab == null)
{
Debug.LogWarning(
"No troop prefab assigned");
return;
}
Vector3 spawnPosition;
if(troopSpawnPoint != null)
{
spawnPosition =
troopSpawnPoint.position;
}
else
{
spawnPosition =
transform.position +
transform.forward * 3;
}
GameObject troop =
Instantiate(
troopPrefab,
spawnPosition,
Quaternion.identity);
TroopAI ai =
troop.GetComponent<TroopAI>();
if(ai != null)
{
ai.owner =
transform;
}
troopsSpawned++;
Debug.Log(
"Spawned troop " +
troopsSpawned);
}
}
结束
orb spawn
using UnityEngine;
public class OrbSpawner : MonoBehaviour
{
public GameObject orbPrefab;
public Terrain terrain;
public int amount = 200;
public float heightOffset = 1f;
void Start()
{
SpawnAllOrbs();
}
void SpawnAllOrbs()
{
if(terrain == null)
{
Debug.LogError("No terrain assigned");
return;
}
TerrainData data = terrain.terrainData;
Vector3 terrainPosition =
terrain.transform.position;
for(int i = 0; i < amount; i++)
{
float x =
Random.Range(
0,
data.size.x);
float z =
Random.Range(
0,
data.size.z);
float y =
data.GetHeight(
(int)x,
(int)z);
Vector3 position =
new Vector3(
x,
y + heightOffset,
z)
+ terrainPosition;
Instantiate(
orbPrefab,
position,
Quaternion.identity);
}
}
}
结束
enemy
using UnityEngine;
using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
{
public NavMeshAgent agent;
[Header("Orb Searching")]
public float searchRange = 100f;
[Header("Orb Collection")]
public float collectDistance = 2f;
private Transform targetOrb;
private OrbCollector collector;
void Start()
{
agent = GetComponent<NavMeshAgent>();
collector = GetComponent<OrbCollector>();
if(collector == null)
{
Debug.LogWarning(
"Enemy needs an OrbCollector component!");
}
InvokeRepeating(
"FindOrb",
0,
2);
}
void Update()
{
if(targetOrb != null)
{
agent.SetDestination(
targetOrb.position);
float distance =
Vector3.Distance(
transform.position,
targetOrb.position);
if(distance <= collectDistance)
{
CollectOrb();
}
}
}
void FindOrb()
{
GameObject[] orbs =
GameObject.FindGameObjectsWithTag("Orb");
float closest =
Mathf.Infinity;
GameObject best = null;
foreach(GameObject orb in orbs)
{
float distance =
Vector3.Distance(
transform.position,
orb.transform.position);
if(distance < closest &&
distance <= searchRange)
{
closest = distance;
best = orb;
}
}
if(best != null)
{
targetOrb =
best.transform;
}
}
void CollectOrb()
{
if(collector != null)
{
collector.CollectOrb(1);
}
Destroy(
targetOrb.gameObject);
targetOrb = null;
}
}
结束
enemy spawn
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
public GameObject enemyPrefab;
public Terrain terrain;
public int enemyAmount = 50;
void Start()
{
SpawnEnemies();
}
void SpawnEnemies()
{
TerrainData data =
terrain.terrainData;
Vector3 terrainPos =
terrain.transform.position;
for(int i = 0; i < enemyAmount; i++)
{
float x =
Random.Range(
0,
data.size.x);
float z =
Random.Range(
0,
data.size.z);
float y =
data.GetHeight(
(int)x,
(int)z);
Vector3 spawn =
new Vector3(
x,
y,
z)
+ terrainPos;
Instantiate(
enemyPrefab,
spawn,
Quaternion.identity);
}
}
}
结束
troop ai
using UnityEngine;
using UnityEngine.AI;
public class TroopAI : MonoBehaviour
{
[Header("Owner")]
public Transform owner;
[Header("Movement")]
public NavMeshAgent agent;
public float followDistance = 5f;
public float updateRate = 0.5f;
[Header("Combat")]
public float health = 100;
public float damage = 10;
private float timer;
void Start()
{
agent =
GetComponent<NavMeshAgent>();
}
void Update()
{
if(owner == null)
{
return;
}
timer += Time.deltaTime;
if(timer >= updateRate)
{
FollowOwner();
timer = 0;
}
}
void FollowOwner()
{
float distance =
Vector3.Distance(
transform.position,
owner.position);
if(distance > followDistance)
{
agent.SetDestination(
owner.position);
}
else
{
agent.ResetPath();
}
}
public void TakeDamage(float amount)
{
health -= amount;
if(health <= 0)
{
Destroy(gameObject);
}
}
}
结束
还没有人评论,去客户端里说两句吧。
评论在新手盒子客户端中发表,这里同步展示。