- Home /
When character falls out of camera view end or reset game
i am making a similar version of doodle jump and want to know how would i go about making the game end when the character falls out of the camera fov,
Answer by RustyCrow · Jan 02, 2018 at 04:02 PM
Well i am not so sure if it is pure math. Check this out MonoBehaviour.OnBecameInvisible()
Leaving some code here if you(or interested people) want to try this method.
// Scripct needs to be attached to the gameobject with the rendrer(I assume you are using sprite rendrer)
public GameObject PlayerGameObject;
void OnBecameInvisible()
{
Destroy(PlayerGameObject);
}
another thing to note this method also counts the editor cam so use maximize on play to get the full effect, now i also think you should add some delay maybe time based or distance like mentioned before.
If you still want to calculate the "when i leave the cam view " i would search for calculating view(assuming orthographic) and not the actual position of the cam, makes it more dynamic if you choose to increase or decrease view size. Hope this helps you @dmroz
I agree, but I would prefer doing a Y value comparison and check when it is 100 units below the camera, and then call the GameOver or LifeOneDown method.
Answer by OneCept-Games · Jan 02, 2018 at 03:54 PM
That would be pure math to calculate when your player is out of vision, but much easier just to do a comparison of your camera- and player Y value (assuming you are in 2D view, and using an XY plane
if (cam.transform.position.y > player.transform.position.y + 100)
...
i have never used that so i don't even know where to put that in the code and is it in player or the camera or the level itself
You could put such a check in your Update() function of a script you add to the Player object. void Update() { if (this.transform.position.y-100 < Camera.main.transform.position.y) { GameOver(); } }
Answer by dmroz · Jan 02, 2018 at 07:41 PM
used the method @OneCept-Games gave me and it seemed to not work with the code i used.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fall : MonoBehaviour {
public Player player;
public Camera camera1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (camera1.transform.position.y > player.transform.position.y + 100)
{
Application.Quit();
}
}
}
this is on an empty game character as i thought this would work
Try adding Debug.Log(player.transform.position.y); to see what happens to y-value. if your character falls, either y or z should change accordingly, and you can see what value would be most close to when out of sight.
yeah did that and changed the value to -10 on both y and z switching between both and still not working