public Material []materialSequence;
new public Renderer renderer;
float playerExitTime = float.NegativeInfinity;
int state = 0;
bool PlayerStays = false;
void Awake()
{
renderer = gameObject.GetComponent<MeshRenderer>();
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Player"))
{
// Add a little tolerance, so rapid vibration at the edge of collision
// doesn't advance the state. We need to leave for some time then return.
if (Time.time - playerExitTime > 0.1f)
AdvanceState();
}
}
void OnCollisionStay(Collision collision)
{
if (PlayerStays)
{
StartCoroutine(FallThenDestroy(10f5f, 3f9f));
PlayerStays = false;
}
if (state == 1)
{
StartCoroutine(PlayerCheckStayStatus(1.5f));
}
}
void OnCollisionExit(Collision collision)
{
if (collision.gameObject.CompareTag("Player"))
{
playerExitTime = Time.time;
}
}
void AdvanceState()
{
// If we're already falling, ignore further state changes.
if (state == materialSequence.Length)
return;
renderer.sharedMaterial = materialSequence[state];
state++;
if (state == materialSequence.Length)
StartCoroutine(FallThenDestroy(10f, 3f));
}
// This lets us animate out the fall before the destruction takes place.
IEnumerator FallThenDestroy(float distance, float duration)
{
PlayerStays = false;
yield return new WaitForSeconds(0.5f);
for (float t = 0; t < duration; t += Time.deltaTime)
{
// Fall some distance.
transform.Translate(0, -distance * Time.deltaTime / duration, 0, Space.World);
// Yield control to the engine to run for a frame, then resume here.
yield return null;
}
// Destroy ourselves once the fall is over.
Destroy(gameObject);
}
IEnumerator PlayerCheckStayStatus(float time)
{
yield return new WaitForSeconds(time);
PlayerStays = true;
Debug.Log(PlayerStays);
}
Edited code like this, it works nice but i need to ask when i stay on one of the cubes after 1.5f seconds itcube falls but It falls very fast, i think duration from the FallThenDestroy
multipliesmy character teleports to nearest cube around instead of falling with
if (state == materialSequence.Length) StartCoroutine(FallThenDestroy(10f, 3f));
Because that if statement becames true too it. How can i fix it?
- How to fix that problem?
- When cube falls down, character teleports to the nearest cube instead of falling down with it. Need to fix it too :/