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;
}
}
What have you tried so far? Can we see your relevant scripts?
ok I added the whole script to the main post. check it out thanks. and sorry unity lol
Your answer
Follow this Question
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