- Home /
Creating random NPC’s with their own stats.
Hello, I’m working on game. I’m really new to Unity so I have a lot of ideas and questions.
Does anyone have ideas of how to achieve this:
Randomly generated NPCs
They each have their own health, energy
Energy reduces when they walk (so they will need to move around the scene)
I wanted the player to be able to click on the NPC and a screen of their stats shows up. And when they click “close” it closes the screen.
If you have some tips on how to carry this out, i would really appreciate. So far I’ve watched a lot of tutorials, but I’m finding it hard understanding what they are doing.
Hi, I would suggest to keep looking at tutorials but very importantly follow them and do what they are doing step by step in your own Unity project. Just replicate all the steps while pausing/resu$$anonymous$$g the video. That should help a lot in understanding, then you can think on how you could accomplish your objectives. It's a tall order for someone to give you an answer that would be more useful to you here than a good tutorial would be.
Randomly generated NPCs - think on what parts of them need to be random, whether it's the position, velocity, or other parameters
Health and energy are tracked as variables in scripts
For movement you will need to understand scripts, most commonly called PlayerController scripts. Unity does a lot for you automatically, you just have to define in C# which direction and how fast they would move. Then you track the distance travelled and subtract some proportionate amount of energy from the energy variable.
For this you should understand Raycasting, where when the player clicks you have Unity draw a straight line and check if you've clicked on an object you care about (the player). Then the stats would pop up in a GUI so read about Unity GUIs.
Great answer!
Would like to add that try to think simplified about complex sounded things, because it may sounds hard "randomly generated NPCs", but behind it is just variables with randomization rules that you declared for them. Also remember very helpful Debug.Log function so when something give you not intended behaviour it's very easy to track your script down with couple of Debug.Log to see if it executes properly or what part of code isn't reached but expected to be.
This was really informative. Thank you. I should definitely rewatch and try to adapt some of the tutorials into my own code. But I really appreciate that you explained in what direction I should go for the bullet points.
Answer by UnityToMakeMoney · May 12, 2020 at 07:15 AM
I believe the best way to achieve this (in terms of stats and not visuals) is to make a NPC script where on Start you can use Random.Range(float min, float max)
for health and energy. You can then have a timer using WaitForSeconds(float time)
then call a method Walk()
which you can make where the NPC moves at a random direction and keep moving for x seconds ot steps (depends on your game). Here is a simple template to help you
//you can choose to make this private or public
private float health;
private float energy;
private float min = 1f;//default
private float max = 100f;//default
private float wait_time = 5f;//default
private void Start(){
//set the health and energy
health = Random.Range(min, max);
energy = Random.Range(min, max);
}
private void Update(){
StartCoroutine(BreakAndWalk());
}
//NPC takes a break before walking again
IEnumerator BreakAndWalk(){
yield new return WaitForSeconds(wait_time);
Walk();
}
//walking method
private void Walk(){
//you fill in here
}
Edit: As for number 4, I never personally used UI in that way, but I am sure someone this platform knows.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
new user very easy c# question regarding syntax 1 Answer
Options UI over all scenes 1 Answer