오늘은 총기구현에서 추가로 정확도를 구현했다.
공부 내용
Rotation 값 변경
https://kkln2486.tistory.com/220
Rotation 값 변경
Quaternion 사용 // x축으로 10도, y축으로 20도, z축으로 30도 회전 transform.rotation = Quaternion.Euler(10, 20, 30); eulerAngle 사용 // x축으로 10도, y축으로 20도, z축으로 30도 회전 // 월드좌표 transform.eulerAngles = new
kkln2486.tistory.com
트러블 슈팅
정확도 구현
정조준 상태가 아닐 때, 정확도가 낮아지도록 구현했다. Raycast의 시작점에서 일정한 랜덤값을 더해서 구현했는데, 멀리있는 물체를 쏠 때 보다 가까이 있는 물체를 쏠 때, 정확도가 더 나빠지는 버그가 생겼다.
private void Hit()
{
Vector3 randomRange = new Vector3(Random.Range(-_currentGun.Accuracy, _currentGun.Accuracy), Random.Range(-_currentGun.Accuracy, _currentGun.Accuracy), Random.Range(-_currentGun.Accuracy, _currentGun.Accuracy));
if (Physics.Raycast(Camera.main.transform.position + randomRange, Camera.main.transform.forward, out _hitInfo, _currentGun.Range)) // 카메라 월드좌표
{
GameObject clone = Instantiate(_currentGun.HitEffectPrefab, _hitInfo.point, Quaternion.LookRotation(_hitInfo.normal));
}
}
멀리 있는 물체일 수록 카메라 중앙에서 많이 벗어나지 않기 때문에 이러한 버그가 생긴 것 같다. Raycast의 시작점을 바꾸는 것이 아닌 Raycast의 방향을 바꾸는 방법으로 구현해야 멀리 있는 물체일 수록 정확도를 떨어지게 할 수 있다.
private void Hit()
{
Vector3 randomRange = new Vector3(Random.Range(-_currentGun.Accuracy, _currentGun.Accuracy), Random.Range(-_currentGun.Accuracy, _currentGun.Accuracy), 0);
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward + randomRange, out _hitInfo, _currentGun.Range)) // 카메라 월드좌표
{
GameObject clone = Instantiate(_currentGun.HitEffectPrefab, _hitInfo.point, Quaternion.LookRotation(_hitInfo.normal));
}
}
Raycast 시작점은 MainCamera의 위치 그대로 두고, Raycast의 방향을 수정했다. 처음에는 총탄이 안보일정도로 오차 범위가 엄청나서 코드를 잘못 수정한 줄 알았다. 하지만 _currentGun.Accuracy 값을 0.02 정도로 낮추니 내가 원하는 총의 정확도 오차 범위가 나왔다.
멀리있는 물체일 수록 정확도가 낮아지도록 수정 완료하였다.
오늘의 회고
추가적으로 총기구현에서 정확도를 구현했다. Raycast의 사용방법에 대해서 더 정확하게 알게되었다. 내일은 추가적인 무기를 구현해보겠다.
'Unity_2기 내일배움캠프 TIL' 카테고리의 다른 글
Unity_2기 10주차 (230108) (0) | 2024.01.08 |
---|---|
Unity_2기 9주차 (230105) (0) | 2024.01.05 |
Unity_2기 9주차 (230103) (1) | 2024.01.03 |
Unity_2기 9주차 (230102) (0) | 2024.01.02 |
Unity_2기 8주차 (231229) (1) | 2023.12.29 |