TIL 0223 게임개발 심화 개인 - 4 / 개인 과제 / Boids 알고리즘 개선

강성원·2024년 2월 26일
0

TIL 오늘 배운 것

목록 보기
41/69

Boids 기능 개선

BoidManager 클래스

객체의 속도, 이동 범위, 최대 이웃 수, 3가지 규칙의 가중치 등을 BoidManager에서 관리할 수 있도록 코드를 수정했다.
(이전 포스트에도 반영돼있음)
-> 인스펙터에서 편하게 가지고 놀 수 있다.

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

public class BoidManager : MonoBehaviour
{
    public static BoidManager Instance;

    public GameObject prefab;

    [Header("Init")]
    public float InstantiateRadius;
    public int number;

    public List<GameObject> Boids = new List<GameObject>();

    [Header("MoveManage")]
    public float cohesionWeight = 1.0f;     // 3규칙 가중치
    public float alignmentWeight = 1.0f;
    public float separationWeight = 1.0f;
    public float moveRadiusRange = 5.0f;    // 활동 범위 반지름
    public float boundaryForce = 3.5f;      // 범위 내로 돌아가게 하는 힘
    public float maxSpeed = 2.0f;
    public float neighborDistance = 3.0f;   // 이웃 탐색 범위
    public float maxNeighbors = 50;         // 이웃 탐색 수 제한

    private void Awake()
    {
        Instance = this;

        for (int i = 0; i < number; ++i)
        {
            Boids.Add(Instantiate(prefab, this.transform.position + Random.insideUnitSphere * InstantiateRadius, Random.rotation));
        }
    }
}

Cohesion 조정 효과

응집력을 조정한다.

Separation 조정 효과

분리되는 힘을 조정한다.

최대 속도 조정 효과

Boid.LimitMoveRadius 메서드

LimitMoveRadius는 객체의 이동 범위를 제한해준다.

현재 객체 위치에 Vector3.zero를 빼주는 이유는 나중에 객체를 원하는 위치에서 떠돌게 하고 싶을 때 Vector3.zero 자리에 벡터 값을 넣어주기 위함이다.

나중에 까먹을까봐 넣어놨다.

private Vector3 LimitMoveRadius()
{
    Vector3 returnVector = Vector3.zero;
    if (spawner.moveRadiusRange < this.transform.position.magnitude)
    {
        returnVector +=
            (this.transform.position - Vector3.zero).normalized *
            (spawner.moveRadiusRange - (this.transform.position - Vector3.zero).magnitude) *
            spawner.boundaryForce;
        // 원점->boid 방향 x -(boid가 벗어난 정도) x 힘 x 델타타임

        returnVector.Normalize();
    }

    return returnVector;
}

깃허브
https://github.com/ChocoMucho/Boids-Jump

profile
개발은삼순이발

0개의 댓글