총이 나가면 앞에 발사된것 처럼 라인을 그려주는 역할을 하는 LineRenderer를 테스트 해보자
해야할 것
입력시 또는 총일 나갈 시 라인렌더러가 false; 에서 true;가 되면 된다.
---
0. 건을 생성 해주고, 라인렌더러를 달아준다.
1. App(실행만 해주는 곳)과 GunLine(작동하는 걸 구현해주는 곳) 스크립터 만들어 준다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class App : MonoBehaviour
{
public GunLine test;
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
this.test.LineShoot();
}
}
}
먼저 테스트로
App 에서 업데이트에서 마우스 버튼을 누르면 GunLine에 LineShoot 메서드를 소환하도록 한다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunLine : MonoBehaviour
{
private LineRenderer bulletLine;
public void LineShoot()
{
Debug.Log("라인 렌더러를 쏩니다.");
}
}
GunLine 에 LineShoot 메서드를 만들어 준다.
단계별로 접근을 해주는 것이 좋다.
----
2. 라인렌더러 안에 있는 SetPosition() 를 사용하여서 라인을 표현해준다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunLine : MonoBehaviour
{
private LineRenderer bulletLine;
public Transform firePosition;
public int Lenth = 1;
public void Awake()
{
this.bulletLine = GetComponent<LineRenderer>();
}
public void LineShoot()
{
StartCoroutine(this.LineShootImpl());
}
private IEnumerator LineShootImpl()
{
//선에서 정점의 위치를 설정합니다.
//시작점
bulletLine.SetPosition(0, this.firePosition.position);
//끝점
var pos = this.firePosition.localPosition; // pos는 파이어 포지션의 로컬 포지션(월드가 아님)
pos.z = this.firePosition.forward.z * this.Lenth;
bulletLine.SetPosition(1, pos);
bulletLine.enabled = true;
yield return new WaitForSeconds(0.03f);
bulletLine.enabled = false;
}
}
--
private LineRenderer bulletLine;
public Transform firePosition;
public int Lenth = 1;
라인렌더를 가져올 변수를 만든다.
라인이 발사되는 지점을 가져온다 (현재는 건의 파이어 포지션임)
총 발사될 거리를 지정해준다.
public void Awake()
{
this.bulletLine = GetComponent<LineRenderer>();
}
라인렌더를 현재 라인렌더러 컴포넌트로 받아준다.
public void LineShoot()
{
StartCoroutine(this.LineShootImpl());
}
코루틴을 하기 위한 메서드를 만들고
그 안에서 스타트 코루틴을 해준다.
private IEnumerator LineShootImpl()
{
//선에서 정점의 위치를 설정합니다.
//시작점
bulletLine.SetPosition(0, this.firePosition.position);
//끝점
var pos = this.firePosition.localPosition; // pos는 파이어 포지션의 로컬 포지션(월드가 아님)
pos.z = this.firePosition.forward.z * this.Lenth;
bulletLine.SetPosition(1, pos);
bulletLine.enabled = true;
yield return new WaitForSeconds(0.03f);
bulletLine.enabled = false;
}
실제 코루틴 코드를 작성해준다.
★복습 && 중요★
코루틴을 쓰기 위해서는
1.타입은 IEnumerator 여야하고
2.안에서 yield return이 하나 이상이 있어야한다.
bulletLine.SetPosition(0, this.firePosition.position);
라인렌더러의 SetPosition 키워드를 사용해준다.
public void SetPosition(int index, Vector3 position); 식으로 쓰이며
int index 는 점의 번호를 뜻한다.
Vector.Position 은 점이 위치할 위치를 뜻한다.
그래서 위 코드에서는 0번째 점을
위에서 받은 파이어 포지션에 위치를 시켜주었따
//끝점
var pos = this.firePosition.localPosition; // pos는 파이어 포지션의 로컬 포지션(월드가 아님)
pos.z = this.firePosition.forward.z * this.Lenth;
bulletLine.SetPosition(1, pos);
이제 끝점의 위치를 잡아준다.
먼저 pos 변수에 파이어 포지션의 로컬 포지션을 받아준다 (월드가 아닌 포지션을 뜻함// 부모의 포지션을 따름)
pos.z 에 파이어포지션의 앞 방향 (z의 값의 방향 1, -1)에 위에서 설정해준 길이 값(점이 도달한 지점)을 곱해준다.
(그 방향 * 길이)
그리고 라인렌더러의 SetPosition를 써서
1번째 점의 위치를 pos 값으로 넣어준다.
pos는 파이어 포지션에서 z값만 이동한 것이 된다. (길이만큼 곱했으니깐)
bulletLine.enabled = true;
yield return new WaitForSeconds(0.03f);
bulletLine.enabled = false;
그리고 꺼져있는 렌더러를 키고,
yield를 써서 0.03f 를 쉬고 난 후에 프레임을 이어간다.
그리고 다시 렌더러를 꺼준다.
-----
'Unity > 엔진프로그래밍' 카테고리의 다른 글
0415_좀비슈터 단계별 테스트 _ UI만들고, 부딪치면 체력깍이게하기 (0) | 2021.04.15 |
---|---|
0415_좀비슈터 단계별 테스트 _ 움직이고, 부딪치면 충돌 체크 (0) | 2021.04.15 |
0415_좀비슈터 단계별 테스트 _ 자동발사 (0) | 2021.04.15 |
0415_좀비슈터 단계별 테스트 _ IK 사용하여 총에 손 달기 (0) | 2021.04.15 |
0415_좀비슈터 단계별 테스트 _ RayCast 사용 (0) | 2021.04.14 |