Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by 93brandon · May 22, 2015 at 02:29 AM · c#collisiontriggerspawnfalling

Respawn from falling off world!

If you fall of the side of the world whats the script you need for you to teleport back to spawn point?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by abhi_360 · May 22, 2015 at 05:05 AM

  public Transform spawnPoint;//Add empty gameobject as spawnPoint
  public float minHeightForDeath;
  public GameObject player; //Add your player
  
  void Update()
 {
   if(player.transform.position.y < minHeightForDeath)
       player.transform.position = spawnPoint.position;
 }
 
 //There are many ways to do this. This is just one way
 //Try not to use Physics if you don't need it(there is a way to 
 //do this by using Physics)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pangamini · May 22, 2015 at 08:29 AM 0
Share

I suggest you reset the rigidbody velocity as well :-P

avatar image
0

Answer by jka4kcha · May 22, 2015 at 09:15 AM

make a new c# script and name it respawn, copy and paste this >>

 using UnityEngine;
 using System.Collections;
 
 public class respawn : MonoBehaviour {
     public float threshold;
 
     void FixedUpdate () {
         if (transform.position.y < threshold)
             transform.position = new Vector3(0, 0, 0);
     }
 }


then drag that script onto your character..the primary parent object. THEN go to the inspector and set the threshold to something below the floor....on a standard setup a number like -20 or -50 will work great.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image STYliz · Feb 21, 2018 at 05:45 AM 0
Share

I had to edit this code to make it work, how did I make it work? I changed the coordinates of the new Vector3.

Change the 0, 0, 0 into your coordinates where your player will respawn at. Anyway, this code helped a lot.

avatar image
0

Answer by chintan_shroff · May 22, 2015 at 09:20 AM

 float fallZone = -10f;
 public Transform playerSpawnPoint = null; //create an empty gameobject and assign it to this script  in the inspector   

 void Update(){

 if(player.transform.position.y < fallZone) //Assuming its a 2D game
 {
  player.transform.position = playerSpawnPoint.position;
 }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by anurag7991 · Jul 27, 2018 at 06:17 PM

suppose the platform size is 300 on the x axis , y axis and anything on z axis

// EndGame() is a function defined by me to end game on falling out of the platform stored in a script called GameManager

//

public Rigidbody player;

public float lastposition;

public GameObject gameover;

bool gameHasEnded = false;

public void Resume()

{

if (player.position.y < 0.1f) // player dies if falls below 0.1 on y axis

{

lastposition = player.position.z;

FindObjectOfType ().EndGame(); .

transform.position = new Vector3 (0, y ,lastposition);

}}

public void EndGame ()

{

if (gameHasEnded == false)

{

gameHasEnded =true;

respawn ();

gameover.SetActive(true);

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Sazails · Jul 27, 2018 at 06:24 PM

// THIS SCRIPT HAS TO BE ATTACHED TO THE PLAYER By default it will set the offset to be -50 on the y from the starting position

 using UnityEngine;
 public class PlayerRespawn : MonoBehaviour {

 Vector3 _InitialPos; // Starting position of the player
 float _YOffset;

 void Start () {
     _InitialPos = transform.position;
     _YOffset = _InitialPos.y - 50;
 }
 
 void Update () {
     if (_InitialPos.y < _YOffset)
     {
         transform.position = _InitialPos;
     }
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnTriggerEnter2D/OnCollisionEnter2D - delay 1 Answer

My spikes don't damage the player 2 Answers

Character Fails To Jump Sometimes 1 Answer

Do triggers not fire if spawned colliding? 1 Answer

Collision triggers do not work on child gameobject 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges