- Home /
DateTime rocket launcher timing issue
Anyone know why this doesn't work? It initially waits the 3 seconds to fire, but after that it's failing to listen to this line:
**LastFire = DateTime.Now.AddSeconds(firerate);**
After the initial 3 seconds, it proceeds to vomit rockets. As far as I can tell LastFire is not being assigned, any ideas?
public class MissleLauncher : MonoBehaviour
{
public Rigidbody projectile;
public float firerate = 3;
DateTime LastFire = DateTime.Now;
float speed = 1f;
// Use this for initialization
void Start ()
`` {
}
void Fire()
{
if (LastFire.AddSeconds(firerate) <= DateTime.Now)
{
Rigidbody instantiatedProjectile;
instantiatedProjectile = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation);
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, 100));
//instantiatedProjectile.position = transform.TransformDirection(transform.position + new Vector3(0, 0, 20));
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.collider);
LastFire = DateTime.Now.AddSeconds(firerate);
}
}
// Update is called once per frame
void Update ()
{
}
Answer by MrPhil · Oct 30, 2012 at 05:45 AM
Update: I think your problem is not actually shown in your code above! I bet you are doing this:
Input.GetKeyDown(KeyCode.Space)
when you mean to be doing this:
Input.GetKey(KeyCode.Space)
Here's my code and it works:
using System;
using UnityEngine;
public class FireRocket : MonoBehaviour
{
public Rigidbody projectile;
public float firerate = 3;
DateTime LastFire = DateTime.Now;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.Space))
{
Fire();
}
}
void Fire()
{
if (LastFire.AddSeconds(firerate) <= DateTime.Now)
{
Rigidbody instantiatedProjectile;
instantiatedProjectile = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation);
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, 100));
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.collider);
LastFire = DateTime.Now.AddSeconds(firerate);
}
}
}
Original Answer: I think this line of code is your problem:
if (LastFire.AddSeconds(firerate) <= DateTime.Now)
This adds firerate seconds to the LastFire variable before comparing it to DateTime.Now, every time the Fire method is called.
The AddSeconds function only returns the result of the time should it have 'firerate' seconds added. Ex. DateTime.Now.AddSeconds(5) will always return 5 seconds in the future and doesn't modify "Now"
It's like this assignment isn't happening - not sure why. LastFire = DateTime.Now.AddSeconds(firerate);
Sorry about that, you are correct about how AddSeconds work. I should have actually tested the code.
Answer by gnubberlang · Oct 31, 2012 at 04:45 AM
So, I don't have an explanation for this yet, but when I moved the DateTime assignment to the top of the Fire() block, it works like a charm. SO CONFUSED! If anyone knows what is going on here please let me know, I'd love to have my sanity back.
public void Fire()
{
if(DateTime.Now.CompareTo(LastFire)>0)
{
LastFire = DateTime.Now.AddSeconds(firerate);
BroadcastMessage("UpdateGUIText", LastFire.ToString());
Rigidbody instantiatedProjectile;
instantiatedProjectile = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation);
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, 100));
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.collider);
}
}
one thing I meant to mention above, something after the LastFire assignment is causing the script to either exit, or do something odd. $$anonymous$$aybe once it's spawned the new projectile it's a new instance of the script?? Just speculating.
Also one more thing, I was messing around with the test, both versions of the if statement work as they're supposed to. I tried CompareTo based on your suggestion Philip.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
FPS custom weapons & characters? 2 Answers
Whow do i change my character height 1 Answer
Aiming down a gun 4 Answers