반응형
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerShooting : MonoBehaviour { public Transform gunBarrelEnd; private LineRenderer gunLine; public float range = 100f; private Ray ray = new Ray(); private RaycastHit hit; public void Awake() { //자식에게 있는 컴포넌트를 가져온다. gunLine = GetComponentInChildren<LineRenderer>(); } public void Update() { //랜더러를 지정한 포지션에 위치시킴 gunLine.SetPosition(0, gunBarrelEnd.position); ray.origin = gunBarrelEnd.position; ray.direction = gunBarrelEnd.forward; if (Physics.Raycast(ray, out hit, range)) { // gunLine의 포지션은 Index 0 과 index1 로 두개의 점으로 한개의 선을 만든다. gunLine.SetPosition(1, hit.point); } else { var goalPosition = gunBarrelEnd.position + (gunBarrelEnd.forward * range); gunLine.SetPosition(1, goalPosition); } } } | cs |
반응형
'etc-posts > Unity :: C# 튜토리얼' 카테고리의 다른 글
[유니티C#][기초] 7. 플레이어 슈팅 3 (0) | 2018.07.08 |
---|---|
[유니티C#][기초] 6.플레이어 슈팅 2 (0) | 2018.07.08 |
[유니티 C#][기초] 4. 마우스, 키보드 입력 받기 (0) | 2018.07.08 |
[유니티 C#][기초] 3. 씬에 스크립트로 큐브 100개 생성하기 (0) | 2018.07.08 |
[유니티C#][기초] 2. 특정 오브젝트를 카메라가 따라오가게 하기 (0) | 2018.07.08 |