Как пофиксить баг с постоянным респавном объектов в Enviro Spawn
Странный баг из за которого объекты при запуске игры начинают постоянно телепортироваться (респавниться) видео
using EnvSpawn;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ResourceHealth : MonoBehaviour
{
public int startHealth;
public int health;
public float destroyTime = 5f;
private Transform resourceSpawer;
public ItemScriptableObject resourceType;
[SerializeField] private string spawnerName = "TreeSpawner";
public void Start()
{
resourceSpawer = GameObject.Find(spawnerName).transform;
resourceSpawer.GetComponent<EnviroSpawn_CS>().Generate();
health = startHealth;
}
public void TreeFall()
{
gameObject.AddComponent<Rigidbody>();
Rigidbody rig = GetComponent<Rigidbody>();
rig.isKinematic = false;
rig.useGravity = true;
rig.mass = 200;
rig.constraints = RigidbodyConstraints.FreezeRotationY;
RespawnResource();
Destroy(gameObject, destroyTime);
}
public void StoneGathered()
{
Destroy(gameObject);
}
public void RespawnResource()
{
float randomX = Random.Range(resourceSpawer.position.x -
resourceSpawer.GetComponent<EnviroSpawn_CS>().dimensions.x / 2,
resourceSpawer.position.x + resourceSpawer.GetComponent<EnviroSpawn_CS>().dimensions.x / 2);
float randomY = Random.Range(resourceSpawer.position.z -
resourceSpawer.GetComponent<EnviroSpawn_CS>().dimensions.y / 2, resourceSpawer.position.z + resourceSpawer.GetComponent<EnviroSpawn_CS>().dimensions.y / 2);
Vector3 rayPos = new Vector3(randomX, 100, randomY);
RaycastHit hit;
if (Physics.SphereCast(rayPos, 6, Vector3.down, out hit, 200))
{
if (hit.collider.gameObject.layer == 10)
{
GameObject newTree = Instantiate(gameObject, hit.point, Quaternion.identity);
Destroy(newTree.GetComponent<Rigidbody>());
newTree.transform.rotation = Quaternion.identity;
}
else
{
RespawnResource();
}
}
}
}
и 2 код
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GatherResources : MonoBehaviour
{
public Camera mainCamera;
public LayerMask layerMask;
public InventoryManager inventoryManager;
public ItemScriptableObject resource;
public int resourceAmount;
public GameObject hitFX;
public void GatherResource()
{
RaycastHit hit;
Ray ray = mainCamera.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
if (Physics.Raycast(ray, out hit, 1.5f, layerMask))
{
if(resource.name == hit.collider.GetComponent<ResourceHealth>().resourceType.name)
{
if (hit.collider.GetComponent<ResourceHealth>().health >= 1)
{
Instantiate(hitFX, hit.point, Quaternion.LookRotation(hit.normal));
inventoryManager.AddItem(resource, resourceAmount);
hit.collider.GetComponent<ResourceHealth>().health--;
if (hit.collider.GetComponent<ResourceHealth>().health <= 0 && hit.collider.gameObject.layer == 9)
{
hit.collider.GetComponent<ResourceHealth>().TreeFall();
hit.collider.GetComponent<Rigidbody>().AddForce(mainCamera.transform.forward * 10, ForceMode.Impulse);
}
if (hit.collider.GetComponent<ResourceHealth>().health <= 0 && hit.collider.gameObject.layer == 11)
{
hit.collider.GetComponent<ResourceHealth>().StoneGathered();
}
}
}
}
}
}
Ответы (1 шт):
Автор решения: Алексей Войтенко
→ Ссылка
Нашел решение — просто удалить строчку:
resourceSpawer.GetComponent<EnviroSpawn_CS>().Generate();
которая находится в void Start