Merge pull request 'Ai-Creating-customers' (#6) from Ai-Creating-customers into main

Reviewed-on: Mourning-Ravens/UnityBiz#6
This commit is contained in:
Nukie 2024-09-17 22:55:52 +00:00
commit ad6ed4eaba
4 changed files with 82 additions and 0 deletions

55
AI.cs Normal file
View File

@ -0,0 +1,55 @@
using UnityEngine;
using UnityEngine.AI;
using System.Collections;
using static UnityEngine.GraphicsBuffer;
public class AI : MonoBehaviour
{
[SerializeField] private NavMeshAgent agent;
[SerializeField] private int waittime;
[SerializeField] public Transform[] targets;
private int currentTargetIndex;
private bool isWaiting;
private void Start()
{
targets = new Transform[3];
targets[0] = GameObject.Find("Cube (2)").transform;
targets[1] = GameObject.Find("Cube (3)").transform;
targets[2] = GameObject.Find("Cube (4)").transform;
if (targets.Length > 0)
{
currentTargetIndex = 0;
SetNextDestination();
}
}
private void Update()
{
if (!isWaiting && agent.remainingDistance <= agent.stoppingDistance && !agent.pathPending)
{
StartCoroutine(GoToNextTarget());
}
}
private void SetNextDestination()
{
if (targets.Length == 0)
return;
agent.SetDestination(targets[currentTargetIndex].position);
}
private IEnumerator GoToNextTarget()
{
isWaiting = true;
yield return new WaitForSeconds(waittime);
currentTargetIndex++;
SetNextDestination();
isWaiting = false;
}
}

17
Create.cs Normal file
View File

@ -0,0 +1,17 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Create : MonoBehaviour
{
public GameObject[] objectToSpawn;
public int z,y;
// Start is called before the first frame update
void Start()
{
int rand = UnityEngine.Random.Range(0, objectToSpawn.Length);
Instantiate(objectToSpawn[rand], new Vector3(y,0,z), Quaternion.identity);
}
}

View File

10
cos do ai.md Normal file
View File

@ -0,0 +1,10 @@
Bake NavMesh:
Wybierz teren, który ma być powierzchnią do nawigacji (np. Plane).
Przejdź do zakładki Window -> AI -> Navigation.
W sekcji Bake kliknij przycisk Bake, aby wygenerować NavMesh na obiekcie.
Dodaj komponent NavMeshAgent do obiektu AI:
Stwórz nowy obiekt 3D (np. Sphere lub Cube), który będzie agentem AI.
Do tego obiektu dodaj komponent NavMesh Agent (w zakładce Inspector -> Add Component -> wpisz NavMeshAgent).
Podłącz skrypt AI do agenta:
Do obiektu AI dodaj załączony skrypt AI (AI.cs).
W Inspector w polu Agent przypisz komponent NavMeshAgent, a w polu Wait Time ustaw odpowiednią liczbę sekund (np. 2).