- Home /
GameOver Scene
Okay so my problem is i dont know how could i make when my charachter comes to 0 hp it says Game Over. I have made new scene about gameover but i dont know what and where should i type in this script to load scene, Can u help me without telling me to read pdf files etc?
THIS IS MY SCRIPT
public var speed : float = 5.0;
var isOffScreen : boolean = false; public var offscreenDotRange : float = 0.7;
var isVisible : boolean = false; public var visibleDotRange : float = 0.8; // ** between 0.75 and 0.85 (originally 0.8172719)
var isInRange : boolean = false;
public var followDistance : float = 24.0; public var maxVisibleDistance : float = 25.0;
public var reduceDistAmt : float = 3.1;
private var sqrDist : float = 0.0;
public var health : float = 100.0; public var damage : float = 20.0;
public var enemySightedSFX : AudioClip;
private var hasPlayedSeenSound : boolean = false;
private var colDist : float = 5.0; // raycast distance in front of enemy when checking for obstacles
function Start() { if ( thePlayer == null ) { thePlayer = GameObject.Find( "Player" ).transform; }
theEnemy = transform;
}
function Update() { // Movement : check if out-of-view, then move CheckIfOffScreen();
// if is Off Screen, move
if ( isOffScreen )
{
MoveEnemy();
// restore health
RestoreHealth();
}
else
{
// check if Player is seen
CheckIfVisible();
if ( isVisible )
{
// deduct health
DeductHealth();
// stop moving
StopEnemy();
// play sound only when the Man is first sighted
if ( !hasPlayedSeenSound )
{
audio.PlayClipAtPoint( enemySightedSFX, thePlayer.position );
}
hasPlayedSeenSound = true; // sound has now played
}
else
{
// check max range
CheckMaxVisibleRange();
// if far away then move, else stop
if ( !isInRange )
{
MoveEnemy();
}
else
{
StopEnemy();
}
// reset hasPlayedSeenSound for next time isVisible first occurs
hasPlayedSeenSound = false;
}
}
}
function DeductHealth() { // deduct health health -= damage * Time.deltaTime;
// check if no health left
if ( health <= 0.0 )
{
health = 0.0;
Debug.Log( "YOU ARE OUT OF HEALTH !" );
// Restart game here!
// Application.LoadLevel( "GameOver" );
}
}
function RestoreHealth() { // deduct health health += damage * Time.deltaTime;
// check if no health left
if ( health >= 100.0 )
{
health = 100.0;
//Debug.Log( "HEALTH is FULL" );
}
}
function CheckIfOffScreen() { var fwd : Vector3 = thePlayer.forward.normalized; var other : Vector3 = (theEnemy.position - thePlayer.position).normalized;
var theProduct : float = Vector3.Dot( fwd, other );
if ( theProduct < offscreenDotRange )
{
isOffScreen = true;
}
else
{
isOffScreen = false;
}
}
function MoveEnemy() { // Check the Follow Distance CheckDistance();
// if not too close, move
if ( !isInRange )
{
rigidbody.velocity = Vector3( 0, rigidbody.velocity.y, 0 ); // maintain gravity
// --
// Old Movement
//transform.LookAt( thePlayer );
//transform.position += transform.forward * speed * Time.deltaTime;
// --
// New Movement - with obstacle avoidance
var dir : Vector3 = ( thePlayer.position - theEnemy.position ).normalized;
var hit : RaycastHit;
if ( Physics.Raycast( theEnemy.position, theEnemy.forward, hit, colDist ) )
{
//Debug.Log( " obstacle ray hit " + hit.collider.gameObject.name );
if ( hit.collider.gameObject.name != "Player" && hit.collider.gameObject.name != "Terrain" )
{
dir += hit.normal * 20;
}
}
var rot : Quaternion = Quaternion.LookRotation( dir );
theEnemy.rotation = Quaternion.Slerp( theEnemy.rotation, rot, Time.deltaTime );
theEnemy.position += theEnemy.forward * speed * Time.deltaTime;
// --
}
else
{
StopEnemy();
}
}
function StopEnemy() { transform.LookAt( thePlayer );
rigidbody.velocity = Vector3.zero;
}
function CheckIfVisible() { var fwd : Vector3 = thePlayer.forward.normalized; var other : Vector3 = ( theEnemy.position - thePlayer.position ).normalized;
var theProduct : float = Vector3.Dot( fwd, other );
if ( theProduct > visibleDotRange )
{
// Check the Max Distance
CheckMaxVisibleRange();
if ( isInRange )
{
// Linecast to check for occlusion
var hit : RaycastHit;
if ( Physics.Linecast( theEnemy.position + (Vector3.up * 1.75) + theEnemy.forward, thePlayer.position, hit ) )
{
Debug.Log( "Enemy sees " + hit.collider.gameObject.name );
if ( hit.collider.gameObject.name == "Player" )
{
isVisible = true;
}
}
}
else
{
isVisible = false;
}
}
else
{
isVisible = false;
}
}
function CheckDistance() { var sqrDist : float = (theEnemy.position - thePlayer.position).sqrMagnitude; var sqrFollowDist : float = followDistance * followDistance;
if ( sqrDist < sqrFollowDist )
{
isInRange = true;
}
else
{
isInRange = false;
}
}
function ReduceDistance() { followDistance -= reduceDistAmt; }
function CheckMaxVisibleRange() { var sqrDist : float = (theEnemy.position - thePlayer.position).sqrMagnitude; var sqrMaxDist : float = maxVisibleDistance * maxVisibleDistance;
if ( sqrDist < sqrMaxDist )
{
isInRange = true;
}
else
{
isInRange = false;
}
}
function OnGUI() { GUI.Box( Rect( (Screen.width * 0.5) - 60, Screen.height - 35, 120, 25 ), "Health : " + parseInt( health ).ToString() ); }
press CTRL+SHIFT+B and see GameOver scene added or not..
Answer by AlucardJay · Jan 06, 2013 at 05:41 PM
If you actually read my script, what you want is in the comments of the function DeductHealth() :
// Restart game here!
// Application.LoadLevel( "sceneLose" );
I have stated numerous times that all questions regarding this guide should be asked on the pages that the guide is posted :
http://answers.unity3d.com/questions/296068/how-to-make-a-slender-man-follow-character-script.html
http://answers.unity3d.com/questions/321749/how-do-you-pick-up-pages-like-in-slender.html
I also bet you never upvoted my answer where you found this :/
Why do people not understand this? I answered the same question as this here : http://answers.unity3d.com/questions/375084/how-to-make-it-die-and-make-it-permanenttrue.html
Look at the CollectPapers script, in the function DeductHealth() :
function DeductHealth()
{
// deduct health
health -= damage * Time.deltaTime;
// check if no health left
if ( health <= 0.0 )
{
health = 0.0;
Debug.Log( "YOU ARE OUT OF HEALTH !" );
// Restart game here!
// Application.LoadLevel( "sceneLose" );
}
}
SO, where it says Debug.Log( "YOU ARE OUT OF HEALTH !" ); , here is where you put what to happen when health is 0.
I have left the commented line :
// Application.LoadLevel( "sceneLose" );
If you make a scene called sceneLose, then uncomment this line (remove the // ), then when health reaches 0 the scene will load. You can do anything you like here, the point is this is the part of the script you need to change for what you want to do when health reaches 0.
Just to say I have now removed the packages for my guide. Reasons : no thanks except from 2 people total and 5 votes over 2 answers; when specifically stating don't ask new questions but post problems on those questions, there have been at least 20 questions all using my scripts, all by people that don't understand and just want their game written for them;
I am extremely disappointed. and sorry for the few people that actually read the guide, absorbed the script and learned from it, then expanded my guide into their own projects rather than copy-paste then saying this doesn't work . Hope you feel lucky to be the last one with the packages. I bet I get more comments asking me to put them back up than comments saying thanks.
Your answer
Follow this Question
Related Questions
how can i get GUI.Button presse(or down not click) 3 Answers
GUI draw rectangle script not drawing rectangle! 1 Answer
Intro GUI Text Script... 3 Answers
GUI Resolution Ajust 1 Answer
Load an image from www and save it for offline use 0 Answers