- Home /
Question by
Chocolade · Jun 22, 2020 at 01:59 AM ·
c#timeeffecttime.deltatimeteleporting
Why when changing object position it's taking few seconds until changing the position ?
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Teleporting : MonoBehaviour
{
public List<GameObject> teleporters = new List<GameObject>();
public float fadeDuration;
public float dissolveAmount;
[Range(-3, 3)]
public float range;
public Vector3 direction;
private List<Material> materials = new List<Material>();
private bool alreadyFading = false;
// Start is called before the first frame update
void Start()
{
foreach (Transform _transform in transform)
{
if (_transform.GetComponent<Renderer>() != null &&
_transform.GetComponent<Renderer>().material.shader != null &&
_transform.GetComponent<Renderer>().material.shader.name == "Custom/Teleport")
{
materials.Add(_transform.GetComponent<Renderer>().material);
}
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Teleporter")
{
StartCoroutine(WaitBeforeGo(3));
}
}
IEnumerator WaitBeforeGo(int TimeToWait)
{
yield return new WaitForSeconds(TimeToWait);
if (!alreadyFading) StartCoroutine(Teleport(-3, 3, 5f, materials));
}
IEnumerator Teleport(float from, float to, float duration, List<Material> materials)
{
alreadyFading = true;
for (int i = 0; i < materials.Count; i++)
{
var timePassed = 0f;
while (timePassed < duration)
{
timePassed += Time.deltaTime;
var factor = timePassed / duration;
var value = Mathf.Lerp(from, to, factor);
materials[i].SetFloat("_DissolveAmount", value);
yield return null;
}
materials[i].SetFloat("_DissolveAmount", to);
}
transform.position = teleporters[1].transform.position;
alreadyFading = false;
}
}
The problem is that it's getting to the line :
transform.position = teleporters[1].transform.position;
but only after 3-5 seconds after the Teleporter Coroutine has finished. and the change position should be part of the teleporting. so first in the while loop it's making the teleporting effect and then change the object position to the next teleporter but when the Coroutine finished it's taking more 3-5 seconds to the object to change position.
I can't figure out why it's not changing position at the time the Coroutine has finished ?
Comment
Your answer
Follow this Question
Related Questions
Time.DeltaTime doesn't seem to be correctly 1 Answer
Having trouble increasing the spawning speed of explosions 0 Answers
How to make this display milliseconds? 3 Answers
Multiple Cars not working 1 Answer