담당기능 소스코드 : 물고기 밥 주기
EatFood.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EatFood : MonoBehaviour
{
[SerializeField] private FishFeed fishEatFood;
[SerializeField] private float moveSpeed = 5f;
private float arrivalDistance = 0.5f;
private float rotationSpeed = 5f;
private GameObject closestFishFeed = null;
private void Update()
{
MoveToPoint();
}
// 가장 가까운 FishFeed로 이동하는 함수
private void MoveToPoint()
{
// FishFeed 태그를 가진 모든 GameObject를 찾음
GameObject[] fishFeeds = GameObject.FindGameObjectsWithTag("FishFeed");
if (fishFeeds.Length == 0) return;
// 가장 가까운 FishFeed를 저장할 변수 초기화
float closestDistance = Mathf.Infinity;
// 모든 FishFeed를 순회하며 가장 가까운 FishFeed 찾기
foreach (GameObject fishFeed in fishFeeds)
{
float distance = Vector3.Distance(transform.position, fishFeed.transform.position);
if (distance < closestDistance)
{
closestFishFeed = fishFeed;
closestDistance = distance;
}
}
// 가장 가까운 FishFeed가 있을 경우
if (closestFishFeed != null)
{
// FishFeed로 향하는 방향과 거리 계산
Vector3 targetPosition = closestFishFeed.transform.position;
Vector3 directionToTarget = targetPosition - transform.position;
float distanceToTarget = directionToTarget.magnitude;
directionToTarget.Normalize();
// 목표 지점에 도착하지 않았을 경우
if (distanceToTarget > arrivalDistance)
{
// 목표 방향으로 회전
Quaternion targetRotation = Quaternion.LookRotation(directionToTarget) * Quaternion.Euler(0, 90, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationSpeed);
// 목표 지점으로 이동
transform.position += directionToTarget * moveSpeed * Time.deltaTime;
}
}
}
}
FishFeedTrough.cs
using Unity.VisualScripting;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.InputSystem;
public class FishFeedTrough : MonoBehaviour
{
[SerializeField] private GameObject feedPrefab;
[SerializeField] private FishFeed fishfeed;
[SerializeField] private GameObject[] gameObjects;
[SerializeField] InputActionProperty BButton;
private int i = 0;
private void Awake()
{
gameObjects = new GameObject[100];
for (int i = 0; i < 100; i++)
{
GameObject gameObject = Instantiate(feedPrefab);
gameObjects[i] = gameObject;
gameObject.SetActive(false);
}
}
private void OnMouseDown()
{
SpawnFeed();
}
public void ClickBButton(InputAction.CallbackContext obj)
{
}
private void SpawnFeed()
{
for (int i = 0; i < 100; i++)
{
gameObjects[i].gameObject.SetActive(true);
}
fishfeed.isActive = true;
//feedPrefab.gameObject.SetActive(true);
}
}
FishFeed.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FishFeed : MonoBehaviour
{
public float jumpForce = 1f;
public float descentSpeed = 0.3f;
public float lifeTime = 20f;
public float unJump = 1f;
private float elapsedTime = 0f;
private float eatingTime = 0.3f;
private Rigidbody rb;
private bool hasJumped = false;
public bool isActive = true;
[SerializeField] private GameObject fishFeedTrough;
private void Start()
{
rb = GetComponent<Rigidbody>();
StartCoroutine(DestroyAfterDelay(lifeTime));
}
private void Update()
{
elapsedTime += Time.deltaTime;
if (elapsedTime >= 0.3f)
{
HasJumped();
}
if (isActive)
{
Jump();
}
}
private void OnCollisionEnter(Collision _collision)
{
if (_collision.gameObject.CompareTag("Fish"))
{
transform.position = fishFeedTrough.transform.position;
gameObject.SetActive(false);
isActive = false;
}
}
private void Jump()
{
Vector3 jumpDirection = Vector3.up;
rb.AddForce(jumpDirection * jumpForce, ForceMode.Impulse);
hasJumped = true;
}
private IEnumerator DestroyAfterDelay(float delay)
{
yield return new WaitForSeconds(delay);
Destroy(gameObject);
}
private IEnumerator StartDescentAfterDelay(float delay)
{
yield return new WaitForSeconds(delay);
hasJumped = true;
}
private void HasJumped()
{
if (hasJumped)
{
rb.velocity = Vector3.down * -descentSpeed;
}
}
}
느낀점 : 팀프로젝트를 수없이 해봤지만 처음부터 끝까지 완벽하게 잘맞아서 한적은 생각해보면 한번도 없다. 아무리 잘맞을거라고 생각하고 마찰이 없을것 같아도 결국은 생긴다
그리고 무엇보다 기획단계에선 반드시 같은 생각을 하고있어야한다.
설령 누군가 이해 못했으면 끝까지 같은 목표를 향하게끔 누군가는 잡아 주어야한다.
그리고 소스코드를 짜면서 오브젝트 폴링 이라는 기법을 알게되어 이부분도 큰 부족이라고 느껴 평소에 공부를 더 많이 해야겠다고 느끼기도 했다. 알고 짜는것과 모르고 짜는것의 차이는 개인적으로 컴퓨터의 리소스 잡아먹는 문제가 있으며 이는 최적화와 직결된다. 평소에 공부를 조금 열심히하고 경험을 쌓아 조금 더 나은 개발자가 되어야겠다.