- Home /
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