Make effect timer in Unity

Asked

Viewed 57 times

1

hello I am making a 2d platform game in Unity and I made an item that to Oce Pegalo Oceoce will be able to jump higher but I would like to put a timer (I don’t want it to appear on the screen)for the effect to pass after a specific time and do not know how you could help me this the code of my item

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Crystal : MonoBehaviour
{
    [Header("Boost")]
    [SerializeField] float jumpBoost = 10f;

    [Header("SFX")]
    [SerializeField] AudioClip pickupSFX;
    [SerializeField] [Range(1f, 10f)] float pickupSFXVolume = 10f;

    //cached referns
    Player player;

    private void Start()
    {
        player = FindObjectOfType<Player>();
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        AudioSource.PlayClipAtPoint(pickupSFX, Camera.main.transform.position, pickupSFXVolume);
        player.jumpForce += jumpBoost;
        Destroy(gameObject);
    }
} 
  • Important you [Dit] your question and explain objectively and punctually the difficulty found, accompanied by a [mcve] of the problem and attempt to solve. To better enjoy the site, understand and avoid closures and negativities worth reading the Stack Overflow Survival Guide in English.

1 answer

0

This is one way to make the effect go away after a while, but I’m not sure if it’s the simplest:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

    public class Crystal : MonoBehaviour
    {
        [Header("Boost")]
        [SerializeField] float jumpBoost = 10f;
        [SerializeField] float boostTime; // tempo que vai levar para o efeito passar (em segundos)
            
        [Header("SFX")]
        [SerializeField] AudioClip pickupSFX;
        [SerializeField] [Range(1f, 10f)] float pickupSFXVolume = 10f;
        
        private bool boosting;
        private float boostTimer;
        private float normalJumpForce;
            
        //cached referns
        Player player;
        
        private void Awake()
        {
            normalJumpForce = player.jumpForce;
            boostTimer = boostTime; // Faz o timer comecar no maximo.
        }

        private void Start()
        {
            player = FindObjectOfType<Player>();
        }

        private void Update()
        {
            if (boosting)
            {
                player.jumpForce = normalJumpForce + jumpBoost;
                boostTimer -= Time.deltaTime; // Faz o timer do efeito diminuir com o tempo.

                if (boostTimer <= 0) // Se o tempo do efeito acabar.
                {
                    boostTimer = boostTime; // Reseta o timer.
                    boosting = false; // Para de fazer o efeito.
                }
            }
    
        private void OnTriggerEnter2D(Collider2D collision)
        {
            AudioSource.PlayClipAtPoint(pickupSFX, Camera.main.transform.position, pickupSFXVolume);
            boosting = true;
            Destroy(gameObject);
        }
    }
  • Man thanks for trying to help but I’ll leave it the other way anyway

  • Blz, good luck with your game!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.