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 CobOfTheCorn · Jan 23, 2018 at 07:43 PM · newbiedeath

Adding in a system to check for player death.

Hello, I am new here and I was wanting to add a system to my script that checks if the player has died from collision or not. Below is my script

public class Guy : MonoBehaviour{

 void Update()
 {
     Movement();
 }

 void Movement()
 {
     if (Input.GetKey(KeyCode.D))
     {
         transform.Translate(Vector2.right * 4f * Time.deltaTime);
         transform.eulerAngles = new Vector2(0, 0);
     }
     if (Input.GetKey(KeyCode.A))
     {
         transform.Translate(Vector2.right * 4f * Time.deltaTime);
         transform.eulerAngles = new Vector2(0, 180);
     }
 }

}

and here is one that has something similar to what I am trying to achieve.

public class Bird : MonoBehaviour { public float upForce; //Upward force of the "flap". private bool isDead = false; //Has the player collided with a wall?

 private Animator anim;                  //Reference to the Animator component.
 private Rigidbody2D rb2d;               //Holds a reference to the Rigidbody2D component of the bird.

 void Start()
 {
     //Get reference to the Animator component attached to this GameObject.
     anim = GetComponent<Animator> ();
     //Get and store a reference to the Rigidbody2D attached to this GameObject.
     rb2d = GetComponent<Rigidbody2D>();
 }

 void Update()
 {
     //Don't allow control if the bird has died.
     if (isDead == false) 
     {
         //Look for input to trigger a "flap".
         if (Input.GetMouseButtonDown(0)) 
         {
             //...tell the animator about it and then...
             anim.SetTrigger("Flap");
             //...zero out the birds current y velocity before...
             rb2d.velocity = Vector2.zero;
             //  new Vector2(rb2d.velocity.x, 0);
             //..giving the bird some upward force.
             rb2d.AddForce(new Vector2(0, upForce));
         }
     }
 }

 void OnCollisionEnter2D(Collision2D other)
 {
     // Zero out the bird's velocity
     rb2d.velocity = Vector2.zero;
     // If the bird collides with something set it to dead...
     isDead = true;
     //...tell the Animator about it...
     anim.SetTrigger ("Die");
     //...and tell the game control about it.
     GameControl.instance.BirdDied ();
 }

}

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
0
Best Answer

Answer by CobOfTheCorn · Jan 23, 2018 at 08:16 PM

I thought a Boolean would do the trick as a simple true or false for the player death, but maybe I am misplacing the boolean in the script, because after adding it in my player does not move at all.

public class Guy : MonoBehaviour{

 private bool isDead = false;
     
 void Update()
 {
  Movement();
 }

 void Movement()
 {
     if (isDead =false)
     { 
     if (Input.GetKey(KeyCode.D))
     {
         transform.Translate(Vector2.right * 4f * Time.deltaTime);
         transform.eulerAngles = new Vector2(0, 0);
     }
     if (Input.GetKey(KeyCode.A))
     {
         transform.Translate(Vector2.right * 4f * Time.deltaTime);
         transform.eulerAngles = new Vector2(0, 180);
     }
 }

} }

Comment
Add comment · Show 1 · 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 LTonon · Jan 23, 2018 at 08:28 PM 0
Share

In your function $$anonymous$$ovement() you're are doing "isDead = false", when the correct would be "isDead == false". In C# using only one "=" means an value attribution, using "==" means a comparison between two values. It's also a good program$$anonymous$$g practice to organize your code in a way that each new block of code is moved to the right, like this:

 private bool isDead = false;
             
 void Update() 
 {
     $$anonymous$$ovement();
 }
             
 void $$anonymous$$ovement() 
 {
     if (isDead == false) 
     {
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) 
         {
             transform.Translate(Vector2.right * 4f * Time.deltaTime);
             transform.eulerAngles = new Vector2(0, 0);
         }
         
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) 
         {
               transform.Translate(Vector2.right * 4f * Time.deltaTime);
               transform.eulerAngles = new Vector2(0, 180);
         }
     }
 }
 
 

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

75 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 avatar image avatar image avatar image

Related Questions

I have an attack/take damage script how do I make the player die when health hits 0? 1 Answer

Kinda lost with animating player from another script 2 Answers

Simple NPC AI using Nav Mesh 1 Answer

Making a pick up item that is timed, that moves,scales and rotates 1 Answer

UI buttons in building game 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