유니티 동물 FSM만드는법좀

luckyzenith-64 OP

https://github.com/KooSik-Dev/AnimaiGameSave/blob/main/Assets/Scripts/AnimalFSM.cs

이거 만든건데 너무길어서 간략버전이 필요함

luckyzenith-64 OP

enum안쓰고싶은데 어캄? 이해안가는데..

luckyzenith-64 OP
using UnityEngine;

public class 동물 : MonoBehaviour
{
    int State = 0;
    void Update()
    {
        if (State == 0) { idle(); }
        ;
        if (State == 1) { Woking(); }
        ;
        if (State == 2) { Water(); }
        ;
        if (State == 3) { Food(); }
        ;
        if (State == 4) { ReStart(); }
        ;
    }

    void idle()
    {

    }
    void Woking()
    {

    }

    void Water()
    {

    }

    void Food()
    {

    }

    void ReStart()
    {

    }
}
luckyzenith-64 OP

다른 뭐 음식을 찾을떄 태그를 따라가게하면되는데... 그 랜덤한 방항으로 돌려서 그 쪽 방향으로 가게하는거 모르겠음... 그리고 멈쳤다가..

luckyzenith-64 OP

ㄴㄴ 그 워킹은 아에 다른 코드임... 그냥 랜덤한 방향으로 돌리고 바라보는 방향으로가면됨... 그리고 일정시간마다 랜덤으로 숫자가 또바끼는거임... 그 배고픔, 물, 휴식은 나중에 조권을 추가해서 랜덤이아닌 무조권 되게 할꺼라..

luckyzenith-64 OP

그냥 워킹함수에 회전, 그쪽으로 가기 같이 넣는건 힘듬?

luckyzenith-64 OP

코드 더 짧게안됨? 이해안될거 같아서..

luckyzenith-64 OP

코드가 너무긴데... 그냥 회전 함수하나만들고 1번실행후 워킹하는 쪽으로 가야할뜻..

luckyzenith-64 OP

void Woking()
{
transform.position += transform.forward * Speed * Time.deltaTime;
}

void RandomSpin()
{
transform.Rotate(0, Random.Range(-180f, 180f), 0);
}

이런씩으로는 안될려나?

luckyzenith-64 OP

대회용이라 1번시연하고 끝나서 일단 그게 중요하지 않음..

luckyzenith-64 OP

아니 쉽게하고 싶은 제가 그정도 실력은 아님..

luckyzenith-64 OP

using UnityEngine;

public class 동물 : MonoBehaviour
{
public int State = 0;
int Speed = 5;
public float timer = 0f;

bool isSpin = true;
void Update()
{
    timer += Time.deltaTime;

    if (timer > 3f)
    {
        State = Random.Range(0, 2);
        timer = 0f;
        isSpin = true;
    }

    if (State == 0)
    {
        idle();
    }
    else if (State == 1)
    {
        Woking();
    }
    else if (State == 2)
    {
        Water();
    }
    else if (State == 3)
    {
        Food();
    }
    else if (State == 4)
    {
        ReStart();
    }
}

void idle()
{

}
void Woking()
{
    if (isSpin == true)
    {
        RandomSpin();
        isSpin = false;
    }
    transform.position += transform.forward * Speed * Time.deltaTime;
}

void RandomSpin()
{
    transform.Rotate(0, Random.Range(-180f, 180f), 0);
}

void Water()
{

}

void Food()
{

}

void ReStart()
{

}

}
설마 벽 뚥고 가는거 아니지? 콜라이더 있는데

luckyzenith-64 OP

어렵다 걍 장애물 설치안하고 해야지..

luckyzenith-64 OP

어기 뭐 물먹으로 갈떄 자신와 가장 가까면 Water 태그가 달린 오브젝트로 가게 하는 코드중에 쉬운거 있음>? 그 부분 코드만 보여주셈

luckyzenith-64 OP

지금 오브젝트랑 가장 가까운곳을로 가야함.. 그리고 뭐 거의 다도착그런거필요없음... 그냥 그쪽방항으로 가는게중요함... 가까운쪽으로만... 코드 극한으로 줄여줘

luckyzenith-64 OP

아니 극한으로 줄려야함..

luckyzenith-64 OP

void Water()
{
var Water = GameObject.FindGameObjectsWithTag("Water").OrderBy(w => Vector3.Distance(transform.position, w.transform.position)).First();
transform.LookAt(Water.transform);
transform.position += transform.forward * Speed * Time.deltaTime;
}

이렇게는 안됨?

luckyzenith-64 OP

그 에러방지넣으면 제가 뭐가 안되는지 안뜨기떄문에 안쓰는게 저의 습관임.. 에러가 떠야 뭐가 문제인지 알니까 알부러임

luckyzenith-64 OP

해결함, 그 그리고 그거 어게만듬? 맵에 나무가 여러게 있는데 일정 시간 마다 1개를 선택해서 생성하게
using UnityEngine;
using UnityEngine.Rendering.Universal;

public class 랜덤나무 : MonoBehaviour
{
public GameObject[] GiftObject = GameObject.FindGameObjectsWithTag("Tree");
public GameObject targat;

void Update()
{
    targat = Random.Range(0, GiftObject.Length);
    ???
}

}

뭐이런씩으로 하면될려나?

luckyzenith-64 OP

짧고 간략하게 쉬운버전으로좀

luckyzenith-64 OP

using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Rendering.Universal;

public class 랜덤나무 : MonoBehaviour
{
public GameObject GiftObject;
public GameObject[] tree;
public float Timer = 0f;

private void Update()
{
    Timer += Time.deltaTime;
    if (Timer > 3f)
    {
        int index = Random.Range(0, tree.Length);
        Instantiate(GiftObject, tree[index].transform.position, Quaternion.identity);
    }
}

}

luckyzenith-64 OP

using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Rendering.Universal;

public class 랜덤나무 : MonoBehaviour
{
public GameObject GiftObject;
public GameObject[] tree = GameObject.FindGameObjectsWithTag("Tree");
public float Timer = 0f;

private void Update()
{
    Timer += Time.deltaTime;
    if (Timer > 3f)
    {
        int index = Random.Range(0, tree.Length);
        Instantiate(GiftObject, tree[index].transform.position, Quaternion.identity);
        Timer = 0f;
    }
}

}

luckyzenith-64 OP

using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Rendering.Universal;

public class 랜덤나무 : MonoBehaviour
{
public GameObject GiftObject;
public GameObject[] tree;
public float Timer = 0f;

private void Update()
{
    tree = GameObject.FindGameObjectsWithTag("Tree");
    Timer += Time.deltaTime;
    if (Timer > 3f)
    {
        int index = Random.Range(0, tree.Length);
        Instantiate(GiftObject, tree[index].transform.position, Quaternion.identity);
        Timer = 0f;
    }
}

}

luckyzenith-64 OP

어자피 나무 최대 30개라

luckyzenith-64 OP

그거 어게만듬? 인풋트 필드있는데 스트링을을 3글자만 저장할수있게 조권 어게만듬?

luckyzenith-64 OP

그냥 짧을수록 좋으니 수운거 알려줘 입력하고 저장할깨 제한되는거임 걍 실행안되게 3글자여야함 딱

As últimas novidades de 김구식의 AI서버

(C# )

Physics.OverlapSphere .