- Home /
Player Death Screen
I want to make a script for my 2D game that can detect when a player collides with a certain object (a projectile in my case) and then transports him to a death screen that has the same options as the main menu (play, quit etc) but it has the player's score that was achieved during the game. I've already written a script for counting the score and it's as follows:
public class Score : MonoBehaviour
{
float playerScore = 0;
public Text scoreText;
void Update() {
playerScore += Time.deltaTime*15;
scoreText.text = playerScore.ToString("0");
}
}
public class Projectile : MonoBehaviour{
public float speed;
private Transform player;
private Vector2 target;
private Vector3 motion;
private float destroyTime = 4;
void Start(){
player = GameObject.FindGameObjectWithTag("Player").transform;
target = new Vector2(player.position.x, player.position.y);
Vector3 sourceToDestination = player.position - transform.position;
motion = sourceToDestination.normalized * speed;
}
void Update(){
transform.position += motion * Time.deltaTime;
Destroy(gameObject, destroyTime);
}
}
Also this is the script for the projectile
Answer by VoidPhoenix96 · Feb 24, 2021 at 03:07 AM
From what I can tell you want to make a death screen, correct? Well, you should have some graphics ready, the simplest solution is just having a black UI sprite (Name it whatever you want but i suggest Death Screen). Make an animation for said black UI Sprite (Just call it "FadeInAnimation") Use keyframes to lerp its opacity from 0-1. Then, in the Death Screen's animator, just make sure the fade in animation is connected to the Entry node. In your player script, make a reference to the death screen like this : public GameObject deathScreen;
make a function that is called when the player dies. In that function write: deathScreen.SetActive(true);
and in the Start()
function write deathScreen.SetActive(false);
Then, when you want to restart the game, just play the Fade in animation backwards.
This should make a working death screen.
Your answer
![](https://koobas.hobune.stream/wayback/20220613025707im_/https://answers.unity.com/themes/thub/images/avi.jpg)