Search Unity

Health Potion

Discussion in 'Scripting' started by bonden55, Feb 5, 2018.

  1. bonden55

    bonden55

    Joined:
    Dec 30, 2017
    Posts:
    31
    Hello been trying all day to figer out how to make like a Health Potion i have this Scripts so i can make a "Item" this is my scripts,

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. [CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]
    5. public class Item : ScriptableObject {
    6.  
    7.     new public string name = "New Item";
    8.     public Sprite icon = null;
    9.     public bool isDefaultItem = false;
    10.  
    11.     public virtual void Use ()
    12.     {
    13.         // Use the item
    14.         // Something might happen
    15.  
    16.         Debug.Log("Using " + name);
    17.     }
    18.  
    19.     public void RemoveFromInventory ()
    20.     {
    21.         Inventory.instance.Remove(this);
    22.     }
    23. }
    24.  

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class Interactable : MonoBehaviour {
    5.  
    6.     public float radius = 3f;
    7.     public Transform interactionTransform;
    8.  
    9.     bool isFocus = false;
    10.     Transform player;
    11.  
    12.     bool hasInteracted = false;
    13.  
    14.     public virtual void Interact()
    15.     {
    16.         // This method is meant to be overwritten
    17.         // Debug.Log(" Interacting with " + transform.name);
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         if (isFocus && !hasInteracted)
    23.         {
    24.             float distance = Vector3.Distance(player.position, interactionTransform.position);
    25.             if (distance <= radius)
    26.             {
    27.                 Interact();
    28.                 hasInteracted = true;
    29.             }
    30.         }    
    31.     }
    32.  
    33.     public void OnFocused (Transform playerTransform)
    34.     {
    35.         isFocus = true;
    36.         player = playerTransform;
    37.         hasInteracted = false;
    38.     }
    39.  
    40.     public void OnDeFocus()
    41.     {
    42.         isFocus = false;
    43.         player = null;
    44.         hasInteracted = false;
    45.     }
    46.  
    47.     void OnDrawGizmosSelected()
    48.     {
    49.         if (interactionTransform == null)
    50.             interactionTransform = transform;
    51.  
    52.         Gizmos.color = Color.yellow;
    53.         Gizmos.DrawWireSphere(interactionTransform.position, radius);
    54.     }
    55.  
    56. }
    57.  
    and the Pick up

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class ItemPickUp : Interactable {
    5.  
    6.     public Item item;
    7.  
    8.     public override void Interact()
    9.     {
    10.         base.Interact();
    11.  
    12.         PickUp();
    13.     }
    14.  
    15.     void PickUp()
    16.     {
    17.         Debug.Log("Picking up " + item.name);
    18.         bool wasPickedUp = Inventory.instance.Add(item);
    19.  
    20.         if(wasPickedUp)
    21.             Destroy(gameObject);
    22.     }
    23. }
    any one ? I need a Ref to the player health i guess but hmm damn cant figer it out i tryed make a new script and Inherent from ItemPick up but noo, and then i whas like maby i can inharet from both my itempick up and my Characterstats? but i dont know how :/
     
  2. bonden55

    bonden55

    Joined:
    Dec 30, 2017
    Posts:
    31
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class CharacterStats : MonoBehaviour {
    6.  
    7.  
    8.     // HealthBar.
    9.     public Slider healthbar;
    10.    
    11.  
    12.  
    13.     // Player Stats.
    14.     public int maxHealth = 100;
    15.     public int currentHealth { get; private set; }
    16.  
    17.     public Stat damage;
    18.     public Stat armor;
    19.  
    20.     void Awake()
    21.     {
    22.         currentHealth = maxHealth;
    23.         healthbar.value = CalculateHealth();
    24.        
    25.     }
    26.  
    27.  
    28.     void Update()
    29.     {
    30.         if (Input.GetKeyDown(KeyCode.H))
    31.         {
    32.             currentHealth += 15;
    33.         }  
    34.     }
    35.  
    36.     public void TakeDamage (int damage)
    37.     {
    38.         damage -= armor.GetValue();
    39.         damage = Mathf.Clamp(damage, 0, int.MaxValue);
    40.  
    41.         currentHealth -= damage;
    42.         healthbar.value = CalculateHealth();
    43.         Debug.Log(transform.name + " takes " + damage + " damage.");
    44.  
    45.         if (currentHealth <= 0)
    46.         {
    47.             Die();
    48.         }
    49.     }
    50.  
    51.     float CalculateHealth()
    52.     {
    53.         return currentHealth / (float)maxHealth;
    54.     }
    55.  
    56.     public virtual void Die ()
    57.     {
    58.         // Die in some way
    59.         // This meathod is meant to be overwritten
    60.  
    61.         Debug.Log(transform.name + " died.");
    62.     }
    63.  
    64. }
    65.  
     
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Is there somewhere you've defined what a HealthPotion is? Where are you implementing the "Use" function of the Item class?

    Multiple inheritance is not supported in C#. You may want to look into using an Interface instead of a parent class for certain behaviors like Interactable. You can have the player controller look for objects which implement your interface, and call "Interact" on them. That way you won't have every object doing distance checks on the player every frame.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Untested, just for example:
    Code (CSharp):
    1. // your interactable objects implement this:
    2. public interface IInteractable
    3. {
    4.     void Focus();
    5.     void UnFocus();
    6.     void Interact();
    7. }
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Player : MonoBehaviour
    4. {
    5.     public float viewDistance;
    6.     public LayerMask interactableLayers;
    7.  
    8.     private IInteractable focusedObject;
    9.  
    10.     public bool HasFocusedObject
    11.     {
    12.         get { return focusedObject != null; }
    13.     }
    14.  
    15.     private void Update()
    16.     {
    17.         this.CheckViewTarget();
    18.  
    19.         // interaction input
    20.         if(Input.GetKeyDown(KeyCode.Space) && HasFocusedObject)
    21.         {
    22.             focusedObject.Interact();
    23.         }
    24.     }
    25.  
    26.     private void CheckViewTarget()
    27.     {
    28.         Ray ray = Camera.main.ViewportPointToRay(Vector2.one * 0.5f);
    29.         RaycastHit hit;
    30.         if(Physics.Raycast(ray, out hit, viewDistance, interactableLayers))
    31.         {
    32.             IInteractable interactableObject = hit.collider.GetComponent<IInteractable>();
    33.             if(interactableObject != null)
    34.             {
    35.                 FocusObject(interactableObject);            
    36.             }
    37.         }
    38.         else
    39.         {
    40.             UnfocusObject();
    41.         }
    42.     }
    43.  
    44.     private void UnfocusObject()
    45.     {
    46.         if(focusedObject != null)
    47.         {
    48.             focusedObject.UnFocus();
    49.             focusedObject = null;
    50.         }
    51.     }
    52.  
    53.     private void FocusObject(IInteractable target)
    54.     {
    55.         UnfocusObject();
    56.         focusedObject = target;
    57.         target.Focus();
    58.     }
    59. }
    Code (CSharp):
    1. // basic component that implements IInteractable explicitly
    2. public class InteractableObject : MonoBehaviour, IInteractable
    3. {
    4.     public string name;
    5.  
    6.     void IInteractable.Focus()
    7.     {
    8.         Debug.LogFormat("{0} was focused.", name);
    9.     }
    10.  
    11.     void IInteractable.Interact()
    12.     {
    13.         Debug.LogFormat("{0} was interacted with.", name);
    14.     }
    15.  
    16.     void IInteractable.UnFocus()
    17.     {
    18.         Debug.LogFormat("{0} was unfocused.", name);
    19.     }
    20. }
     
    Last edited: Feb 5, 2018
  5. bonden55

    bonden55

    Joined:
    Dec 30, 2017
    Posts:
    31
    But how do i Get my currentHealth that is in my CharacterStats script in my Item Script qus the i can do like currentHealth += 15; ??
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You could do what you're already doing, and on Focus or Interact, pass in the Player object as a parameter.

    Then in the item class, you can do player.GetComponent<CharacterStats>().AddHealth(itemValue).

    Maybe like this:
    Code (CSharp):
    1. // your interactable objects implement this:
    2. public interface IInteractable
    3. {
    4.     void Focus();
    5.     void UnFocus();
    6.     void Interact(GameObject playerObject);
    7. }
    Code (CSharp):
    1. // basic component that implements IInteractable explicitly
    2. public class InteractableObject : MonoBehaviour, IInteractable
    3. {
    4.     public string name;
    5.     public float value;
    6.  
    7.     void IInteractable.Focus()
    8.     {
    9.         Debug.LogFormat("{0} was focused.", name);
    10.     }
    11.  
    12.     void IInteractable.Interact(GameObject player)
    13.     {
    14.         Debug.LogFormat("{0} was interacted with by {1}.", name, player.name);
    15.  
    16.         // get the script that has the health management functions
    17.         CharacterStats stats = player.GetComponent<CharacterStats>();
    18.  
    19.         // if the reference is not null
    20.         if(stats != null)
    21.         {
    22.             // use the health function
    23.             stats.AddHealth(value);
    24.         }
    25.     }
    26.  
    27.     void IInteractable.UnFocus()
    28.     {
    29.         Debug.LogFormat("{0} was unfocused.", name);
    30.     }
    31. }
     
    Last edited: Feb 5, 2018
  7. poglife0612

    poglife0612

    Joined:
    Jun 12, 2013
    Posts:
    2
    has this been solved?