Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by NihkValentine · Mar 18, 2017 at 05:17 PM · inventorynpcartificial intelligence

Npc with inventory?

Hey does anyone know how I should start making Npcs and AI have inventory systems? I want everyone to have an inventory, but no image of it. just an imaginary inventory with cash, building material, food, drink, etc.

how do I do this?

thanks

here is the script. add it to an enemy object, preferably a capsule. if you also have a player object in game, you can change his tag to "Enemy" at any time to get chased by these npcs. also insert two gameobjects. one with the tag "Food" and the other with the tag "Drink". npcs will eat or drink whenever their hunger or thirst reaches below 50.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;

public class EnemyAI : MonoBehaviour {

 public float fpsTargetDistance;
 public float enemyLookDistance = 30.0f;
 public float attackDistance = 10.0f;
 public float enemyMovementSpeed;
 public float damping;
 public Transform fpsTarget;
 public Transform food;
 public Transform drink;
 Rigidbody theRigidbody;
 public float playerHealth = 100;
 private float maxplayerHealth = 100;
 private float damage = 8;
 public float thinkTimer = 5f;
 private float thinkTimerStart;
 public float randomUnitCircleRadius = 25f;
 public UnityEngine.AI.NavMeshAgent agent;
 public bool isBusy;
 public AudioSource aSource;
 private int healthReg = 1;
 bool isRegenHealth;
 bool isDrainingHandT;
 public float hunger = 100f;
 private float maxHunger = 100f;
 public float thirst = 100f;
 private float maxThirst = 100f;

 void Start() {
     theRigidbody = GetComponent<Rigidbody>();
     aSource = GetComponent<AudioSource>();
     UpdateHealthBar();
     thinkTimerStart = thinkTimer;
     agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
 }
 void FixedUpdate()
 {
     UpdateHealthBar();
     CheckDeath();
     UpdateHNT();
     UpdateState();
     drink = GameObject.FindWithTag("Drink").transform;
     food = GameObject.FindWithTag("Food").transform;
     fpsTarget = GameObject.FindWithTag("Enemy").transform;
     {
         fpsTargetDistance = Vector3.Distance(fpsTarget.position, transform.position);
     }
     if (fpsTargetDistance < enemyLookDistance)
     {
         lookAtPlayer();
     }
     else
     {
         isBusy = false;
     }
     if (fpsTargetDistance < attackDistance)
     {
         attackPlease();
     }
 }
 private void CheckDeath()
 {
     if (playerHealth <= 0)
     {
         Die();
     }
 }
 private void Die()
 {
     Destroy(GetComponent<Movement>());
     Destroy(GetComponent<EnemyAI>());
 }
 private void UpdateHealthBar()
 {
     if (playerHealth != maxplayerHealth && !isRegenHealth)
     {
         StartCoroutine(RegainHealthOverTime());
     }
     if (playerHealth > maxplayerHealth)
     {
         playerHealth = maxplayerHealth;
     }
 }
 private IEnumerator RegainHealthOverTime()
 {
     isRegenHealth = true;
     while (playerHealth < maxplayerHealth)
     {
         Healthregen();
         yield return new WaitForSeconds(3);
     }
     isRegenHealth = false;
 }
 private IEnumerator HOverTime()
 {
     while (isDrainingHandT == true)
     {
         GetHungry();
         yield return new WaitForSeconds(11);
     }
 }
 private IEnumerator TOverTime()
 {
     while (isDrainingHandT == true)
     {
         GetThirsty();
         yield return new WaitForSeconds(8);
     }
 }
 public void Healthregen()
 {
     playerHealth += healthReg;
 }
 private void UpdateHNT()
 {
     if (hunger > maxHunger)
     {
         hunger = maxHunger;
     }
     if (thirst > maxThirst)
     {
         thirst = maxThirst;
     }
     if (!isDrainingHandT)
     {
         isDrainingHandT = true;
         StartCoroutine(HOverTime());
         StartCoroutine(TOverTime());
     }
     if (hunger <= 0 && (thirst <= 0))
     {
         playerHealth -= Time.deltaTime / damage * 16;
     }
     else if (hunger <= 0 || thirst <= 0)
     {
         playerHealth -= Time.deltaTime / damage * 16;
     }
     if (hunger < 50f)
     {
         eatPlease();
     }
     if (thirst < 50f)
     {
         drinkPlease();
     }
     if (hunger > 50f)
     {
         isBusy = false;
     }
     if (thirst > 50f)
     {
         isBusy = false;
     }
 }
 void OnCollisionEnter(Collision _collision)
 {
     if (_collision.gameObject.tag == "Drink")
     {
         thirst += 45f;
     }
     else
     {
         if (_collision.gameObject.tag == "Food")
         {
             hunger += 30f;
         }
         else
         {
             if (_collision.gameObject.tag == "Enemy")
             {
                 playerHealth -= damage;
                 aSource.Play();
                 {
                     if (playerHealth < 0)
                     {
                         playerHealth = 0;
                     }
                     UpdateHealthBar();
                 }
             }
         }
     }
 }
 public void HealDamage(float heal)
 {
     playerHealth += heal;
     if (playerHealth > maxplayerHealth)
     {
         playerHealth = maxplayerHealth;
     }
     UpdateHealthBar();
 }
 void lookAtPlayer()
         {
     isBusy = true;
         Quaternion rotation = Quaternion.LookRotation(fpsTarget.position - transform.position);
         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
     }
 void attackPlease()
 {
     theRigidbody.AddForce(transform.forward * enemyMovementSpeed);
 }
 public void eatPlease()
 {
     isBusy = true;
     Quaternion rotation = Quaternion.LookRotation(food.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
     theRigidbody.AddForce(transform.forward * enemyMovementSpeed);
 }
 public void drinkPlease()
 {
     isBusy = true;
     Quaternion rotation = Quaternion.LookRotation(drink.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
     theRigidbody.AddForce(transform.forward * enemyMovementSpeed);
 }
 public void UpdateState()
 {
     thinkTimer -= Time.deltaTime;
     if (thinkTimer <= 0)
     {
         Think();
         thinkTimer = thinkTimerStart;
     }
 }
 public void Wander()
 {
     Vector3 newPos = transform.position + new Vector3(Random.insideUnitCircle.x * randomUnitCircleRadius, transform.position.y, Random.insideUnitCircle.y * randomUnitCircleRadius);
     agent.SetDestination(newPos);
 }

 private void Think()
 {
     if(isBusy == false)
     {
         Wander();
     }
     else
     {

     }
 }
 void GetHungry()
 {
     hunger -= 1;
 }
 void GetThirsty()
 {
     thirst -= 1;
 }

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Commoble · Mar 19, 2017 at 04:31 PM 0
Share

What have you tried so far? Can we see your relevant scripts?

avatar image NihkValentine Commoble · Mar 20, 2017 at 09:18 PM 0
Share

ok I added the whole script to the main post. check it out thanks. and sorry unity lol

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

97 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Npc and AI 1 Answer

How to write better AI code? 0 Answers

Inventory problem 0 Answers

Buttons in one panel affecting another. 0 Answers

GetComponent.sprite is not valide. please help 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges