Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lestes25 · Sep 29, 2017 at 01:15 AM · 2d-platformerresetsidescrollerlives

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

 

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lestes25 · Sep 30, 2017 at 03:25 AM 0
Share

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.

avatar image DasNanda lestes25 · Sep 30, 2017 at 01:21 PM 0
Share

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 :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

72 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to setup health, respawn/checkpoint, lives systems 1 Answer

Unity 2D - Make Player Auto-Move? 1 Answer

Resting health images.. convoluted 3d tut + TT tu script 0 Answers

2D camera that tracks player after running short distance 1 Answer

How do I stop momentum in 2D sidescroller? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges