- Home /
random respawn and respawn delay
so I have this health script that is attached to each player which when the player's health is depleted the player instantly respawns to assigned spawnpoint. what I want to do is temporarily disable to player when he/she is killed, wait 3 seconds then spawn at random spawnpoint and re enable at the same time, also I have set it to add 100 health back to the health float variable when respawned and I want it to set the float to 100 instead of adding to it.
how would I go adding these things into my script?
#pragma strict
var PointCube : Transform;
var spawneffect : Transform;
var deatheffect : Transform;
var SpawnPoint : Transform;
var respawn : boolean = false;
var health:float = 90;
var healthText : UI.Text;
function Damage(dmg:float){
health -= dmg;
}
function Update (){
if(healthText)healthText.text = health.ToString();
if(health <=0){
//temporarily disable player
Instantiate (deatheffect,transform.position,transform.rotation);
Instantiate (PointCube,transform.position,transform.rotation);
//wait 3 seconds
//change this to respawn randomly
transform.position = SpawnPoint.position;
//enable player
Instantiate (spawneffect,transform.position,transform.rotation);
var audio: AudioSource = GetComponent.<AudioSource>();
audio.Play();
audio.Play(44100);
//change this to set float not add to it
health += 100;
}
}
Answer by KittenSnipes · Jan 16, 2018 at 09:19 AM
I am not absolutely fluent in the UnityScript but here is my best shot:
#pragma strict
var PointCube: Transform;
var spawneffect: Transform;
var deatheffect: Transform;
var SpawnPoints: Transform[];
private var SpawnPoint: Transform;
//Respawn timer for the coroutine
var respawnTime: float = 3;
var respawn: boolean = false;
//This is the players current health
private var health: float;
//The max health the player is spawned with
var maxHealth: float = 90;
var healthText: UI.Text;
//At start set the players health to maxHealth
function Start() {
health = maxHealth;
}
function Damage(dmg: float) {
health -= dmg;
}
function Update() {
if (healthText) {
healthText.text = health.ToString();
// If the players health is zero start the respawn function
if (health <= 0) {
StartCoroutine(Respawn());
}
}
}
//This is a coroutine. Basically meaning it has a special wait timer function
function Respawn() {
//temporarily disable player
Instantiate(deatheffect, transform.position, transform.rotation);
Instantiate(PointCube, transform.position, transform.rotation);
//Wait for the respawnTimer. (This is the special wait timer I was speaking of)
yield WaitForSeconds(respawnTime);
//Find a random spawn point
var spawnPointID: float = Random.Range(0, SpawnPoints.length);
//Set the player at that random spawn point
transform.position = SpawnPoints[spawnPointID].position;
//enable player
Instantiate(spawneffect, transform.position, transform.rotation);
var audio: AudioSource = GetComponent.<AudioSource>();
audio.Play();
audio.Play(44100);
//change this to set float not add to it
health = maxHealth;
}
@$$anonymous$$ittenSnipes this looks about right I'll give it a go and see if it works :)
aah I see must of mixed it up with C# or somethin.
I'll give it a try again and hopefully it works :P
@$$anonymous$$ittenSnipes
looks like it worked, I'm getting an error (which I've had for a while) but it's nothing drastic and it doesn't stop me from running the game.
cheers :P