في أي لعبة دائما ما نحتاج إلي توليد العديد من الأجسام داخلها ونقدم لكم في هذا المقال سكربت "SpawnerScript"وهو يقوم بعملية توليد للأجسام التي تحددها له في المتغير العام " yourPrefab" في المكان الواقع المحدد بواسطة :
" float addXPos = Random.Range(-1.6f, 1.6f);"
using UnityEngine;
using System.Collections;
public class SpawnerScript : MonoBehaviour {
public Transform yourPrefab;
private float nextyourPrefabTime = 0.0f;
private float spawnRate = 1.5f;
void Update () {
if (nextyourPrefabTime < Time.time)
{
SpawnyourPrefab();
nextyourPrefabTime = Time.time + spawnRate;
//Speed up the spawnrate for the next yourPrefab
spawnRate *= 0.98f;
spawnRate = Mathf.Clamp(spawnRate, 0.3f, 99f);
}
}
void SpawnyourPrefab()
{
float addXPos = Random.Range(-1.6f, 1.6f);
Vector3 spawnPos = transform.position + new Vector3(addXPos,0,0);
Instantiate(yourPrefab, spawnPos, Quaternion.identity);
}
}
using System.Collections;
public class SpawnerScript : MonoBehaviour {
public Transform yourPrefab;
private float nextyourPrefabTime = 0.0f;
private float spawnRate = 1.5f;
void Update () {
if (nextyourPrefabTime < Time.time)
{
SpawnyourPrefab();
nextyourPrefabTime = Time.time + spawnRate;
//Speed up the spawnrate for the next yourPrefab
spawnRate *= 0.98f;
spawnRate = Mathf.Clamp(spawnRate, 0.3f, 99f);
}
}
void SpawnyourPrefab()
{
float addXPos = Random.Range(-1.6f, 1.6f);
Vector3 spawnPos = transform.position + new Vector3(addXPos,0,0);
Instantiate(yourPrefab, spawnPos, Quaternion.identity);
}
}

0 تعليقات