Collision based combat system?

I am using C#. The player is attatched to an entity that acts as the weapon. The player prefab has an integer that defines the damage the player can deal based on the weapon he is holding. How can I get it to where when an enemy collides with the player’s weapon, it loses health based on the damage value set in the player prefab?

Each frame, you use Physics.Raycast from the blade hilt to the tip, and see if that hit something.

Alternatively, you could have a couple of points on the weapon which you raycast from where it was to where it is, and see any of them hit something.

Raycasting is not what I needed, thanks anyway. What I did need is how can the weapon set its own damage value by getting the damage value from the player. There are two damage values, one for the weapon, one for the player.

Just reference the script the player has on the weapon.

First you can set its variable like atk, dmg, def.

public class Equipments : MonoBehaviour { 
public float atk;
public float def;
public float dmg;
public string name;


public weapon(string wpname, float wpatk, float wpdef, float wpdmg)
{
name = wpname;
atk = wpatk;
}

Please take a look at this for further understanding…
I’m learning about it myself too!

1 Like

On an architectural aspect, you could write a game manager script that oversees every events of the game and decide how to process them. You sword script would send an event to the game mager telling it that it has hit an object, then the game manager would apply the damage to the target.

So, what do you usually use as this omnipotent game manager object?
Some sort of collider or empty object?
Won’t it crash upon overloaded features and variables?

An empty object with your GameManager script attached to it will do the job. In theory, you don’t actually need a “concrete” object in the scene. But that’s how Unity works; everything should be a GameObject.

When the manager grows in complexity, you can always simplify it by splitting it into sub-managers.

1 Like

Hmmm, interesting…otherwise, how would it work then? Like in visual studio?
Do they all run from the main Loop C# script and jumps from one loop to another loop as in scenes?

What do you mean? Visual Studio is just a text editor (over simplification).

Nothing holds from making your manager an object outside the Unity API. But since you’re using Unity is a good practice to do the stuff within the framework. I’m not saying that’s always the best way. But if there is no reason to do it otherwise, just do things as it is expected to be done.

Furthermore, you get the benefit of asset management: you can edit and save your manager parameters using the inspector, save it as a prefab and all that jazz…

1 Like

O…okay ? It’s just a text editor?
Indeed i find Notepad++ a much better comparison…but then…
what makes an exe, an exe? What builds it?

And if i want to make my manager outside Unity API, what is the benefit then?
Does it allow for more control, or perhaps the parallel loop allows for better functionality?
Since the Manager exist outside Unity loop, or it’s 3d space, then what it is?
A C# script that plays on its own with first priority?

A compiler translates source code to binary executable .

I don’t quiet understand what you are asking for.

I suggest you do some more research online about programming and compilation .

1 Like