Let me start by saying I apologize if I am in the wrong forum for this question. Not sure where to post.
I am trying to create an RPG-style Unity game. I am a scripting novice, but I am getting a veeery general idea (and watching a crapload of tutorials!).
One of the features I would like to add to my game is durability to weapons and armor. My question is: how!? lol. Firstly, I need a script for the durability. But I am thinking I would also need a script for object hardness, as well (and I kind of want that, so that way it can be dynamic, like if someone were to hit a rock/building, the durability of their weapon would decrease a bit more than if they were to hit a soft object/body/animal).
As a script noob, I’m not sure how to set any of that up. I almost have an idea, but not really. Any help would be GREATLY appreciated!
RPG’s are a pretty complex place to start. If your anything like me, simpler things just won’t hold your interest though!
First, learn the basics of programming. Just basic tutorials covering the following should suffice as a starting point:
Declaring and using variables
Functions
Classes (just how to declare and use them should be enough for now)
After you have a basic understanding of classes it should be pretty easy for you to figure out how to model a weapon with durability, hardness, and the functions necessary to interact with the objects it strikes.
Disclaimer: This script isn’t the best and I’ve likely misspelled some important words =P
public class Weapon : MonoBehavior
{
public int durability;
public void StrikeObject(GameObject theObject )
{
HardnessScript hScript = theObject.GetComponent<HardnessScript>();
durability = durability - hScript.hardness;
}
}
This is a very basic example. The function, StrikeObject, receives a Unity game object. This can be passed in a number of ways, but you’ll most likely be using colliders on the weapon and the thing being hit to grab the game object. StrikeObject() can be called from the weapon’s built in OnCollison function (every class inheriting from MonoBehavior has this and a number of others, such as Update).
Once the function has a game object, it asks the game object for a hardness script, which it then uses to find the objects hardness and finally reduce the durability by that hardness.
Not sure how much that helps! Honestly, looking into fundamental programming concepts will probably do the most for you at this point.
Thanks Noiz. I had an idea like that, as far as the collision aspect of the script. I do need help with scripting though, because I understand enough to formulate the ideas for what might work, but can’t write the code.
Dont over think it. Durability and health are just a number and a function to add to and remove from that number. Start small and build up to what you want. Better to learn a bit as you go than be handed code and not know how to edit it to do what you want…
I understand the merit in that logic, and I know watching the tutorials I am that I will eventually be able to piece together the code to do it ( maybe, hopefully ). Here I was hoping I guess to get more of an idea for my idea; I’m unsure about the value application ( I don’t know how to create, assign, and connect the numerical value). I know I would have to create a base stat for durability and hardness, and assign it to virtually everything, but not how I would tie the script to an action ( for instance, a weapon striking another object would be the trigger) and how to script a computation ( i.e. if a weapon hit a rock versus a person/animal/whatever, how would I make a script to decrease durability less for softer objects, and conversely how I could tie that in with a strength stat to break things with a low hardness open/apart).
For a script noob with a desire for greatness, this is a bit over my head
But that’s why I’m here :)>
That’s why I said to start small and work up. You can use Noiz’s example as a starting point, but I prefer to keep stuff like health separate of whatever it may be on (but no real wrong answer if it does what you need it to in the end).
I’d start like:
// only really put to public for inspector ease of use... use your favored accessibility type
public float maxDurability = 100.0f; // max durability
public float curDurability; // current durability
void Start(){
curDurability = maxDurability; // start out brand new
}
public void HitSomething(){
curDurability-=5; // take 5 durability away
if(curDurability <= 0){
Debug.Log(gameObject.name + " is broken!"); // just print for now
}
}
Putting this in you weapon along side your current weapon script. in your weapon script just grab a reference to the durability and when you detect a hit… call the hit.
//in weapon script
public Durabiliy myDurability; // assuming your durability class is called Durability
void Start(){
if(!myDurability){
myDurability = gameObject.GetComponent<Durability>(); // if durability script not added in inspector... try to grab it on start
}
//Not sure of your weapon script set up so using "Attack" as a generic function
void Attack(){
// you usual code
// if you detect a hit and try to apply damage.. call durability hit
myDurability.HitSomething();
}
}
if you get it to work… then you build on it as a base. Keep it simple and work up to the features you need and you will understand it all as you go. Diving into the end product will usually cause you to abandon parts or piece together code you don’t understand and cause more headache down the road.
I did a little starter Health for someone here as well:
Read, and that is a very helpful bit of script. Both are actually. I definitely need to bone up on my fundamentals. The RPG is coming together rather well in idea and theory, now I simply have to get to work on it. These comments have helped immensely. Always appreciate the community