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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Creating a stat system that increases stats based on actions done

Discussion in 'Scripting' started by ferellion, Apr 17, 2018.

  1. ferellion

    ferellion

    Joined:
    Mar 15, 2018
    Posts:
    2
    Hey there, my friends and I are making an RPG that is based around real time combat, we are not doing the standard stat system that rpgs use however, we're using one that increases a specific stat based on what was done, IE: player attacks with a sword and strength increases, they get hit enough and hp increases, they sprint and dodge enough, their stamina and agility increases, etc... But I cannot find a way to write a script for this, we don't have absolutely everything in terms of other scripts but we have some which should allow me to make the script at least do something for one or two of the stats. I have 2 scripts to show you and they are the attacking and the moving one which controls running and I was wondering if I could reference them in the stat script and if I could then how would I reference them to make whenever they're true to increase a specific stat.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerAttack : MonoBehaviour {
    6.  
    7.         public int damagePerAttack = 3;                  // The damage inflicted by each bullet.
    8.         public float timeBetweenAttacks = 1f;        // The time between each shot.
    9.     private Animator attackanim;
    10.     private float timer = 0f;
    11.     public bool attack = false;
    12.     private bool gavetheD;
    13.     PlayerHealth alive;
    14.     public float knockback;
    15.         //public float range = 100f;                      // The distance the gun can fire.
    16.     //public Object weapon;
    17.     //public Collider2D weaponattack;
    18.     // A timer to determine when to fire.
    19.         //Ray shootRay;                                   // A ray from the gun end forwards.
    20.         //RaycastHit shootHit;                            // A raycast hit to get information about what was hit.
    21.         //int shootableMask;                              // A layer mask so the raycast only hits things on the shootable layer.
    22.         //ParticleSystem gunParticles;                    // Reference to the particle system.
    23.         //LineRenderer gunLine;                           // Reference to the line renderer.
    24.         //AudioSource gunAudio;                           // Reference to the audio source.
    25.         //Light gunLight;                                 // Reference to the light component.
    26.         //float effectsDisplayTime = 0.2f;                // The proportion of the timeBetweenBullets that the effects will display for.
    27.     void Start(){
    28.         alive = GetComponentInParent<PlayerHealth> ();
    29.         attackanim = GetComponent<Animator> ();
    30.     }
    31.     void FixedUpdate()
    32.     {
    33.         if (timer >= 0) {
    34.             timer -= Time.deltaTime;
    35.             attackanim.SetBool ("Attacking", false);
    36.         } else {
    37.             attack = false;
    38.         }
    39.         //Debug.Log (attackanim.GetCurrentAnimatorStateInfo (0).Equals("Attacking"));
    40.             //Debug.Log ("maybe");
    41.  
    42.     }
    43.  
    44.     void Update ()
    45.     {
    46.         //timer += Time.deltaTime;
    47.             // Add the time since Update was last called to the timer.
    48.  
    49.             // If the Fire1 button is being press and it's time to fire...
    50.             /*if(Input.GetButtonDown ("Fire1"))
    51.             {
    52.                 // ... shoot the gun.
    53.                 Attack ();
    54.             }*/
    55.         if (alive.currentHealth > 0) {
    56.             if (Input.GetKeyDown (KeyCode.Space) && !attack && attackanim.GetBool ("Attacking") == false) {
    57.                 attack = true;
    58.                 attackanim.SetBool ("Attacking", true);
    59.                 timer = 1f;
    60.                 gavetheD = false;
    61.             }
    62.         }
    63.  
    64.             // If the timer has exceeded the proportion of timeBetweenBullets that the effects should be displayed for...
    65.             /*if(timer >= timeBetweenAttacks * effectsDisplayTime)
    66.             {
    67.                 // ... disable the effects.
    68.                 DisableEffects ();
    69.             }*/
    70.         }
    71.  
    72.         /*public void DisableEffects ()
    73.         {
    74.             // Disable the line renderer and the light.
    75.             gunLine.enabled = false;
    76.             gunLight.enabled = false;
    77.         }
    78.         */
    79.  
    80.     void OnTriggerStay2D(Collider2D other){
    81.         if (other.gameObject.tag == "Enemy" && attack && !gavetheD) {
    82.             other.gameObject.GetComponent<EnemyHealth> ().TakeDamage (damagePerAttack);
    83.             Vector2 wack = other.gameObject.transform.position - transform.position;
    84.             wack = wack.normalized;
    85.             other.gameObject.GetComponent <Rigidbody2D>().AddForce(wack * knockback);
    86.             gavetheD = true;
    87.         }
    88.     }
    89. }
    Now the moving script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerControl : MonoBehaviour
    7. {
    8.  
    9.     public float speed;
    10.     Vector2 move;
    11.     private Animation anim;
    12.     public float attackTime;
    13.     private float attackTimeCounter;
    14.     private float accel = 0;
    15.     //This is meant to allow the player to control the character using WASD or a control pad.
    16.     PlayerHealth alive;
    17.     private float x;
    18.     private float y;
    19.     Rigidbody2D player;
    20.     public Slider staminaslider;
    21.     public int stamina;
    22.     public int currentstamina;
    23.     private float stamtimer = 0f;
    24.     public bool ismove = false;
    25.     public bool isrun = false;
    26.     void Start(){
    27.         alive = GetComponent<PlayerHealth>();
    28.         player = GetComponent<Rigidbody2D> ();
    29.         staminaslider.maxValue = stamina;
    30.         currentstamina = stamina;
    31.     }
    32.     private void FixedUpdate(){
    33.         if (ismove) {
    34.             player.AddForce (move);
    35.             //player.MovePosition(new Vector2(player.position.x + move.x * accel * Time.deltaTime, player.position.y + move.y * accel * Time.deltaTime));
    36.             if (accel < speed)
    37.                 accel += speed / 5f;
    38.         }
    39.         else if (accel > 0)
    40.             accel -= speed / 5f;
    41.  
    42.  
    43.         if (Input.GetKey (KeyCode.LeftShift) && currentstamina > 0) {
    44.             currentstamina -= 1;
    45.             staminaslider.value = currentstamina;
    46.             stamtimer = 0f;
    47.         } else if (currentstamina < stamina && stamtimer > 2f) {
    48.             currentstamina ++;
    49.             staminaslider.value = currentstamina;
    50.             //stamtimer -= 0.5f;
    51.         } else
    52.             stamtimer += 0.02f;
    53.  
    54.     }
    55.     private void Update()
    56.     {
    57.         if (Input.GetKey (KeyCode.LeftShift) && alive.currentHealth > 0 && currentstamina > 0) {
    58.             move = new Vector2 (Input.GetAxis ("Horizontal") * accel * 1.5f, Input.GetAxis ("Vertical") * accel * 1.5f);
    59.         } else if (Input.GetAxis ("Horizontal") != 0 || Input.GetAxis ("Vertical") != 0 && alive.currentHealth > 0) {
    60.             move = new Vector2 (Input.GetAxis ("Horizontal") * accel, Input.GetAxis ("Vertical") * accel);
    61.             ismove = true;
    62.             //player.AddForce(move);
    63.             //player.transform.Translate (move.x, move.y, 0);
    64.             //if (move.x > 0 || move.x < 0)
    65.             //transform.Translate (move.x, 0, 0);
    66.             //if (move.y > 0 || move.y < 0)
    67.             //transform.Translate (0, move.y, 0);
    68.         } else
    69.             ismove = false;    }
    70. }
    71.  
    Is it possible to reference something in these to accomplish what I want, or am I going about this all wrong?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Where's your stat script wit the stats you want to modify? I'd just call a method on the stat script indicating you've just done some action that may affect stats, and have the stat script then update the appropriate stat.
     
  3. ferellion

    ferellion

    Joined:
    Mar 15, 2018
    Posts:
    2
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterStats : MonoBehaviour
    6. {
    7.     public int strength = 5;               //This is setting the initial strength.
    8.     public int intelligence = 5;           //Setting int.
    9.     public int agility = 5;                //Setting agility.
    10.     public int hp;                         //Making an int variable for hp and making = to health.currentHealth
    11.     public int stam;
    12.     PlayerControl stamina;
    13.     PlayerHealth health;
    14.     PlayerAttack attacking;
    15.     public bool isAttack = false;
    16.  
    17.     private void Start()
    18.     {
    19.         attacking = GetComponentInParent<PlayerAttack>();
    20.         stamina = GetComponentInParent<PlayerControl>();
    21.         health = GetComponentInParent<PlayerHealth>();
    22.     }
    23.  
    24.     private void IncreaseStat()
    25.     {
    26.         if (attacking.attack == true || Input.GetKeyDown(KeyCode.Space))
    27.         {
    28.             isAttack = true;
    29.  
    30.             if (isAttack == true)
    31.             {
    32.                 strength += 1;
    33.             }
    34.         }
    35.  
    36.         if (stamina.ismove == true && stamina.currentstamina == 0)
    37.         {
    38.             stamina.stamina += 1;
    39.             agility += 1;
    40.         }
    41.  
    42.         if (health.damaged == true)
    43.         {
    44.             hp = health.startingHealth;
    45.             hp += 1;
    46.         }
    47.     }
    48. }
    Here was what my first script was like, I just now tried using playerprefs but haven't gotten anywhere with that quite yet.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Stats : MonoBehaviour {
    6.  
    7.     string strength = "Strength";
    8.     string intelligence = "Intelligence";
    9.     string agility = "Agility";
    10.     string hp = "Health";
    11.     string stam = "Stamina";
    12.  
    13.     public int str;
    14.     public int intel;
    15.     public int agi;
    16.     public int health;
    17.     public int stamina;
    18.  
    19.     // Use this for initialization
    20.     void Start ()
    21.     {
    22.         PlayerPrefs.SetFloat(strength, str);
    23.         PlayerPrefs.SetFloat(intelligence, intel);
    24.         PlayerPrefs.SetFloat(agility, agi);
    25.         PlayerPrefs.SetFloat(hp, health);
    26.         PlayerPrefs.SetFloat(stam, stamina);
    27.     }
    28.    
    29.  
    30. }
    31.