- Home /
The question is answered, right answer was accepted
My Death() method is working on collusion but fails to get deathMenu when i fall.
Good evening Unity folks. It is my first post here and I started studying Unity a month ago, therefore if this question seems stupid to you i apologize for it beforehand.
I have basically 3 scripts working and i followed N3K's Endless Runner tutorial on YouTube. The first script is PlayerMotor and calls the OnDeath method of PlayerScore script when our controller hits an obstacle. OnDeath method of PlayerScore script calls the ToggleEndMenu method of DeathMenu script which sets active the DeathMenu gameobject with the score we have on death.
PlayerMotor script:
void Update()
{
if (transform.position.y < -1)
{
Death();
}
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.point.z > transform.position.z + 0.1f && hit.gameObject.tag == "Enemy")
{
Death();
}
}
private void Death()
{
isDead = true;
GetComponent<PlayerScore>().OnDeath();
}
PlayerScore script:
public void OnDeath()
{
isDead = true;
if (PlayerPrefs.GetFloat("Highscore") < score)
PlayerPrefs.SetFloat("Highscore", score);
deathMenu.ToggleEndMenu(score);
}
DeathMenu script:
public void ToggleEndMenu(float score)
{
gameObject.SetActive(true);
scoreText.text = ((int)score).ToString();
isShown = true;
}
So my problem is when i hit an obstacle Death() method works perfectly fine and my death menu pops up with my score immediately. However when my character falls from a bridge or something my Death() method in Update class stops the score counter on screen but doesnt set active DeathMenu gameobject so my death menu doesnt pop on my screen. I'm really confused why it doesnt work. Can someone help me with this?
Note: I deleted unrelated lines of codes from my scripts.
Answer by dARkStorN- · Jan 26, 2019 at 07:56 PM
I added a box collider under my all re-instantiated prefabs and added this line of code to my PlayerMotor and it works like a charm. Thanks @Ymrasu for the hint!
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.point.z > transform.position.z + 0.1f && hit.gameObject.tag == "Enemy")
{
Death();
}
if (hit.point.y < transform.position.y + 0.5f && hit.gameObject.tag == "Enemy")
{
Death();
}
}
Answer by Ymrasu · Jan 26, 2019 at 06:59 PM
Instead of checking if the player is beyond a certain y point, I would put a long box collider under your world set up similar to the other obstacles that are working. That way if the player falls, they would hit the obstacle under your world and call your OnDeath that way.
I thought the same thing but because it is an endless runner shouldn't my box collider has an infinite scale to achieve the result in every frame possible. What if i destroy the colliders on my past and re-instantiate it under the tiles that i am about to run. Would it be more efficient in terms of memory usage?