- Home /
Death Zone Difficulties
Heads up, I am very new to programming and using unity.
Now, I am working on a 2D platformer. When the player falls off of the map I would like to have a death zone that destroys the player character (in this case a block), resets the level, and reduces a lives counter. I have been able to do this in a breakout tutorial on the Unity site. I am trying to re-purpose the code that was used there to accomplish this, but I cannot get it to work out.
Would anyone be able to give me some advice?
using System.Collections; using UnityEngine.SceneManagement; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class DeadZone : MonoBehaviour { private void OnTriggerEnter(Collider other) { BlockController.instance.loseLife(); } }
public class BlockController : MonoBehaviour {
public float maxSpeed = 10;
private bool facingRight = true;
public int lives = 3;
public Text livesText;
public float resetDelay = 1f;
Animator anim;
bool grounded = false;
public Transform groundCheck;
float groundRadius = .02f;
public LayerMask whatIsGround;
public float jumpForce = 700;
private GameObject cloneBlock;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate ()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
anim.SetBool("Ground", grounded);
anim.SetFloat("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
float move = Input.GetAxis("Horizontal");
anim.SetFloat("Speed", Mathf.Abs(move));
GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if (move > 0 && !facingRight)
Flip();
else if (move < 0 && facingRight)
Flip();
}
private void Update()
{
if(grounded && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool("Ground", false);
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 thescale = transform.localScale;
thescale.x *= -1;
transform.localScale = thescale;
}
public void loseLife()
{
lives--;
livesText.text = "Lives: " + lives;
Destroy(cloneBlock);
}
}
Answer by DasNanda · Sep 29, 2017 at 07:08 AM
I don't know how you want your game to look, but if you just want a fast respawn, you can also reset the players position to the start instead of restarting the scene, because passing the life variable through a scene restart is a little trickier. If you would do it that way you would just need to create an empty game object called Spawn or something and put it where the player is supposed to spawn. Then in your DeadZone script, do this:
using System.Collections;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DeadZone : MonoBehaviour
{
public Transform spawn;
private void OnTriggerEnter(Collider other)
{
BlockController.instance.loseLife();
other.transform.position = spawn.position;
}
}
You could also use BlockController.instance.position = spawn.position, but I think using the collider is faster.
For some reason I can't get BlockController.instance.loseLife(); to work. It is not recognizing instance saying that there is no definition for it.
I'm not sure what to put there.
Okay. I wasn't sure how you were using it and thought your solution might be faster, but in that case, just do this:
other.GetComponent<BlockController>().loseLife();
other is the collider of the player entering the deadzone, so all you need to do is get the blockController from it and start the function :)