Setting up a battle system

I'm trying to set up a battle system which sounds tough so I'll just show what i've been doing so far

PlayerController.js

if(Input.GetAxis("Fire1"))
{   
    Command("Attack");  //Calls the function Command and passes in Attack
}

function Command(cmd : String )
{
    currentDirection = cmd; // this is a flag so that the animation script knows what to do.
    gameObject.SendMessage("Attack"); //calls the attack function of Commandline.js

}

Commandline.js

function Attack()
{
    //I will calculate how much the attack will be worth using the players stats
    //Once calculated I will send the damage to an enemy collider else nothing happens
    //Should I use a send message here? or a function call that sends the damage to the  
    //Enemy collider. 

}

I probably should have used a on trigger enter function and another box collider that activates for a split second.

but i'm just not sure how I would go about transferring the damage over to the enemy.

First off, your first section needs to be enclosed in an Update function, you can't just have it sitting in the script all by itself.


function Update()
{
    if(Input.GetAxis("Fire1"))
    {   
        Command("Attack");  //Calls the function Command and passes in Attack
    }
}

function Command(cmd : String )
{
    currentDirection = cmd; // this is a flag so that the animation script knows what to do.
    gameObject.SendMessage("Attack"); //calls the attack function of Commandline.js

}

As for retrieving your enemy object... you need to identify it some way, either with a collider or a raycast, or something like that.

All of the objects returned by these detection methods let you access the "other" GameObject in question. Using a RaycastHit, for example, would be `RaycastHit.transform.gameObject`, and for a Collider, you could just use `Collider.gameObject` or `Collision.gameObject` depending on if it's a trigger or not.

Note that the examples above aren't literal... you would actually need to use the object from the function in order to access the GameObject. So, let's pretend that you have a trigger set up. We'll be using OnTriggerEnter and Collider other. I'll get your "Commandline" object as if it were on the other colliding object.


function OnTriggerEnter(Collider other)
{
    Commandline myScript = other.gameObject.GetComponent(Commandline);

    // Now you can call whatever you want on that script, on that gameObject.
    myScript.Attack();
}

Now as for a rig for the controls I have it set to send messages depending on the type of attack. So this is my CommandLine.js:

private var cPlayer : PlayerStatus;
var colliderType : String = "NULL";
var damage : int = 0;
var physicalPercent : float = 0.0;
var magicPercent : float = 0.0;
var elementalPercent : float = 0.0;
var elementalType : String = "NULL";
var statusEffects : String[] = new String[12];
function Awake()
{
    cPlayer = GetComponent(PlayerStatus);
    if(!cPlayer)
        Debug.Log("Cannot connect to player");
}

function Update () {
}

function Attack(){}

function Guard(){}

function Special(){}

function Dash(){}

function PowerSlash(){}

And as for how I would like to handle the damage transfer I elected to try and implement your recent suggestion into another class that will create a specific collider of choice. It will eventually set a size depending on the players stats (Stronger == bigger) in some cases. so heres the stub for the ActionController. The action controller will handle combat centered interaction.

function Update () 
{

}
function OnTriggerEnter(other : Collider)
{
      Opponent = other.gameObject.GetComponent(ActionController);

        Opponent.ApplyDamage("Relative information to be sent to opponent for processing")

}

function Commence(dmg : int, phyDmg : float, magDmg : float, eleDmg : float, eleType : String, stsEfx : String[], colTyp : String)
{
    //I was planning to use this function but I realized that I might not need it. 
    //The original apply damage function will be a two way.
    //However I Was going to use this to create and destroy the correct type of collider.

}

function ApplyDamage(dmg : int, phyDmg : float, magDmg : float, eleDmg : float, eleType : String, stsEfx : String[], colTyp : String)
{
        //This function will send damage to the opponent including how much damage is physical, magical, and elemental. T
        //Then if the attack had status tags applied to it then send the statuses as well. all information will move over to the 
        //player status script and other opperations will take place there. such as resistances to certain types of damage and ailments

}

Instead of sending all those parameters like that, you could create a Struct that contains all the information... it's a whole lot cleaner, code-wise.


struct DamageInformation
{
     var damage : int;
     var physicalDamage : float;
     var magicDamage : float;
     var elementalDamage : float;
     var elementType : String;
     var stsEffects : String[];
     var colType : String;
}

I think that code will work... I don't know any Javascript, I code exclusively in C#, but I think something like that should work. You can then just create a "DamageInformation" object like a normal object, and pass that around instead of using all those values like that. :)

Edit: If that doesn't work (I don't think there are structs in Javascript), you can place this code exclusively in a .cs file, all by itself, and then use the object like you normally would with Javascript.


// C#

using UnityEngine;

struct DamageInformation
{
     public int damage;
     public float physicalDamage;
     public float magicDamage;
     public float elementalDamage;
     public String elementType;
     public String stsEffects;
     public String colType;
}

This is what I did for the damage information struct, I just made a new file and put that in there. For the most part it seems to work just fine. *DamageInformation.j*s (because the c# script failed to communicate)

public var colliderType : String;
public var damage : int;
public var physicalPercent : float;
public var magicPercent : float;
public var elementalPercent : float;
public var elementalType : String;
public var statusEffects : String;

Now when I make a new skill I'll make a new DamageInformation and then edit the variables accordingly to the type of function that I wish to use. This would work for Buffs and Debuffs I should add in a few more lines so that when ever there is some kind of transaction between two objects this is called. Like if I were trading Items with another player or i'm getting money from a battle. Or something of that nature but only for things that are temporary. Like Current HP status effects or something like that. I have to be careful not to make changes to stats such as Str or Def I need to make something different for that.

So for my Command based stubs i'm using something like this... CommandLine.Js

function Attack()
{
    var cDamage = new GetComponent(DamageInformation);
    //Create a new instance of DamageInformation

}

I will edit the new instance and then pass it on to the Command function by calling Calling

Command("Box", cDamage);

The Command function will then create a box collider based on an instantiation call then make a variable equal to whats being passed along. then pass that variable to the apply damage of the opponent IF it has a PlayerStatus (Which I just realized I will also give to NPCs) ActionController.js var attacking : boolean = false; var Damage; function Update () {

}
function OnTriggerEnter(other : Collider)
{

    var Opponent = other.gameObject.GetComponent(PlayerStatus);

        Opponent.ApplyDamage(Damage);

}

function Commence(colTyp : String, dmg : DamageInformation) 
{

    if(attacking)
    {

    }
    Damage = dmg;   
}

Commence will make a collider at runtime and if that collider hits something that contains a player status Then call Apply damage on the other object.

Since the commence function is linked to the command which is also linked to the controller class its (Its Ancestor is the product of an update loop) In theory the collider should work provided that I make it so that the time the control is carried out will determine the colliders life span.

This is my code using your suggestions and Hybridizing them to make a context solution.

What do you think so far?

hello, I would like to create an undertale battle system can someone please tell me how