• 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
0
Question by DracoScorpius · Apr 02, 2012 at 01:34 AM · timeattackcharge

Charging an Attack over Time since Last Shot

Hi, and sorry if this has already been answered, but I couldn't find it. I have a weapon, a lightning field, and as it stands I can use it every second, and it does 3 damage (I think, haven't tested the tag yet). I would like to set it up so that for the first second you can't use it, from 1-2 seconds it'll do 1 damage, 2-3 seconds it'll do 2 damage, and from 3+ seconds if used it'll do 3 damage.

 var lightningShield : Rigidbody;
 
 var spawnPoint;
 
 var fireRate : float = 1.0;
 
 var nextFireTime : float = 1.0;
 
 function Update () 
 
 {
 
     if (Input.GetButtonDown("Fire2") && Time.time > nextFireTime)
 
     {
     
     nextFireTime = Time.time + fireRate;
     
     var clone : Rigidbody;        
 
     clone = Instantiate(lightningShield, transform.Find("LightningShieldSpawn").transform.position, Quaternion.identity);
 
     lightningShield.gameObject.tag = "dmg3";
 
     }
 
 }



Any help is greatly appreciated. How it works now is fine for me, but I wouldn't mind adding that extra element that benefits those with more patience, which few people seem to have nowadays xD.

EDIT: Ok, I have a few ideas on how to fix the problem myself, just needed a day to think about it. The use of "int" values can set it up to work at certain times, also spawning multiple shields at once, all tagged to do 1 damage, could solve the problem. I still need to try these ideas, but if someone would like to help it would be appreciated.

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 rutter · Apr 02, 2012 at 09:24 PM

I'd probably do something like this:

  • When player pushes trigger, store current Time.time

  • When player releases trigger, subtract stored time from current Time.time

  • Result tells you how long the trigger was held

Something like this, maybe:

var triggerDownTime : float;

 function Update () 
 {
     if (Input.GetButtonDown("Fire2"))
     {
         triggerDownTime = Time.time;
     }
     else if (Input.GetButtonUp("Fire2"))
     {
         var chargeTime = Time.time - triggerDownTime;

         //you could do some branching, here
         //but it looks like X seconds held gives X damage
         //so we can just take the floor (round down)
         var damage = Mathf.Floor(chargeTime);
     }
 }

That's not a complete solution, of course, but it's another option you could try. If you have anything that indicates charge steps to the player, code like this could make it really easy to use lerps.

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 DracoScorpius · Apr 02, 2012 at 09:37 PM 0
Share

Thanks for the prompt reply. However, what I am planning involves checking the time since the trigger was last used ie. when the trigger is pushed, the item is used, holding it has no effect. So in essence, you can use the item once every second, but it'll only do one damage, but if you wait the extra second it'll do two, then to a maximum of three. I want it to focus on the patience factor of the player before they spam the button.

avatar image rutter · Apr 03, 2012 at 01:04 AM 0
Share

Ah, I see. Cool idea, that.

The math above is still more or less good, I think; only change I anticipate is that you're setting the "start" value per shot, rather than per trigger pull. $$anonymous$$ain idea I was trying to convey was your ability to easily and efficiently calculate time deltas.

avatar image
0

Answer by DracoScorpius · Apr 03, 2012 at 01:35 AM

Ok, I solved it myself, so I'll post an answer here for anyone else who may have a similar question. Enjoy :)

var lightningShield : Rigidbody; var spawnPoint; var triggerDownTime : float = 0.0; var chargeTime : int = 0.0;

function Update () {

 chargeTime = Time.time - triggerDownTime - 1.5;
 
 if (Input.GetButtonDown("Fire2"))
 {
   
     //nextFireTime = Time.time + fireRate;
   
     triggerDownTime = Time.time;
     
 if(chargeTime == 1)
      {
         var clone1 : Rigidbody;        
         clone1 = Instantiate(lightningShield, transform.Find("LightningShieldSpawn").transform.position, Quaternion.identity); 
           clone1.gameObject.tag = "dmg1";
       } 
   
 if(chargeTime == 2)
       {
         var clone2 : Rigidbody;        
         clone2 = Instantiate(lightningShield, transform.Find("LightningShieldSpawn").transform.position, Quaternion.identity); 
           clone2.gameObject.tag = "dmg2";
       } 
   
 if(chargeTime >= 3)
       {
         var clone3 : Rigidbody;        
         clone3 = Instantiate(lightningShield, transform.Find("LightningShieldSpawn").transform.position, Quaternion.identity); 
           clone3.gameObject.tag = "dmg3";
      }    

 } 

}

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Enemy behavior is not working after the enemy has spawned. 1 Answer

Simple Attack Script Problem 2 Answers

"Charge" shot script help 1 Answer

I need to know how to make gameobject activate in like 1 minute.Please Help 2 Answers

Turret script TT longer reloading 2 Answers


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