Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Time.realtimesincestarup + x (seconds)

Discussion in 'Scripting' started by Gregorius, Jun 25, 2017.

  1. Gregorius

    Gregorius

    Joined:
    Jan 29, 2015
    Posts:
    25
    using System.Collections;
    using UnityEngine;
    using System;
    using UnityEngine.UI;
    public class gun : MonoBehaviour {

    public float damage = 10f;
    public float range = 100f;
    public Camera fpsCam;
    public ParticleSystem ps;
    float shot;
    public GameObject impactEffect;
    public float impactForce = 30f;
    public float fireRate = 15f;
    public int ammo = 16;
    private float nttf = 0f;
    public AudioClip sound;
    AudioSource audio;
    public AudioClip reload;
    public Text textammo;
    public float timer;

    public bool isRN = false;
    void Start()
    {
    audio = GetComponent<AudioSource>();
    }

    void Update() {
    timer = Time.realtimeSinceStartup;
    textammo.text = "Count:" + ammo.ToString();
    if (isRN == false && Input.GetButtonDown("Fire1") && Time.time >= nttf && ammo >= 1 )
    {
    nttf = Time.time + 1f / fireRate;
    Shoot();
    audio.PlayOneShot(sound, 0.7F);

    }
    if (isRN == false && Input.GetKeyUp(KeyCode.R) && ammo < 16)

    {
    audio.PlayOneShot(reload, 0.7F);
    reloading();
    }
    else if(ammo==16 && timer== Time.realtimeSinceStartup + 2)
    {

    isRN = false;
    }

    }
    void Shoot()
    {

    ammo--;
    ps.Play();
    RaycastHit hit;
    if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
    {

    enemyhelt helt = hit.transform.GetComponent<enemyhelt>();
    if (helt != null)
    {
    helt.TakeDamage(damage);

    }

    if (hit.rigidbody != null)
    {
    hit.rigidbody.AddForce(-hit.normal * impactForce);
    }

    GameObject impactGo = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
    Destroy(impactGo, 2f);
    }
    }


    void reloading()
    {

    ammo = 16;
    isRN = true;
    }

    }


    IsRN(isReloadingNow)



    i wanted to make ,fe. sth like that "else if(ammo==16 && wait 2 seconds)
    {

    isRN = false;
    }"
    but timer== Time.realtimeSinceStartup + 2 dont work. it ist error but if statement just doesnt come true if u wait this 2 even 10 seconds. Can sb tell me what is wrong pls? ty
     
  2. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Gregorius likes this.
  3. Gregorius

    Gregorius

    Joined:
    Jan 29, 2015
    Posts:
    25
    Ty for help its working :)
     
    TaleOf4Gamers likes this.