Whats a good way to kill player if they go below a certain area?
I need some advice or some help pointing me in the right direction on how to create a script that would kill off the player if they fell off the course and falling down below a certain point.
So for instance my course is scaled 50 tall on the Y Axis and say I want to kill off the player if they reach below 25 or something like that.
Any advice on how to do that?
I'm still looking around trying to figure this out better though I'm still trying to get the hang of scripting.
Answer by Natecurt · Nov 29, 2015 at 02:44 AM
Assuming that on the players death you would like to simply restart the game you can do:
function death() { Application.LoadLevel("SceneNameHere"); }
This is essentially the same as hitting the play button and will start the game from the scene's start. If not what are you looking to happen upon death?
Also, in c# you could try:
public GameObject player;
public Transform destination;
void Update () {
{
if(player.transform.position.y < 25) {
this.player.transform.position = destination.position;
}
}
}
Then you would need to select a game object as the destination and set the FirstPersonCharacter as the player. Also, attach the script to the FirstPersonController. $$anonymous$$aker sure the box collider on the gameobject is disabled as well.
Thanks for the help, you rock! I will go ahead and give that a try.
Hmm...I think I messed something up in it...though I feel your answer is still the best because I feel strongly that it has gotten the closest to what I am trying to achieve. Ty.
I thought it was working at first but even after disabling the collider I am now noticing that the Player which is a ball that I have a camera controller attached to is getting sent to the destination point before falling to below 25 Y. So with that happening I can't even move the player anymore at all now because it's getting already sent to destination point for some odd reason.
I'm currently trying to figure out why this is happening...but I think I'm doing something wrong somewhere.
$$anonymous$$y friend said doing it that way would keep the script running and only create lag since it would continously run? Any idea if that that is true? I'm not sure.
So as an alternative I kept your suggested script for the time until I can figure out how to fix it. But, I've detached it and made a simple secondary script that is set to destroy the player object if it hits a non-rendered plane that I placed 25 Y. The script is attached to the plane.
void OnTriggerEnter(Collider other) {
Destroy(other.gameObject);
}
However, I found some other threads saying it's better to disable the object and recall it ins$$anonymous$$d of destroying it. But, opposingly I also found some other threads spawning new objects that were previously destroyed.
$$anonymous$$y Start & Restart Point are positioned at 0x, 0y, 0z
I was looking at both to try to understand it more. So I am attempting to figure out how to fix this and reset the object either that way or another way with your provided script which seems to make more sense though I'm very confused at this point still.
Answer by $$anonymous$$ · Dec 03, 2015 at 06:30 PM
First thing first.
1: Take off the camera controller script. 2: Take off the main camera from the player Object. ' Second thing to do (and the easiest)
1: create another camera and remove its audio listener. 2: Attach that second camera naming it Player Camera and make it a child of the player ball 3: Position the Player Camera in the angle or view you prefer,
Third thing; Barrier
Different people have different preferences for different styles of games so that is why you are getting confused. Don't worry about "how they want it." You need to figure out how things will work for you on the back-end of things.
If you use a barrier as "a point of no return" to kill the player if they fall into that abyss, then you can do something very simple, you just make a flat place, remove the visible aspect of the mesh, name it and tag it barrier, and slap on it something like the script from the Space Shooter game tutorial as;
using UnityEngine; using System.Collections;
public class Barrier : MonoBehaviour { void OnTriggerExit(Collider other) { Destroy(other.gameObject); } }
All I did here was change the file name as Barrier rather than "DestroyByBoundary." It does not really matter and in reality the only thing having it listed in player health or destruction or whatever is so that this specific file is found.
Instead of making a whole single box (which also can cause problems like sucking players outside of it or into it soon as you turn the game on), just create a cube, make it flat and transparent and set it however high or low you want it. If you want side barriers, just do the same thing by creating barrier walls.
As far as options, it is again up to you. You can set it up to kill someone if they hit those barriers, or simply restart them back at the starting location, or a combination of the two if you are doing a setup like "how many lives" one has to play before the game is completely over.
If you want the camera to still act as in a following type manner, use the secondary one and reserve the main camera for UI.
If you can pull off a really disturbing feature, when the player hits the barrier from the fall and you set up a scene to be replayed, or you just want to be funny and add something odd instead, you can also have it where if the player hits said barrier to play cut scene before restarting at designated start point.
Thank you for the detailed example and help. I'll try doing it that way and see how it works out.
Yeah, I'm sure i'll need the luck, thanks again. I owe ya one.