Hi guys, i would like to ask if someone can handle me here… I hv a player script that handles is base stats, health and so on, i hv a inventory system that pick up items that can be equiped and change stats, but wt i would like to know is how i can do things like this:
i have my maxHealth = 5000 for example, and i want to every time i equipe a item that give me stamina, my maxhealth wil increase like maxHealth + 0.5 (my stamina), same doe damage ( base damage + 0.8(strenght))
the problem is if i use update method it grows over and over again, the other problem is if i use in calstats i pick item i equip it calculates but if i take it from character windows to inventory it canculates again…
if someone can help i appreciate… heres scripts:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Player : HumanoidControl
{
private static Player instance;
public static Player Instance
{
get
{
if(instance == null)
{
instance = GameObject.FindObjectOfType<Player>();
}
return Player.instance;
}
}
public Image healthBar;
public Text healthtext;
public static Transform opponent;
public static Player player;
bool block;
private Animator anim;
private bool attacking;
private bool running;
private bool sendDamage;
//character base status
public int baseIntellect;
public int baseAgility;
public int baseStrength;
public int baseStamina;
//character modified status with equipment
private int intellect;
private int agility;
private int strength;
private int stamina;
public Text statsText;
public GameObject fireballPrebab;
void Awake()
{
player = this;
anim = GetComponent<Animator>();
}
void Start()
{
curHealth = maxHealth;
SetStatus (0, 0, 0, 0);
}
void Update()
{
healthBar.fillAmount = curHealth / maxHealth;
healthtext.text = "Health: " + curHealth + "/" + maxHealth;
Attack();
AnimatorStateInfo animStateInfo = anim.GetCurrentAnimatorStateInfo(0);
if (animStateInfo.IsName("Attack") && !sendDamage && animStateInfo.normalizedTime > 0.36f)
{
//sendDamage = false;
//opponent.GetComponent<Enemy>().GetHit(damage);
float Shot = anim.GetFloat("Shot");
if(!sendDamage && Shot > 0.36f)
opponent.GetComponent<Enemy>().GetHit(damage);
}
if (Input.GetKeyDown (KeyCode.Alpha1))
{
Vector3 pos = Input.mousePosition;
GameObject go = Instantiate(fireballPrebab, transform.position, transform.rotation) as GameObject;
go.transform.position = Vector3.Lerp (transform.position, pos, 10 * Time.deltaTime);
}
}
public void SetStatus(int intellect, int agility, int strength, int stamina)
{
this.intellect = intellect + baseIntellect;
this.agility = agility + baseAgility;
this.strength = strength + baseStrength;
this.stamina = stamina + baseStamina;
statsText.text = string.Format ("Intellect: {0}\nAgility: {1}\nStrength: {2}\nStamina: {3}", this.intellect, this.agility, this.strength, this.stamina);
}
void OnTriggerStay(Collider other)
{
if (other.name == "HealthRegen")
{
curHealth += 10;
}
if (curHealth > maxHealth)
{
curHealth = maxHealth;
}
}
protected override void Attack()
{
if (Input.GetMouseButtonUp (1))
{
if(opponent != null && Vector3.Distance(opponent.position, transform.position) < attackRange)
{
if(!block)
{
sendDamage = true;
transform.LookAt(opponent);
opponent.GetComponent<Enemy>().GetHit(damage);
attacking = true;
anim.SetTrigger("IsAttacking");
block = true;
Invoke("UnBlock", attackSpeed);
}
if(Vector3.Distance(opponent.position, transform.position) < attackRange && opponent != null)
{
attacking = true;
}
else
{
attacking = false;
}
anim.SetBool ("IsAttacking", attacking);
}
}
}
void UnBlock()
{
block = false;
}
}
using UnityEngine;
using System.Collections;
using System;
public class CharacterPanel : Inventory
{
public Slot[] equipmentSlots;
private static CharacterPanel instance;
public static CharacterPanel Instance
{
get
{
if(instance == null)
{
instance = GameObject.FindObjectOfType<CharacterPanel>();
}
return CharacterPanel.instance;
}
}
public Slot WeaponSlot
{
get{ return equipmentSlots[9]; }
}
public Slot OffHandSlot
{
get{ return equipmentSlots[10]; }
}
void Awake()
{
equipmentSlots = transform.GetComponentsInChildren<Slot>();
}
public override void CreateLayout()
{
}
public void EquipItem(Slot slot, ItemScript item)
{
if (item.Item.ItemType == ItemType.MAINHAND || item.Item.ItemType == ItemType.TWOHAND && OffHandSlot.IsEmpty)
{
Slot.SwapItems(slot, WeaponSlot);
}
else
{
Slot.SwapItems (slot, Array.Find (equipmentSlots, x => x.canContain == item.Item.ItemType));
}
}
public override void ShowToolTip(GameObject slot)
{
Slot tmpslot = slot.GetComponent<Slot>();
if (slot.GetComponentInParent<Inventory>().IsOpen && !tmpslot.IsEmpty && InventoryManager.Instance.HoverObject == null)
{
InventoryManager.Instance.visualTextObject.text = tmpslot.CurrentItem.GetToolTip();
InventoryManager.Instance.sizeTextObject.text = InventoryManager.Instance.visualTextObject.text;
InventoryManager.Instance.tooltipObject.SetActive(true);
InventoryManager.Instance.tooltipObject.transform.position = slot.transform.position;
}
}
public void CalcStats()
{
int intellect = 0;
int agility = 0;
int strength = 0;
int stamina = 0;
foreach (Slot slot in equipmentSlots)
{
if(!slot.IsEmpty)
{
Equipment e = (Equipment)slot.CurrentItem.Item;
intellect += e.Intellect;
agility += e.Agility;
strength += e.Strength;
stamina += e.Stamina;
}
}
Player.Instance.SetStatus (intellect, agility, strength, stamina);
}
public override void SaveInventory()
{
string content = string.Empty;
for (int i = 0; i < equipmentSlots.Length; i++)
{
if(!equipmentSlots[i].IsEmpty)
{
content += i + "-" + equipmentSlots[i].Items.Peek().Item.ItemName + ";";
}
}
PlayerPrefs.SetString ("CharPanel", content);
PlayerPrefs.Save ();
}
public override void LoadInventory()
{
foreach (Slot slot in equipmentSlots)
{
slot.ClearSlot();
}
string content = PlayerPrefs.GetString ("CharPanel");
string[] splitContent = content.Split (';');
for(int i = 0; i < splitContent.Length - 1; i++)
{
string[] splitValues = splitContent[i].Split('-');
int index = Int32.Parse(splitValues[0]);
string itemName = splitValues[1];
GameObject loadedItem = Instantiate(InventoryManager.Instance.itemObject);
loadedItem.AddComponent<ItemScript>();
if(index == 9 || index == 10)
{
loadedItem.GetComponent<ItemScript>().Item = InventoryManager.Instance.ItemCountainer.Weapons.Find(x => x.ItemName == itemName);
}
else
{
loadedItem.GetComponent<ItemScript>().Item = InventoryManager.Instance.ItemCountainer.Equipment.Find(x => x.ItemName == itemName);
}
equipmentSlots [index].AddItem(loadedItem.GetComponent<ItemScript>());
Destroy(loadedItem);
CalcStats();
}
}
}
using UnityEngine;
using System.Collections;
public abstract class HumanoidControl : MonoBehaviour
{
public string characterName;
public float attackRange;
public int damage;
public float curHealth;
public float maxHealth;
public float attackSpeed;
void Awake()
{
curHealth = maxHealth;
}
void Start()
{
}
public void GetHit(int playerDamage)
{
curHealth = curHealth - playerDamage;
if (curHealth <= 0)
{
Destroy (gameObject, 2f);
}
}
protected abstract void Attack();
}
PS: the humanoid script handle variables that can be used both by player and enemy, like health and so on.