Нужна помощь с менеджером соб
Добавлено: 03 сен 2023, 19:02
Мне нужно создать скрипт который случайным образом выбирает объект из массива и запускает у него функцию, в данном случае это функция плавного движения объекта вверх. Проблема в том, что когда функция запускается через скрипт менеджера, движение объекта вверх не происходит, он просто телепортируется.
Скрипт менеджера:
Скрипт объекта:
Скрипт менеджера:
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnomalyManager : MonoBehaviour
{
public FurnitureInteract[] furniture;
void Start()
{
StartCoroutine(Interact());
}
IEnumerator Interact()
{
Debug.Log("1");
int waitUntil = Random.Range(20, 60);
int rand = Random.Range(1, 10);
Debug.Log(rand);
yield return new WaitForSeconds(waitUntil);
furniture[rand].anomaly();
Debug.Log(furniture[rand]);
int waitAfter = Random.Range(5, 15);
yield return new WaitForSeconds(waitAfter);
furniture[rand].anomalyBack();
}
}
using System.Collections.Generic;
using UnityEngine;
public class AnomalyManager : MonoBehaviour
{
public FurnitureInteract[] furniture;
void Start()
{
StartCoroutine(Interact());
}
IEnumerator Interact()
{
Debug.Log("1");
int waitUntil = Random.Range(20, 60);
int rand = Random.Range(1, 10);
Debug.Log(rand);
yield return new WaitForSeconds(waitUntil);
furniture[rand].anomaly();
Debug.Log(furniture[rand]);
int waitAfter = Random.Range(5, 15);
yield return new WaitForSeconds(waitAfter);
furniture[rand].anomalyBack();
}
}
Скрипт объекта:
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FurnitureInteract : MonoBehaviour
{
private Vector3 fromPosition;
private Vector3 toPosition;
public float speed;
public float posY;
private float progress;
private void Start()
{
fromPosition = transform.position;
toPosition = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y + posY, gameObject.transform.position.z);
}
public void anomaly()
{
progress += Time.deltaTime * speed;
transform.position = Vector3.Lerp(fromPosition, toPosition, progress);
}
public void anomalyBack()
{
progress += Time.deltaTime * speed;
transform.position = Vector3.Lerp(toPosition, fromPosition, progress);
}
}
using System.Collections.Generic;
using UnityEngine;
public class FurnitureInteract : MonoBehaviour
{
private Vector3 fromPosition;
private Vector3 toPosition;
public float speed;
public float posY;
private float progress;
private void Start()
{
fromPosition = transform.position;
toPosition = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y + posY, gameObject.transform.position.z);
}
public void anomaly()
{
progress += Time.deltaTime * speed;
transform.position = Vector3.Lerp(fromPosition, toPosition, progress);
}
public void anomalyBack()
{
progress += Time.deltaTime * speed;
transform.position = Vector3.Lerp(toPosition, fromPosition, progress);
}
}