#Automatic Fire

1 messages · Page 1 of 1 (latest)

queen void
#

hey, channel is crowded so let's take it here

wide lagoon
#

yup thanks

queen void
#

Need a few minutes to get my head around logic. The code is for Button when Held, so it never triggers the first time

wide lagoon
queen void
#

did you run the script I linked, or did you modify it to work?

wide lagoon
#

should i send you that

queen void
#

sure

wide lagoon
#

can you speak german just asking

queen void
#

No, I only studied it for one year half a lifetime ago. Göten tag.

wide lagoon
#

so the use thing is a bit more down

#

bassicly the script has a lot in it

queen void
#

isch spreissen nicht deutch (?)

wide lagoon
#

i followed a youtube tutorial

wide lagoon
queen void
#

lol

#

hope I still recall the correct one for French

wide lagoon
#

did you find anything usefull on the link i send ya

queen void
#

yes

#

reading

wide lagoon
#

cool

queen void
#

public class PlayerController : MonoBehaviourPunCallbacks, IDamageable

MonoBehaviour, PunCallbacks ?

#

or did you make your own variation of MonoBehavior?

wide lagoon
#

i dont think so

queen void
#

then you might be missing a comma there

wide lagoon
#

where

queen void
#

line 9, public class etc

#

MonoBehaviourPunCallbacks

wide lagoon
queen void
#

if there was no error, then no need

wide lagoon
queen void
#

before or after?

wide lagoon
#

i think the problem is in the shooting script

wide lagoon
wide lagoon
queen void
#

nvm, I thought it was an error

wide lagoon
#

this is the shooting script

queen void
#
public override void Use()
    {
        cooldownTimer -= Time.deltaTime;

        if (Input.GetButton("Fire1") && cooldownTimer <= 0)
        {
            Shoot();
            cooldownTimer = rateOfFire;
        }
        else if (Input.GetButtonDown("Fire1"))
        {
            Shoot();
            cooldownTimer = rateOfFire;
        }
    }
#

remove the void Update I added

#

keep the variables rateOfFire and cooldownTimer

#

I removed Mathf.Clamp, since I realized it was not necessary

#

this means cooldownTimer also tells you how long since you last fired your weapon (but in negative numbers + rateOfFire)

#

hmm, I might have to add this in the other script. Let me know how it goes.

wide lagoon
#

i dont manage to get that done

queen void
#

what, specifically?

wide lagoon
#

where i have to put your code in without getting 10000 errors lol

#

should i replace public override void with your code?

queen void
#

yes

wide lagoon
#

ok

queen void
#

I rewrote that entire segment

#

but I kinda see now that the input triggers are in the other script

#

so at this moment, I am sure the first one will fail

#

it might work, but still bad logic

wide lagoon
queen void
#

brb

wide lagoon
#

is this right?

queen void
#

{

    Shoot();
}
#

remove that

wide lagoon
queen void
#

but also, I think my IF statement logic is bad

#

I'm rewriting for the other script, brb

wide lagoon
#

now?

queen void
#

missing the end curly-bracket } for Use()

wide lagoon
#

what line ?

#

i dont see it

queen void
#

36

wide lagoon
#

i can shoot but without it being automatic

#

bassicly like before

queen void
#

sec, I think I have fixed it

#

in PlayerController

#

delete the following

if(Input.GetButtonDown("Fire1"))
{
    items[itemIndex].Use(); 
}
wide lagoon
#

ok lemme try that

queen void
#

then replace it with

if (cooldownTimer <= 0)
{
    // Click
    if(Input.GetButtonDown("Fire1"))
    {
        items[itemIndex].Use();
        cooldownTimer = rateOfFire;
    }
    // Hold
    else if(Input.GetButton("Fire1"))
    {
        items[itemIndex].Use();
        cooldownTimer = rateOfFire;
    }
}
#

and add these variables on the top

float rateOfFire = 0.5f;
float cooldownTimer;
#

(undo all the other changes - I did this from scratch, without the stuff you tried earlier)

#

and change rateOfFire = 0.1f; or something

wide lagoon
#

right?

queen void
#

yes

wide lagoon
#

and in the other script i have to delete everything right?

#

like go back

#

where i was at the start

queen void
#

yes

#

correct

wide lagoon
#

let me test that

#

nothing

#

can i send ya what i did

wide lagoon
queen void
#

are you getting any errors?

#
float cooldownTimer = 0f;
#

try that

wide lagoon
wide lagoon
#

i can shoot one time

#

and then not

queen void
#
public override void Use()
{
Shoot();
}

is this still in the other script?

wide lagoon
#

1 shot

queen void
#

hm

wide lagoon
queen void
#

post your current SingleShotGun script

wide lagoon
#

i will post both i think i made something wrong in the player controller stuff

wide lagoon
wide lagoon
queen void
#

if (cooldownTimer <= 0)
if (cooldownTimer <= 0f)

#

change the zero to a float, try again

wide lagoon
#

what ? i dont understand what you mean

queen void
#

0 is an int
0f is a float

#

int cannot have decimals

wide lagoon
#

but both of them have an f

queen void
#

often it doesn't matter, but since we are testing for a bug, it's best to make sure all numbers are cast to the same type

#

if (cooldownTimer <= 0)

#

line 103

wide lagoon
#

ok

#

so i gave the 0 an f

queen void
#

any change in the result?

#

you are so far able to shoot once, but then never again?

wide lagoon
#

nope 1 shoot

wide lagoon
#

and then never

queen void
#

Debug.Log("Click");
Debug.Log("Hold");

put these in their respective if-statements

queen void
#
        if (cooldownTimer <= 0)
        {
            // Click
            if (Input.GetButtonDown("Fire1"))
            {
                items[itemIndex].Use();
                cooldownTimer = rateOfFire;
            }
            // Hold
            else if (Input.GetButton("Fire1"))
            {
                items[itemIndex].Use();
                cooldownTimer = rateOfFire;
            }
        }
#

to test whether any of the statements are triggered, add a debug log to each of them

wide lagoon
#

so i add the debug log to both of them

queen void
#

with different texts, so you know which one is triggered

wide lagoon
#

k

#

so i added the thing under cooldown timer is this right?

queen void
#

as long as it is within the same curly-bracket code segment

wide lagoon
#

nothing more

#

even when i hold or click again

queen void
#

show me the code you just fixed

#

the if-statements

wide lagoon
#

what do you mean

#

i will send you the code again

queen void
#

Okay. Take a minute and breathe.
Look at the Debug.Log() lines you entered
then read line 105 and 113

wide lagoon
#

i dont get it

queen void
#

only one debug log per if-statement

#

one is // Click

#

one is // Hold

wide lagoon
#

oh ok

#

only click no hold

queen void
#

and you are indeed holding down the button?

wide lagoon
#

let me try again

#

did the hold nothing happend

queen void
#

ok. ANd you can only fire once?

#

Were you able to fire multiple times before I started helping?

wide lagoon
#

onlce 1

#

yes

#

i could shoot as much as i wanted

queen void
#

revert your code back to that time
test that it works, then post the code to me

wide lagoon
#

so before you helped me

#

all to 0

queen void
#

yes

wide lagoon
#

k

#

i can spam again

queen void
wide lagoon
#

what do i have to do with that

#

do i have to replace that with my code

queen void
#

copy it
go to your script
mark all
paste

wide lagoon
#

YES

#

i can hold now

#

but can i change the firerate in any way?

#

it seems to fast for me

queen void
#

Good stuff!

#

yes, just change the variable rateOfFire

wide lagoon
#

very cool

#

so i have another weapon like a pistol if i change the script here it will also change it for the pistol right?

queen void
#

if you didn't copy/paste the entire script earlier, that might be why it didn't work
I used better logic this time, but it should essentially be 100% the same.

Remember to never make assumptions, and reduce as many assumptions as possible
When trying to fix problems, a good point is to search every corner, even the ones you think you know

#

Very nice that we made it :)

wide lagoon
#

it works great VERY MUCH thanks but yeah how can i change it so it has different values on another weapon

#

is there way to make that happen

#

but yeah thank you so much

#

it took me 4 dayys

#

jesus

#

thanks

queen void
#

that depends on how you've structured your project
ideally, the rate of fire is a property of the weapon itself
or it contains a value which it gives to rateOfFire, whenever it is equipped

#

I am guessing you want to add rateOfFire to the Gun class

queen void
wide lagoon
#

like i dont want my pistol to shoot at the same speed as the assault rifle

queen void
#

yeah that would be broken. there is a reason those are banned in real life xD

queen void
#

not everywhere ofc

wide lagoon
#

so is there a way for that

#

maybe to make the firerate a public class

#

oh no

#

wait

#

nah that would not fix anthing

queen void
#

the quickest fix I see

wide lagoon
#

tell me

queen void
#

is adding a float to Gun
named rateOfFire

and whenever a weapon is equipped
rateofFire = yourGun.rateOfFire;

wide lagoon
#

how can i do that

queen void
#

link your Gun class

wide lagoon
queen void
#

the code

#

for the class named Gun

wide lagoon
#

yup

#

what do i have to do with the class named gun

#

i have a script called gun

queen void
#

show it to me

wide lagoon
#

k

#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class Gun : Item
{
public abstract override void Use();

public GameObject bulletImpactPrefab;

}

queen void
#

so at this moment, none of the guns have different attributes?

wide lagoon
#

no?

queen void
#

are you following a guide or writing this from scratch yourself?

wide lagoon
#

guide

queen void
#

have you completed the entire guide?

wide lagoon
#

yes

#

the pistol has a different damge thing

#

like i can change the damage on the pistol

queen void
#

where is the different damage for guns coded?

wide lagoon
#

i have to look

#

guninfo

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName = "FPS/New Gun")]
public class GunInfo : ItemInfo
{
public float damage;
}

#

this is the script

queen void
#

try adding a public float rateOfFire;

wide lagoon
#

ok

#

should i just copy public float rateOfFire

#

i did that

#

can i try it

#

ok i can change rate of fire

queen void
#

there is already a public float damage there

wide lagoon
#

i will test f it does anything

queen void
#

you literally just add another float witrha different name

wide lagoon
#

ok

#

🙂

#

nothing changed

#

i did put different numbers in them

queen void
#

but at this point, it kind of stops for me.
It's nearly midnight, and I am barely familiar with the design pattern of your project.

From this point, you should ask in the channel again. People there have more diverse experience.

Your goal has to be understand the code. And I suspect you will learn a great deal once you figure out how to connect this last dot.

The last thing you need to fix, is to see how the float damage is called when you equip your weapon.
You need to do the same for the float rateOfFire

wide lagoon
#

i think i found it in the singleshotgun code

#

hit.collider.gameObject.GetComponent<IDamageable>()?.TakeDamage(((GunInfo)itemInfo).damage);

queen void
#

that's what I don't have the time to figure out for you
and I also believe you should fix that part yourself. it should be simple, as you just need to follow the logic of the damage float

wide lagoon
#

if i have question i will ask them in beginner code again

wide lagoon
#

28

queen void
#

hm. it's functional, but there might be a performance issue down the line.
It's standard practice to get components only once, and beforehand, because it is slow.

But in this case I don't think it matters at all.

wide lagoon
#

hmm ok

queen void
#

is there an Equip Weapon method?

wide lagoon
#

like in a script?

queen void
#

yes

#

void EquipItem(int _index)

wide lagoon
queen void
#

inside that method, get the rateOfFire of the weapon
and then set the global rateOfFire to that value

wide lagoon
#

item info

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemInfo : ScriptableObject
{
public string itemName;
}

item

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class Item : MonoBehaviour
{
public ItemInfo itemInfo;
public GameObject itemGameObject;

public abstract void Use();

}

queen void
#

hm, yeah, go to the channel again lol

#

try General Code or Advanced

#

Have a nice evening man. Good luck with your game o/

wide lagoon
#

thanks good night 🙂

wide lagoon
#

Simplest approach is to change the cool down according to your current equipped weapon in that firerate script. For that you need to save firerate(the cool down float value) somewhere and it has to be accessible by code.
You can add a script to your weapon which has that data, or use a scriptable object for it

LOL how do that

queen void
#

You already have that

#

float damage
float rateOfFire

are in a scriptable object

#

search "scriptable object" in that article

#

it shows good examples of how to refer to the variables

wide lagoon
#

And how can I use that on my script

#

public class PlayerHealth : MonoBehaviour
{
AudioSource playerAudioSource;

void Start()
{
    playerAudioSource = GetComponent<AudioSource>();
}

}

wide lagoon
queen void
#

the article is very long. you need to search inside the website for "scriptable object"

#

Ideally, I recommend you read the entire article. It is extremely good at explaining how to reference all sorts of variables.

#

I will be rather busy this week, so I'm not sure I if I will have the time.
If I come up with a solution, I will let you know.

#

Fixing your problem would require me to learn how your programming pattern works.
And at that point, it makes more sense that you do the research yourself.
But since you seem to be in way over your head, you should try to get help from someone who knows how this pattern works.

#

in simplicity, you need to set the script's private float rateOfFire to the gun's rate of fire, somewhere in the Equip process

wide lagoon
#

But yeah I will do my own research

wide lagoon
#

lol

queen void
wide lagoon
wide lagoon
#

damage is retrieved in a complete different way

#

we can forget how damage is retrieved

#

hit.collider.gameObject.GetComponent<IDamageable>()?.TakeDamage(((GunInfo)itemInfo).damage);

#

this is the damage part

#

and i dont think we can put something like this on the code

queen void
#
((GunInfo)itemInfo).damage
#

try itemInfo.rateOfFire or something

wide lagoon
#

where do i have to put it in

queen void
#

without the other stuff

#

try writing a line of code that leads to the Rate Of Fire float

wide lagoon
queen void
#

No, I have no idea how to do it with your design pattern.

#

Did you read the article I linked?

wide lagoon
#

yeah

#

did not help much

queen void
#

the object holding the floats for damage and rateoffire for your guns, is a scriptable object
maybe re-check that paragraph
or ask in one of the other channels

wide lagoon
#

i see nothing

#

seems like i cant find a fix for that problem

queen void
#

the only thing you need to know, is how to reference a scriptable object's variable, within the design pattern which makes up your code

wide lagoon
#

is it possible to make an if statement so it is like

when 1 is equiped
change the firerate variable to this

#

and when 0 is equipped do this

queen void
#

yes - but if you're getting data from the weapon, then you should just as easily get the other variables

#

or is that from the array that chooses weapons?

wide lagoon
queen void
#

Well, you could set it there. It would work.
But it's much more code.

wide lagoon
queen void
# wide lagoon but would it be more easy ? bc i cant seem to get it done without your help but ...

Look. I visit beginner-code to help people start coding, and teach them how to do the basic stuff.
What you have here is not a beginner type of code.
For some reason, you are lacking very basic knowledge, yet you have chosen to use this design pattern.
What you are trying to achieve is beyond me - because you might end up with a "finished product" but you risk learning nothing.

I do not have the knowledge or time to help you.

I have looked over your efforts in the other channels.
You need to look more at the examples around you who actually received help.
Don't ask for help - post the issue - describe it in every way that you can, and explain what you are trying to achieve.

If you define your question properly, you will attract the more competent people who want to help.

wide lagoon
#

ok thanks for the tipp

queen void
#

You're welcome.

#

One thing that might make it easier to get help, is upload a compressed rar/zip of your project
I think it is necessary due to the structure of your code

wide lagoon
#

i will finish my map and stuff for the my game will only have one weapon as it seems but yeah thanks for all the help you gave me

queen void
#

I do hope you fix it before your deadline. Best of luck, man o7