bool tntFall = true;
void Awake()
{
tntFallTime = Time.deltaTime / 0.05f;
}
void OnCollisionEnter(Collision coll)
{
if (CurrentScene != "ColorGame" && CurrentScene != "PaintingGame" && coll.gameObject.tag != "Player" && CurrentScene == "TNT Run" && tntFall)
{
Debug.Log(coll.gameObject.GetComponent<MeshRenderer>().material.name);
if (coll.gameObject.GetComponent<MeshRenderer>().material.name == "TNT_0 (Instance)")
{
coll.gameObject.GetComponent<MeshRenderer>().material = TNT_Mat1;
StartCoroutine(coroutine);
}
else if (coll.gameObject.GetComponent<MeshRenderer>().material.name == "TNT_1 (Instance)")
{
coll.gameObject.GetComponent<MeshRenderer>().material = TNT_Mat2;
StartCoroutine(coroutine);
}
else if (coll.gameObject.GetComponent<MeshRenderer>().material.name == "TNT_2 (Instance)")
{
coll.gameObject.transform.position = Vector3.Lerp(coll.gameObject.transform.position, new Vector3(coll.gameObject.transform.position.x,-10,coll.gameObject.transform.position.z), tntFallTime);
Destroy(coll.gameObject, 3);
}
}
}
IEnumerator TNTFallDelay(int WaitTime)
{
tntFall = false;
yield return new WaitForSeconds(WaitTime);
tntFall = true;
}
This my code from Player script. If you look at my as you can see it's actually cubes holding together. All i want is;
- When player touches one of the cubes For the first time it will change material
- When player touches that cube Second time it will change material again
- When player touches that cube Third time cube is going to fall down.
But in these case (with my code) when i touch one of the cubes it does all of the if-else statements in a second and cube falls down.
transform.position
or usingtransform.Translate
, you can expectOnCollisionEnter
to be called every frame, because the physics engine is forced to treat the object like it's teleported and re-process all collisions from scratch. \$\endgroup\$