Have 2 almost indentical FixedUpdate += Time.deltaTime but one doesn't work

So like in the title i have this:

var prefab : GameObject;
var spawn : Transform;
var attackSpeed : float;
private var apd : float;

function Start()
{
    apd = attackSpeed;
}

function FixedUpdate ()
{
    if(Input.GetButtonDown("Fire1") && apd>=attackSpeed)
    {
        clone = Instantiate(prefab, spawn.position, spawn.rotation);
        apd = 0;
    }
    if(apd<=attackSpeed)
    {
    apd += Time.deltaTime*1;
    }
}

So it’s kinda like limit to the “attack speed” or instantiate cooldown and there is this code that doesn’t work

var abilityQ : int;
var abilityW : int;
var abilityE : int;
var abilityR : int;

var abilityQCooldown : int; var qCd : int;
var abilityWCooldown : int; private var wCd : int;
var abilityECooldown : int; private var eCd : int;
var abilityRCooldown : int; private var rCd : int;

var multipleArrowsArrow : GameObject;
var multipleArrowsSpawn : Transform;

function Start()
{
    qCd = abilityQCooldown;
    wCd = abilityWCooldown;
    eCd = abilityECooldown;
    rCd = abilityRCooldown;
}

function FixedUpdate ()
{
    if(Input.GetKeyDown(KeyCode.Q) && qCd>=abilityQCooldown)
    {
        switch (abilityQ)
        {
            case 0: //No ability selected
                Debug.Log("No Q Ability Chosen");
                break;
           
            case 1: //MultipleArrows
                Instantiate(multipleArrowsArrow, multipleArrowsSpawn.position, multipleArrowsSpawn.rotation);
                Instantiate(multipleArrowsArrow, multipleArrowsSpawn.position, new Vector3(multipleArrowsSpawn.rotation.x,multipleArrowsSpawn.rotation.y,multipleArrowsSpawn.rotation.z+10));
                Instantiate(multipleArrowsArrow, multipleArrowsSpawn.position, new Vector3(multipleArrowsSpawn.rotation.x,multipleArrowsSpawn.rotation.y,multipleArrowsSpawn.rotation.z-10));
                qCd = 0;
                break;
        }
    }
   
   
    if(qCd<=abilityQCooldown)
    {
        qCd += Time.deltaTime;
    }
   
    if(wCd<=abilityWCooldown)
    {
        wCd += Time.deltaTime*1;
    }
   
    if(eCd<=abilityECooldown)
    {
        eCd += Time.deltaTime*1;
    }
   
    if(rCd<=abilityRCooldown)
    {
        rCd += Time.deltaTime*1;
    }

}

So I have 2 questions. Why does += Time.deltaTime work in the case no. 1 but not in case no. 2. Even if I put it in Update without IF statemant it still doesn’t count up like in case no. 1. It just would not go up after reseting it to 0 like a cooldown. Now there might be better ways to do “Cooldown spells” and stuff like that on the internet but since this is more like me learing the script im just curious to see what is wrong.

Secondly that part with Instantiate in case no. 2 where i add new vector3 blah blah rotation.z +10 it says that that is not a regular Unity Vector 3. I tried doing it with Eulers and Quanturnions but still had no luck. This is realy “itching” me since this is very basic script and yet I have next to no clue what I did wrong.

So if anyone has any idea please share it with me.

It doesn’t work because in 2nd it is of type int. (int)0.003 = 0, so you are adding 0. Also your *1 at the end of deltatTime does nothing.

As for Vector3, I don’t use js but I think you don’t use new keyword. Just Vector3(…)

1 Like

Thanks a lot! Both for quick reply and pointing out that I am blind (lol) Anyway with or without ‘new’ it still doesn’t work.
So one problem less if anyone else can help with that other issue that would be amazing!

Once again thank you a lot SlateNeon!

Could you paste the error it gives about the vector?

This. Change the variable types to float to match your first script and it will work as desired.

Thanks BoredMormon but SlateNeon already pointed that out while I am still confused about the second problem, here Slate this is the error I am getting:

Whitch is this line of code:

Instantiate(multipleArrowsArrow, multipleArrowsSpawn.position, Vector3(multipleArrowsSpawn.rotation.x,multipleArrowsSpawn.rotation.y,multipleArrowsSpawn.rotation.z+10));

I mean, I fixed the problem with adding 2 more spawnpoints with their unique rotations but that still doesn’t answer why Vector3 is not working.

Instantiate takes a quaternion as the final argument. You can use Quaternion.EulerAngles to convert between a Vector3 and a Quaternion.

1 Like

Thanks BoredMormon and SlateNeon you fixed both my problems!