#Disable coin interaction during animation
1 messages · Page 1 of 1 (latest)
That's my coin script.
I have another script that moves the coin towards the player aswell.
Ok I'll need the animation script too
also, to make it easier, enable all notifications in this thread
Ok, so what we're gonna do is make a reference to your animator in GoldTicket
Then, we're gonna want to check what state the animator is currently in so we're going to use Animator.GetCurrentStateInfo (returns AnimatorClipInfo[]), to get the clip name you're just gonna get the .clip.name from the animatorclipinfo's 0th place
So I disable that script if the animator is playing an animation?
If it make it easier, the coin only has one animation
Don't allow it to be picked up if the clip.name is equal to whatever the animation name is that makes it float
Don't hardcode it in case you want to add an animation later
Alright. I'll try GetCurrentClip
I'm not sure how to do this. I've set up my variables, but I don't know how I would tell my script to not do the pickup if the animation is playing.
Well, first of all youre setting the variables in start
which means they'll only be updated once when you start the program
ticketcontroller is fine
ticketchestclipinfo, length, and ticketchest shouldn't be in start though
Why are we getting the length of the clip?
How would I say "If animation is playing" , "return;"
I was blindly following the reference for a while. I realise I don't need the length though.
by the way, you're destroying the gameobject before getting the component and adding money
you can't do anything after destroying the object the script is on
Oh? The script worked fine. Would it be better to just switch it though?
Yes, and for future reference don't try to ever do anything after destroying the object the script is on
Alright.
It only worked because of luck in the garbage collection cycle
This is commented code that shows how you would stop it from being picked up while the animation is being played
I changed all of my scripts that had it this way. Luckily it was only the coins.
you shouldn't need this part anymore in start
you can always use OnDestroy() if you want to do something like that
I've run into an issue. I've been playing around with my GameObjects trying to get it to work and I keep running into the error "Index was outside the bounds of the array". One thing I had to do was copy my animator component onto my gameobject where the script is being held.
was your animator not on the same gameobject as the script
Not previously, due to some kinks in my coin script.
alright, you know what? we're gonna do this a simpler way
im assuming you dont know how to use coroutines, right
we're simply just going to wait a little bit after the coin appears to make it be able to be picked up
Generally not.
Still working on it one sec
Take your time. I'm grateful that your helping me in the first place haha.
i consider this my volunteer work :p
@soft kelp what is "cam"
you shouldn't use abbreviations in variable names because it makes it hard to understand for others, especially without a comment
Ah, cam is my player. My bad, I should've mentioned that.
Ok, I'm going to rename "cam" to player in the script
That's fine.
https://gdl.space/yevaraxaye.cs
here you go, I tried to be as thorough as possible @soft kelp
obviously a lot of the comments you can delete after you understand what everything does
but you should keep them until you know 100% that you understand what all of the code means
Yeah. I'm reading through it now.
if you have any questions feel free to ask
Does "internal float invincibilityDuration = 0.25f;" have to be internal to work?
I'm only asking because it'd be easier to edit if it showed up in the inspector. Just for convenience' sake, but if it's not then that's fine.
No it doesn't need to be internal
Ok how about I teach you something else
Sure.
you know how you have public GameObject player; at the top now?
Yeah.
you don't need it to be public, you don't need a script to access your coin script and get the player gameobject from it
but you want it to show in your inspector, right?
Yes, so I can assign it.
Well, theres this cool thing called [SerializeField] that you put above private variables to show them in the inspector
so you can make your player gameobject private, and then put [SerializeField] above it so you can edit it in the inspector
"serialize" basically just means "save"
Thanks! I'll study the references a bit. I think I've seen it before.
@proven python I've run into an issue. Using [SerializeField] I've determined that "isInvincible" is true before it's enabled, but it becomes instantly false as soon as the coin is enabled no matter how high I set "invincibilityDuration"
one sec
what IDE are you using
or code editor
Visual Studio
do you know how to make a breakpoint
No, I haven't heard of that yet.
alr
do you have visual studio correctly set up to attach to unity
it should say this at the top of vs
@soft kelp
Yes, I'm looking for it. One issue is that I'm using an older version of VS. I tried downloading the newer releases, but they refused to work on my laptop.
I've researched it a bit. I think I haven't installed "VS Tools for Unity" and I can't find a download. I'm going to try and re-install a newer version of VS. I'll come back to you when it's done.
its in packages
I see them.
(i have visual studio code selected, but download visual studio editor)
I've hit 'update'.
I've managed to install the 2022 version of VS
you can make the game pause when it gets to a part of the script by adding a breakpoint
right click > breakpoint > insert breakpoint
Where should I add it?
right before the coroutine is started
Before the private void or just before the coroutine?
put the breakpoint on BecomeInvincible();
Alright, I've done this
ok now attach vs studio to unity and press play
it should stop right before the coin (is supposed to) become invincible
sorry i was a bit busy
Alright. My character picked up the coin like normal. Is that supposed to happen or did I not start debugging properly?
it should've paused the game at some point
Both my VS and Unity Editor bars are red. Does that mean I attached it correctly?
yes they should be attached
"The breakpoint will not currently be hit. No symbols have been loaded for this document."
player settings -> other settings: change API Compatability to 2.0 (if already 2.0+, change to subset then back to 2.0)
close & re-open visual studio and press build -> clean solution, then build -> rebuild solution
@soft kelp
Wait, I think something happened.
When I tried to open my chest, I got kicked out of the editor.
yes, thats what the breakpoint does
What should I do now?
That's after the coroutine has ended in your script
ohh alright
ok well i think whats happening is that it continues after starting the coroutine
so simply change
// This coroutine 'waits' for 'seconds' amount of seconds
IEnumerator Wait(float seconds) { yield return new WaitForSeconds(seconds); }
to
// This coroutine 'waits' for 'seconds' amount of seconds
IEnumerator Wait(float seconds)
{
yield return new WaitForSeconds(seconds);
isInvincible = false;
}
and get rid of the isinvincible after startcoroutine
I think it works! I'll come back to you if I encounter any issues. I didn't expect this to be so fragile lol.
Also thanks for teaching me about Breakpoints, Coroutines, Debugging etc . I really appreciate it!
Alright. What I've found is that it doesn't matter whether isInvincible is true, it still lets me collect the coin either way. I've found this by setting isInvincible to permanently true and false.
wha
I'm really confused aswell.
oh add && other.CompareTag("Player") to the if statement in ontriggerenter
yeah
make sure your player has a player tag
hey wait
you have isInvincible = false in the if statement
was it like that before
Oh, before it was !ifinvincible and I tried messing around a bit to solve the problem. I changed it around a minute ago.
I changed it to ! and == but no dice. Even with the comparetag, it's still letting me pickup the coin if I collide with it (I'm using SerializeField to check if invincible is true and it is)
Here's everything on the inspectors of my object (Top) and it's parent (Bottom)
"ChestGold" is the script that we're doing now
"TicketCamera" is a billboarding script
"TicketMover" is a magnet script
ok i think i misunderstood how awake works
How my chest works is that it enables the coin when the player interacts with it, in case I forgot to say.
yeah my method would work if your chest instead instantiated a coin
So the script needs to run as soon as the coin is enabled, not as soon as the game starts* I’m not sure that’d be done though
I decided to not instantiate because it takes a lot of resources. Also it’s just one time in a set position.
use OnEnable() instead of awake
void OnEnable ?
yes
I don't think that's the problem though. The game is definitely registering it as "isInvincible", it's just that it doesn't really know or care what "isInvincible" means (somehow).
Should I ask #💻┃code-beginner ? I feel like I'm demanding too much help from you.
Well, @proven python . It works now. I don't know what made it work, but it works. Thanks for sticking with me!