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
1
Question by gnubberlang · Oct 29, 2012 at 11:04 PM · fpsdatetime

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 () 
     {
        
     }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
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
avatar image gnubberlang · Oct 30, 2012 at 10:59 PM 0
Share

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);

avatar image MrPhil · Oct 31, 2012 at 09:28 PM 0
Share

Sorry about that, you are correct about how AddSeconds work. I should have actually tested the code.

avatar image
0

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);           
             
         }
      
     }
Comment
Add comment · Show 3 · Share
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
avatar image gnubberlang · Oct 31, 2012 at 04:47 AM 0
Share

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.

avatar image gnubberlang · Oct 31, 2012 at 04:50 AM 0
Share

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.

avatar image MrPhil · Nov 06, 2012 at 07:23 PM 0
Share

Did you see the update to my answer?

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

10 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

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

Problem with Javascript updating variables. 1 Answer


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