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);
}
}