- Home /
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.
Answer by rutter · Apr 02, 2012 at 09:24 PM
I'd probably do something like this:
When player pushes trigger, store current
Time.timeWhen player releases trigger, subtract stored time from current
Time.timeResult 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.
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.
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.
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";
}
}
}
Your answer
Follow this Question
Related Questions
Increase health smoothly not instantly? 2 Answers
Recharging battery script, help 1 Answer
Need help with this Rotation Script (fixes) 1 Answer
a one time script 1 Answer
How to improve my enemy behavior ? 2 Answers