etc-posts/Unity :: C# 튜토리얼

[유니티 C#][기초] 5.플레이어 슈팅 1

플라즈밍 2018. 7. 8. 18:17
반응형



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


반응형