Hi I was just wondering if there was anyone that knew how to make a script that will remove the players health after a certain amount of time has past (so lets say during the night the players health stays normal but during the day they will start to lose health slowly until its night again. kind of like a vampire) but i also need it to stop hurting the player while they are inside a building even if it is during the day. which i thought would require a variable to be changed e.g. inside = true. when the player collides with a box trigger in the door way. If anybody knows how to do this I would be so grateful. ![]()
If youâre doing a timed effect over an indistinct length of time that you can disable and enable at will, sounds like the job for a coroutine. I would recommend studying up on coroutines (theyâre a little tricky), but essentially youâll have this function that has a loop like âwhile(someBool is true){ }â and then inside of that loop youâll do health damage, and then pause for a given length of time like 10 seconds, then the loop will run again, unless the bool is turned to false at which point itâll stop looping and end. You can also just set up an infinite loop with âwhile(true)â and then end the coroutine forcefully from the outside whenever you want it to stop, which is probably what Iâd do.
Wow sounds like some tricky stuff. That puts a downer on the situation because Iâve only just started using unity so I know literally nothing about how to code with it and Iâm not the sort of guy that learns through reading :(. Iâve been using game maker up until now and this would of been easy to do on that ( I could do it in a heart beat). Hmm welp back to the drawing board.
Coroutines are fantastic and I have a kind of fanboyish love for them, so I canât just let you ditch them because theyâre a little tricky. Watch these.
XD how did I know this would happen. Yeah I know about these videos.
How are you keeping track of night and day in the first place? Once you know that, you can use that variable in your code to damage the player. OR, what you could do is have the sun point a ray at the player that is blocked by objects like buildings, so night and inside would keep the player from being hurt.
I havenât even got that far in the game yet because this is like the 3rd day of using the engine ( i donât even know how to make variables yet) and the night and day thing was just an example.
It depends really on how you want everything set up. You could have a script where there is a GameObject (probably a directional light), that could floating above the scene. You could then have a raycast shoot from the directional light and collide with the player. While the hit is colliding with the player, then just reduce health over time slowly. If the ray cast hits something (lets say that itâs day time, and the player is underneath the shade of a cliff), then the player wont take any damage, because the raycast will be colliding with the scene instead of the player.
If you havenât been able to create variables yet, I would recommend learning all you can first before trying to accomplish something like that.
Iâm sorry if I sound mean but I just explained that I know next to nothing about the engine and then you give me a really long example of what I could do but it seems liked you knew I knew nothing because you said it at the end so that also means that you read the part where I said that the night and day thing was just an example. The reason I asked the question so that I could hopefully get someone to explain to me how to do it instead of taking the example I gave as the main point of my question. Once again sorry if I sound mean :(.
We can try to dumb down the explanations of how to do what you want until weâre all blue in the face, but the fact of the matter is that you canât really explain how to make a script in 10 seconds to someone who doesnât know how to make scripts (unless we write it for you, and thatâs not going to happen). What you want isnât something you can create with no programming experience- the explanations are going to sound like Greek (proven by the fact that youâre getting upset over the explanations sounding like Greek). You need to go find and read some C# books, watch as many Unity tutorials you can find, and examine/create a few demo projects, etcâŚ
The problem wasnât with the fact that it âsounds like greekâ because I understand what you guys are trying to say its the lack of knowing how to program it to do that that is the problem. Lysander you actually gave me the best information because it seemed like you actually read my question but it seemed like everyone else just read the example part and thought that was what I wanted. You on the other hand read it and understood what I meant and gave a clear answer and some actual good information because how in the world would I of every stumbled across Coroutines is beyond me. I have had programming experience before (I didnât take 3 years of A level IT for nothing I hope) but as I said before it was with game maker which has its own language which makes all these task 100x easier than it is on unity. (Still canât understand why they donât have global variables).
But anyway the biggest problem I have is that Iâm whats known as a cannibal learner which means I take something someone has already made and strip it for all the parts I need and learn from that. But because it seems like whenever I ask for help the only thing anyone does is say âgo learn itâ which helps no one. Its the what and how that would be helpful. Sorry to be a bit ranty but this kind of thing happens so often that I can never get anything done. Iâm only suppose to be a games designer, not a programmer. Greek would probably be easier to learn.
Start simple. Have a timer & deduct health every so often. When you deduct the health reset the timer. Once you get that working add another timer that starts at the same time that triggers a bool. When the bool is true the health timer runs. When the game timer reaches the point where the player stops losing health set the health timer bool to false.
Ps. Iâm learning game design. As a part of that we are supposed to be able to code (probably not the most efficient code) & also learn basic 3d art. Saying you are a designer & therefore donât need to understand coding is a bit lame.
âCannibal learningâ is fine in theory, but in practice it means that youâre constantly handling scripts that are over your head- they have to be over your head, otherwise you could just write them yourself. That leads to questions about âwhatâs wrong with my script?!â when you inadvertently break it, and so we have to spend 10+ minutes sorting through some rather complex functions (complex because they were made by a professional programmer- some of them we made ourselves, months or years ago) in an effort to solve whatâs often a beginnerâs mistake right in the middle of it. It gets old, fast, and so weâre often not as kind about such things as we probably could be. I would apologize for this, but I think itâs reasonable on our parts to be annoyed as much as it is for others to be annoyed that weâre annoyed. Que sera sera~
Add to that all of the âgame designersâ who just want their million dollar idea to work with no effort, and well⌠Anyways, I wish you luck. As a final note: coroutines are fantastic! (As I said- fanboy).
Becaue they are usually considered bad practice, because of their non-locality: global variables can potentially be modified from anywhere in the program code (unless they reside in protected memory, in a class or rendered read-only by declaring them as constants).
Other than that I agree with wath Lysander said in his posts, and want to add that even if you are a âcannibal learnerâ, reading some books and following/analyzing tutorials will help you greatly, as both have more then enough pieces of sample code you can dig through.
I didnât say I donât need to understand it. I just meant I hate having to do it which is never a good thing when you are trying to learn something in fact it is the main cause of death of learning something. And I am also learning games design and I know you need to learn programming and asset creation because I have had to do all of that and I suck at all of them. But as I said I am a âcannibal learnerâ (its not just a theory its what I was born like which I canât change unless you also want me to stop having aspergers as well) I learn with scraps from other programs. It doesnât mean I just use complex programs that I donât understand. It means that I take them apart and learn what makes what work and then how to write it again later. (didnât say I couldnât write them.) I even made a game using that exact method and because of it I was able to learn programming. (my teacher got annoyed because she didnât understand why I wasnât learning it with the books and talks we had.) but yeah I wanted to still thank Lysander for showing me coroutines because that is going to be really helpful but kinda sadden by the low blow of lumping me in with wannabe game designers who think just having idea is enough. (I have great appreciation for all the different parts of game development I just suck at them all apart from the idea part which is why its so hard when Iâm stuck doing it alone.)
But enough ranting sorry if I have made some people angry or annoyed especially you Lysander because I think youâre such a cool dude. Back to business tedthebug you have had the exact same idea as I had which is kind of what I was hoping someone would suggest because the other stuff they brought up was advanced but the only problem was I didnât know how to go about it because from what I know so far you canât reference variables in different scripts so I donât know how to link the timer, the indoor variable, and the players health all together. I tried to do it by using what I had learnt from the timer and player health tutorials but I just couldnât get it to work because I donât know what the unity/ c# equivalent to global variables are.
Sorry Timelog didnât see your comment as I posted that so donât take it as a reply to your comment but yeah you are right about that which is why I have been looking through tutorials. It was my first instinct but when I couldnât find the answer on there I turns to the forums because you can find exactly what you are looking for from what people know.
mad probably what would be an awesome idea for you, is to go to the asset store, and get some full example projects, which have all their scripts attached.
All code in the game act like any other Unity âLegoâ piece. In that you can just add it as a component, and it works on that object. So maybe try going to an example project. If you type in full project in the Asset Store, or going to the Community section and they have full working examples of games. You can pick and choose all their classes, or just bits of their code, and use them as much as makes you happy.
I mean what youâre talking about is something very simple if youâre just noobing about.
Something as simple as adding this:
public float health = 100;
public bool bIsDaytime = false;
public float damageAmount = 0.1f;
void Update()
{
if(bIsDaytime)
{
health -= damageAmount;
if(health < 0)
{
Destroy(gameObject);
}
}
}
to a script (right click in the Asset folder in the Unity Inspector and click âCreate/C# Scriptâ. Tapping the true/false thing will turn off and on damage to the object you attach it to. But youâd need to look at one example video atleast, just to see where to paste it, and to know that youâd need to delete the other âvoid Update(){}â function that youâd find in a new C# script, and to put the variables (where you give a name of something a type and a amount), at the top of the script.
Iâm guessing you have 0 code in your project, so thatâd be the simplest thing to start with. But I do suggest grabbing an example from the Asset Store and playing around with those. Or the working examples from the video earlier posted in here.
They are right in that global variables can be bad practice. However when used correctly, they can be a clear line between the designer and the developer. Global variables and other things like that can be changed in the inspector before and during the game is playing. Meaning that budding designers can play with the numbers they are given access to by the developers, so they can adjust them to fit the flow of the game. With that end Iâve made these variables global for you to play and understand how it works. I understand both sides of what everyoneâs saying and your point of view. I suggest taking that very noobish code I gave you, learning how to add it to your script, and learning from there.
Once you learn how to do the basics of adding variables, and making if statements, youâll be well on your way.