junhyungPARK78 / chuchuUnity

0 stars 0 forks source link

패들에 공 닿는 위치에 따른 반사각 변경 #13

Closed junhyungPARK78 closed 1 year ago

junhyungPARK78 commented 1 year ago

참고 url https://post.naver.com/viewer/postView.nhn?volumeNo=21636704&memberNo=1085064&searchKeyword=%EB%B2%BD%EB%8F%8C&searchRank=332

junhyungPARK78 commented 1 year ago

OnTriggerEnter2D() 넣어서 로그 찍히게 한 코드가 반응을 안해서 하루 동안 끙끙댐. 알고보니 패들에 연결한 스크립트를 수정한게 아니고 예전 거 놔둔거를 만지고 있어서 반응이 없었던 거였음. 개삽질을...

제대로 패들에 연결되어 있는 스크립트를 수정하니 문제없이 로그 찍히는 거 확인함.

junhyungPARK78 commented 1 year ago

접촉했을 때 접촉한 물체의 정보를 다루는 방법에 대해서

private void OnTriggerEnter2D(Collider2D other)
{
    Debug.Log ($"ball Position.x : {other.transform.position.x}");
}
junhyungPARK78 commented 1 year ago

공 반사에 대한 참고 자료

https://nekojara.city/unity-vector-reflect

junhyungPARK78 commented 1 year ago

스크립트에서 스크립트가 붙어있는 게임 오브젝트를 지정하는 방식

Vector2 myPos = gameObject.transform.position; 과 같이, gameObject를 사용하면 된다.

junhyungPARK78 commented 1 year ago

오브젝트의 부모의 정보를 가져오고 싶을 때

참고 url https://dydvn.tistory.com/48

Vector2 parentPos = gameObject.transform.parent.transform.position;

junhyungPARK78 commented 1 year ago

collision에 접촉한 collision의 오브젝트를 알고 싶을 때

private void OnCollisionEnter2D(Collision2D other)
{
    Rigidbody2D ballRigidBody = other.collider.gameObject.GetComponent<Rigidbody2D>();

Collision2D.collider.gameObject 를 사용한다.

junhyungPARK78 commented 1 year ago

공 반사시키는 코드

Vector2 inDirection = ballRigidBody.velocity; // 입사 벡터 (속도)
Vector2 inNormal = transform.up; // 노말 벡터
Vector2 result = Vector2.Reflect(inDirection, inNormal);

ballRigidBody.velocity = result;

입사각과 반사되는 기물의 각도를 조정하면 패들 위치에 따른 반사각을 조정할 수 있을 듯하다.

입사각을 transform.down 으로 고정하고 노말 벡터의 각도를 패들과 공의 접촉 위치에 따라 바꾸어주면 될 듯하다.

junhyungPARK78 commented 1 year ago

Application.LoadLevel 은 이제 쓰지 말라고 warning 나올 때

SceneManager.LoadScene 를 쓰래더라.

현재 열려있는 scene을 다시 열려면 : Application.LoadLevel(Application.loadedLevel);SceneManager.LoadScene(SceneManager.GetActiveScene().name);

junhyungPARK78 commented 1 year ago

log 등에서 문자를 복수 열로 보여주고 싶을 때

Debug.Log (@$"
====== Ball과 Block 관련 Log 시작 ======
・Break Block
・ball Block Vector = {ballRigidBody.GetVector(other.transform.position)}
====== Log 종료 ======");

@"" 를 사용한다. $"" 와 겸용도 가능하다. : @$"" 의 방법으로.

junhyungPARK78 commented 1 year ago

벡터를 길이 1인 단위 벡터로 만드는 방법

Vector2 startVector = new Vector2(1f, 2f).normalized;

normalized 를 사용하면 같은 방향을 가리키면서 길이가 1인 단위 벡터를 자동으로 만들어 준다.

참고 url : https://fiftiesstudy.tistory.com/249