Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Collision based combat system?

Discussion in 'Scripting' started by GameCode4878, Feb 25, 2016.

  1. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    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?
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    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.
     
  3. GameCode4878

    GameCode4878

    Joined:
    Jan 5, 2016
    Posts:
    173
    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.
     
  4. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    Just reference the script the player has on the weapon.
     
  5. yanuaris

    yanuaris

    Joined:
    Oct 16, 2015
    Posts:
    61
    First you can set its variable like atk, dmg, def.
    Code (CSharp):
    1.  
    2. public class Equipments : MonoBehaviour {
    3. public float atk;
    4. public float def;
    5. public float dmg;
    6. public string name;
    7.  
    8.  
    9. public weapon(string wpname, float wpatk, float wpdef, float wpdmg)
    10. {
    11. name = wpname;
    12. atk = wpatk;
    13. }
    14.  
    https://unity3d.com/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries

    Please take a look at this for further understanding...
    I'm learning about it myself too!
     
    RavenOfCode likes this.
  6. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    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.
     
  7. yanuaris

    yanuaris

    Joined:
    Oct 16, 2015
    Posts:
    61
    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?
     
  8. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    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.
     
    yanuaris likes this.
  9. yanuaris

    yanuaris

    Joined:
    Oct 16, 2015
    Posts:
    61
    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?
     
  10. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    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...
     
    yanuaris likes this.
  11. yanuaris

    yanuaris

    Joined:
    Oct 16, 2015
    Posts:
    61
    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?
     
  12. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    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 .
     
    yanuaris likes this.