Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 isyrfs · Apr 20, 2021 at 03:11 PM · gameobjectdestroygameobjecttimer countdown

how to stop the timer when player dies hit the enemy?,how to stop the timer when player dies hit the enemy?

Hi, I'm trying to make my timer stop when player dies from hitting the enemy. My corncern is when the player hits the enemy, the player died and the timer isn't stop. Can someone help? Thanks.

Here is the script:

Timer

 public class TimerController : MonoBehaviour
 {
     public Image time_radial;
     float time_remaining;
     public int max_time = 60;
     public Text timeText;
  
     // Start is called before the first frame update
     void Start()
     {
         time_remaining = max_time;
     }
  
     // Update is called once per frame
     void Update()
     {
         if(time_remaining > 0)
         {
             time_remaining -= Time.deltaTime;
             time_radial.fillAmount = time_remaining / max_time;
             timeText.text =time_remaining.ToString("N0");
         }
        
        
     }
  
 }

Player

 public class PlayerControl : MonoBehaviour
 {
     Animator anim;
     public float moveSpeed = 3f;
     float velY;
     float DirX;
     bool facingRight = true;
     Rigidbody2D rigBody;
  
     public GameObject bulletToRight, bulletToLeft, gameOverText, restartButton, restartBox, blood;
     Vector2 bulletPos;
     Vector3 localScale;
     public float fireRate = 0.5f;
     float nextFire = 0.0f;
  
  
     // Start is called before the first frame update
     void Start()
     {
         rigBody = GetComponent<Rigidbody2D>();
         anim = GetComponent<Animator>();
         localScale = transform.localScale;
  
         gameOverText.SetActive(false);
         restartButton.SetActive(false);
         restartBox.SetActive(false);
  
     }
  
     // Update is called once per frame
     void Update()
     {
        
         DirX = Input.GetAxisRaw("Horizontal");
         velY = rigBody.velocity.y;
         rigBody.velocity = new Vector2(DirX * moveSpeed, velY);
  
         if (Input.GetButtonDown("Jump") && rigBody.velocity.y == 0)
             rigBody.AddForce(Vector2.up * 400f);
  
         if (Mathf.Abs(DirX) > 0 && rigBody.velocity.y == 0)
             anim.SetBool("isRunning", true);
         else
             anim.SetBool("isRunning", false);
  
         if (rigBody.velocity.y == 0)
         {
             anim.SetBool("isJumping", false);
             anim.SetBool("isFalling", false);
         }
  
         if (rigBody.velocity.y > 0)
             anim.SetBool("isJumping", true);
  
         if (rigBody.velocity.y < 0)
         {
             anim.SetBool("isJumping", false);
             anim.SetBool("isFalling", true);
         }
  
  
             if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
         {
             nextFire = Time.time + fireRate;
             fire();
  
         }
     }
     void LateUpdate()
     {
         Vector3 localScale = transform.localScale;
         if (DirX > 0)
         {
             facingRight = true;
         }
         else if (DirX < 0)
         {
             facingRight = false;
         }
  
         if (((facingRight) && (localScale.x < 0)) || ((!facingRight)
             && (localScale.x > 0)))
         {
             localScale.x *= -1;
         }
  
         transform.localScale = localScale;
     }
  
     void fire()
     {
         bulletPos = transform.position;
         if (facingRight)
         {
             bulletPos += new Vector2(+1f, -0.43f);
             Instantiate(bulletToRight, bulletPos, Quaternion.identity);
  
         }
         else
         {
             bulletPos += new Vector2(-0.4f, -0.4f);
             Instantiate(bulletToLeft, bulletPos, Quaternion.identity);
  
         }
     }
  
     private void OnCollisionEnter2D(Collision2D col)
     {
         if (col.gameObject.tag.Equals("Enemy"))
         {
             gameOverText.SetActive(true);
             restartButton.SetActive(true);
             restartBox.SetActive(true);
             Instantiate(blood, transform.position, Quaternion.identity);
             gameObject.SetActive(false);
         }
     }
 }


,I trying to make my timer stop when player dies from hitting the enemy. But when player dies, the timer is still counting down.

Here's the script: timer

 public class TimerController : MonoBehaviour
 {
     public Image time_radial;
     float time_remaining;
     public int max_time = 60;
     public Text timeText;
  
     // Start is called before the first frame update
     void Start()
     {
         time_remaining = max_time;
     }
  
     // Update is called once per frame
     void Update()
     {
         if(time_remaining > 0)
         {
             time_remaining -= Time.deltaTime;
             time_radial.fillAmount = time_remaining / max_time;
             timeText.text =time_remaining.ToString("N0");
         }
        
        
     }
  
 }


player script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
 public class PlayerControl : MonoBehaviour
 {
     Animator anim;
     public float moveSpeed = 3f;
     float velY;
     float DirX;
     bool facingRight = true;
     Rigidbody2D rigBody;
  
     public GameObject bulletToRight, bulletToLeft, gameOverText, restartButton, restartBox, blood;
     Vector2 bulletPos;
     Vector3 localScale;
     public float fireRate = 0.5f;
     float nextFire = 0.0f;
  
  
     // Start is called before the first frame update
     void Start()
     {
         rigBody = GetComponent<Rigidbody2D>();
         anim = GetComponent<Animator>();
         localScale = transform.localScale;
  
         gameOverText.SetActive(false);
         restartButton.SetActive(false);
         restartBox.SetActive(false);
  
     }
  
     // Update is called once per frame
     void Update()
     {
        
         DirX = Input.GetAxisRaw("Horizontal");
         velY = rigBody.velocity.y;
         rigBody.velocity = new Vector2(DirX * moveSpeed, velY);
  
         if (Input.GetButtonDown("Jump") && rigBody.velocity.y == 0)
             rigBody.AddForce(Vector2.up * 400f);
  
         if (Mathf.Abs(DirX) > 0 && rigBody.velocity.y == 0)
             anim.SetBool("isRunning", true);
         else
             anim.SetBool("isRunning", false);
  
         if (rigBody.velocity.y == 0)
         {
             anim.SetBool("isJumping", false);
             anim.SetBool("isFalling", false);
         }
  
         if (rigBody.velocity.y > 0)
             anim.SetBool("isJumping", true);
  
         if (rigBody.velocity.y < 0)
         {
             anim.SetBool("isJumping", false);
             anim.SetBool("isFalling", true);
         }
  
  
             if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
         {
             nextFire = Time.time + fireRate;
             fire();
  
         }
     }
     void LateUpdate()
     {
         Vector3 localScale = transform.localScale;
         if (DirX > 0)
         {
             facingRight = true;
         }
         else if (DirX < 0)
         {
             facingRight = false;
         }
  
         if (((facingRight) && (localScale.x < 0)) || ((!facingRight)
             && (localScale.x > 0)))
         {
             localScale.x *= -1;
         }
  
         transform.localScale = localScale;
     }
  
     void fire()
     {
         bulletPos = transform.position;
         if (facingRight)
         {
             bulletPos += new Vector2(+1f, -0.43f);
             Instantiate(bulletToRight, bulletPos, Quaternion.identity);
  
         }
         else
         {
             bulletPos += new Vector2(-0.4f, -0.4f);
             Instantiate(bulletToLeft, bulletPos, Quaternion.identity);
  
         }
     }
  
     private void OnCollisionEnter2D(Collision2D col)
     {
         if (col.gameObject.tag.Equals("Enemy"))
         {
             gameOverText.SetActive(true);
             restartButton.SetActive(true);
             restartBox.SetActive(true);
             Instantiate(blood, transform.position, Quaternion.identity);
             gameObject.SetActive(false);
         }
     }
 }



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

0 Replies

· Add your reply
  • Sort: 

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

176 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 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 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 can I destroy gameobject when passing the screen 0 Answers

GameObject doesnt get destroyed onCollision 2 Answers

Optimize huge number of Destroy() calls (cannot use object pooling) 3 Answers

Timer not working in prefabs 0 Answers

Destorying gameobjects on a network 0 Answers


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