1.구현한 내용
- Sphere범위 내 가장 가까운 무기 장착하기
2.code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OverlapPlayer : MonoBehaviour
{
[SerializeField]
private float radius = 1f;
private Collider[] colls = new Collider[6];
[SerializeField]
private Transform trans;
private void Start()
{
int layerMask = 1 << LayerMask.NameToLayer("Gun");
int cnt = Physics.OverlapSphereNonAlloc(this.transform.position, radius, colls, layerMask);
Debug.Log(cnt);
float minDis = 10000f;
GameObject gunGo = null;
foreach(var coll in colls)
{
Debug.Log(coll.gameObject.name);
float dis = Vector3.Distance(coll.transform.position, this.transform.position);
Debug.Log(dis);
if (dis < minDis)
{
gunGo = coll.gameObject;
minDis = dis;
}
}
gunGo.transform.SetParent(this.trans);
gunGo.transform.localPosition = Vector3.zero;
gunGo.transform.localRotation=Quaternion.identity;
}
private void OnDrawGizmos()
{
Gizmos.DrawWireSphere(transform.position, radius);
}
}
3.결과
'K-digital traning > 유니티 심화' 카테고리의 다른 글
[주말과제]UGUI+데이터 연동(1) (0) | 2023.09.10 |
---|---|
HeroShooter에 new Input System 적용 (0) | 2023.09.03 |
[주말과제] HeroShooter stage1까지 (0) | 2023.08.28 |
캐릭터 이동 후 normal(법선벡터)로 회전 (0) | 2023.08.21 |
[주말과제] 궁수의 전설 (0) | 2023.08.21 |