Gusic
1
Hi guys,
I have made a Player Die script for my 2d game so when the player hits the object(Spikes) he dies but i am trying to make it so when the player dies he respawns a couple of seconds later in coordinates (0.04833326, 3.980667, 0), here is my script i cant seem to get it to work.
Thank You
using UnityEngine;
using System.Collections;
public class PlayerDie : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D(Collision2D collision){
if (collision.gameObject.name == "Spikes") {
Destroy(gameObject);
}
}
//player respawn
if (gameObject == Destroy){
current.transformatio.position = new vector3(0.04833326, 3.980667, 0);
}
}
When you call ‘Destory()’ on line 18, this script and the game object it was attached to ceases to exist. As an alternate to destroying the game object, consider hiding it, moving it and then showing it again.
using UnityEngine;
using System.Collections;
public class PlayerDie : MonoBehaviour {
void OnCollisionEnter2D(Collision2D collision){
if (collision.gameObject.name == "Spikes") {
StartCoroutine(DieAndRespawn());
}
}
IEnumerator DieAndRespawn() {
renderer.enabled = false;
yield return new WaitForSeconds(2.0f);
transform.position = new Vector3(0.04833326f, 3.980667f, 0.0f);
transform.rotation = Quaternion.identity;
renderer.enabled = true;
}
}
In order to “respawn” this way there may be additional things you have to do. You may have to turn colliders off and back on, or reset the Ribidbody.velocity to 0.0.
try this
using UnityEngine;
using System.Collections;
public class playermovements : MonoBehaviour {
public float speed;
private Rigidbody rb;
private Vector3 spown;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
spown = transform.position;
}
// Update is called once per frame
void FixedUpdate () {
speed = 25;
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3 (moveHorizontal,0, moveVertical);
rb.AddForce (movement * speed);
}
void OnCollisionEnter (Collision other)
{
if(other.transform.tag == "enimy")
{
transform.position = spown;
}
}
}