• 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
0
Question by largow · Apr 13, 2014 at 01:43 AM · 2dinstantiaterespawnfallingkill

Respawning player not working

Hello Guys, I'm making a 2D game , and I am having some trouble making my player respawn when he falls. I have a script attached to my player, that makes basically 3 things: Kill the player when he falls off a platform and touches an invisible Collider2D that detects the player, create a new one on the spawning point and then move the camera towards the new player.

But I can't get that going. Whenever my player dies, the camera moves towards the spawning point and I can see that a Character(Clone) is created, but I can't see him on the Game window (Only the camera moving if i move my player) and he appears visible on the Scene Window as well...

Here's the code:

 public class Respawn : MonoBehaviour {
 
     public GameObject character; //a prefab of my player will be attached to the script
     public Transform spawnPoint; // an empty gameObject will act as the spawnPoint
 
     void OnTriggerEnter2D(Collider2D coll){
         Destroy (coll.gameObject);
         if (character) {
             GameObject p = Instantiate (character, spawnPoint.position, Quaternion.identity) as GameObject;
             CamController cam = Camera.main.gameObject.GetComponent<CamController> (); //The script that mo
             cam.target = p.transform;            
         }
 
 
     }
 }

Hope you can help me!

Comment
Add comment · Show 2
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 getyour411 · Apr 13, 2014 at 01:47 AM 0
Share

FYI - you probably have other scripts that have Start/Awake GetComponent or drag/drop references to the player, but when you Destroy and then Respawn those are probably flaking out

avatar image glaucomorais · Apr 13, 2014 at 03:03 AM 0
Share

Why destroy player instance? Disable it, move it to the respawn and re-enable it. Unless you are making permanence for the corpse...

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by largow · Apr 14, 2014 at 03:16 AM

Fixed it! It was something related to the camera! Dont know why, just changed the camera projection I was using. Remember, this is 2D, never use perspective, you don't want depth and stuff like that. Use ORTOGRAPHIC. Also made a simpler script of my camera, I attach it if you want it:

Code:

using UnityEngine;

using System.Collections;

 public class CamaraController : MonoBehaviour {
  
     public Transform target;
     public float height = 3f;
     public float distance = 3f;
     public float damping = 5f;  
 
     // Update is called once per frame
 
     void Update () {
         Vector3 pos =  target.TransformPoint(0,height,-distance);
         transform.position = Vector3.Lerp (transform.position, pos, Time.deltaTime * damping);  
     }
 
 }
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 glaucomorais · Apr 13, 2014 at 03:15 AM

Destroy (coll.gameObject); at line 7.

Probably you're destroying the player when it is instantiated. Tag it and make sure to not destroy player when it collides with the gameobject that this script is attached.

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mouse position not returning actual position 0 Answers

How to instantiate and destroy objects with 2d trigger 1 Answer

Random instantiate at same frame with each instantiate having unique random direction 1 Answer

The prefab you want to instantiate is null. 2 Answers

Photon Gun Pickup Not Showing On All Screens 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges