- Home /
Don'tDetroyOnLoad | issue
Hi, I have a player who die when he hit something and persist while reloading the level : using System.Collections; using System.Collections.Generic; using UnityEngine;
public class killer : MonoBehaviour {
public Rigidbody2D collider2;
public player script;
public SpriteRenderer spriteRenderer;
public Sprite sprite;
public GameObject GO;
public bool IsDead;// if already dead
// Use this for initialization
void Start () {
}
void Awake()
{
GameObject[] objs = GameObject.FindGameObjectsWithTag("Player");
DontDestroyOnLoad(this.gameObject);
}
private void OnTriggerEnter2D(Collider2D collision)
{
script.enabled = false;
spriteRenderer.sprite = sprite;
collider2.constraints = RigidbodyConstraints2D.None;
IsDead = true;
Application.LoadLevelAsync(Application.loadedLevelName);
}
}
And a camera who follow him on the x axis
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class cameraplane : MonoBehaviour {
public Transform transformcam;
public Transform transformplane;
public float smooth;
public float offset;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// the following mechanic
transformcam.position = new Vector3(Mathf.Lerp(transformcam.position.x, transformplane.position.x + offset, Time.deltaTime * smooth), 4.5f ,-10);
}
}
I want the camera to follow ONLY the one who isn't killed. Please HEEEEEEEEEEEEEEEEEEEEEEEEEEEEEELP
Answer by ray2yar · Dec 28, 2018 at 02:11 PM
How many cameras are in the scene? Just one? Is it following just one player or both? Your camera script is written to follow whatever transformplane is... so, change it to the player you want it to follow.
I suggest reworking your scripts so that you don't have all players being flagged as "don't destroy on load"... instead maybe have a gamemanager persist between scenes that holds player data and spawns the players if they aren't dead. However, if you must have players persist, don't destroy them by script. Instead have them turn off their certain components (like colliders). Then when a new scene loads add a listener and have it check if the player died in the previous scene.
Your answer
Follow this Question
Related Questions
Camera follow player with code 3 Answers
How can I get a Capsule to move and rotate in the direction the cam is facing? 1 Answer
How to get camera to follow player 2d 11 Answers
Issues with player prefabs when I try to teleport it 1 Answer
How can I keep a player inside the camera box? [C#] 3 Answers