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.

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.

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

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";
 	}    

} 

}