using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestPlayer : MonoBehaviour
{
[SerializeField]
private Transform head;
[SerializeField]
private Transform tposTrans;
private Vector3 tpos;
private Quaternion trot;
[SerializeField]
private float damping = 1f;
private void Start()
{
this.MoveForward();
}
public void MoveForward()
{
Ray ray = new Ray(this.head.position, this.transform.forward);
Debug.DrawRay(ray.origin, ray.direction, Color.green, 5f);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100f))
{
this.tpos = new Vector3(hit.point.x, this.transform.position.y, hit.point.z);
this.tposTrans.position = tpos;
DrawArrow.ForDebug(hit.point, hit.normal, 5f, Color.blue, ArrowType.Solid);
this.trot = Quaternion.LookRotation(hit.normal);
this.StartCoroutine(this.CoMoveForward());
}
}
private IEnumerator CoMoveForward()
{
while(true)
{
var dis = Vector3.Distance(this.transform.position, this.tpos);
if(dis <= 0.1f)
{
break;
}
this.transform.Translate(Vector3.forward * 1f * Time.deltaTime);
yield return null;
}
Debug.Log("move Complete");
this.transform.rotation = this.trot;
}
}
OverlapPlayer
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class OverlapPlayer : MonoBehaviour
{
[SerializeField]
private float radius = 1f;
private Collider[] colls = new Collider[6];
private Dictionary<GameObject, float> dic = new Dictionary<GameObject, float>();
[SerializeField]
private Transform hand;
// Start is called before the first frame update
void Start()
{
int layerMask = 1 << LayerMask.NameToLayer("Gun");
int cnt = Physics.OverlapSphereNonAlloc(this.transform.position, this.radius, colls, layerMask);
Debug.Log(cnt);
foreach (Collider col in colls)
{
Debug.Log(col.gameObject.name);
var dir = col.gameObject.transform.position - this.transform.position;
DrawArrow.ForDebug(this.transform.position, dir, 5f, Color.green, ArrowType.Solid);
this.dic.Add(col.gameObject, dir.magnitude);
col.gameObject.GetComponent<OverlapGun>().distance = dir.magnitude;
}
foreach (var pair in this.dic)
{
Debug.LogFormat("{0} : {1}", pair.Key.name, pair.Value);
}
var val = this.dic.Values.Min();
var obj = this.dic.Where(x => x.Value == val).FirstOrDefault();
Debug.LogFormat("<color=yellow>{0} : {1}</color>", obj.Key, obj.Value);
obj.Key.transform.SetParent(this.hand);
obj.Key.transform.localPosition = Vector3.zero;
obj.Key.transform.localRotation = Quaternion.identity;
}
private void OnDrawGizmos()
{
Gizmos.DrawWireSphere(this.transform.position, this.radius);
}
}
OverlapGun
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class OverlapGun : MonoBehaviour
{
public float distance;
private void OnDrawGizmos()
{
if (distance != 0)
{
GUIStyle labelStyle = EditorStyles.boldLabel;
labelStyle.alignment = TextAnchor.MiddleCenter;
labelStyle.fontSize = 16;
Handles.Label(this.transform.position + (Vector3.up * 0.2f), distance.ToString(), labelStyle);
}
}
}
잘 이해 안되었던 부분
하위오브젝트로 설정해주고 localPosition과 localRotation을 Vector3.zero, Quaternion.identity로 초기화 시켜주기
이게 전혀 뭔지 몰랐는데.. LINQ이다.
많이 써봐야지 익숙해질 것 같다..
'유니티 심화 > 복습' 카테고리의 다른 글
[복습 & 추가] HeroShooter - 몬스터 Indicator.. 잘모르는 것들 (0) | 2023.08.25 |
---|---|
[복습] HeroShooter - Main 만들기 + AngleAxis, Mathf.Atan2, Mathf.Rad2Deg (0) | 2023.08.24 |
[복습] SpaceShooter - 마우스로 캐릭터 이동 (0) | 2023.08.18 |
[복습] SpaceShooter - 줌인, 줌아웃, 캐릭터 이동 (0) | 2023.08.18 |
[복습] SpaceShooter - Idle 오류 (0) | 2023.08.18 |