자습서1(WASD이동, 마우스 포인트 Look, 라인렌더링

Unity/수업내용 2019. 4. 30. 23:49



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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class TestLineRenderer : MonoBehaviour
{
 
    private LineRenderer lineRenderer;
    private Light gunLight;
    private LineRenderer gunLine;
    private Ray shootRay;
    private ParticleSystem particle;
    private float time = 0.2f;
    private float range;
    private int shootableMask;
 
    private void Awake()
    {
        this.lineRenderer = this.GetComponent<LineRenderer>();
        this.gunLight = this.GetComponent<Light>();
        this.gunLine = this.GetComponent<LineRenderer>();
        this.particle = this.GetComponent<ParticleSystem>();
        this.shootableMask = LayerMask.GetMask("Shootable");
 
        this.shootRay = new Ray();
    }
    private void Start()
    {
        this.AttackReady();
    }
 
    private void AttackReady()
    {
        StartCoroutine(this.AttackReadyImpl());
    }
 
    private IEnumerator AttackReadyImpl()
    {
        while (true)
        {
            if (Input.GetMouseButtonDown(0))
            {
                this.OnLight();
                this.OnLine();
                this.OnParticle();
                yield return new WaitForSeconds(time);
                this.DisableEffect();
            }
            else if(Input.GetMouseButtonDown(1))
            {
                this.OnLight();
                this.OnLine2();
                this.OnParticle();
                yield return new WaitForSeconds(time);
                this.DisableEffect();
            }
            yield return null;
        }
    }
 
    private void OnParticle()
    {
        this.particle.Stop();
        this.particle.Play();
    }
 
    private void OnLine()
    {
        this.gunLine.enabled = true;
        //어디서부터 어디까지 그릴꺼냐
        this.gunLine.SetPosition(0this.transform.position);
 
        this.shootRay.origin = transform.position;
        this.shootRay.direction = transform.forward;
        RaycastHit shootHit;
 
        if (Physics.Raycast(this.shootRay.origin, this.shootRay.direction, out shootHit, 100f, this.shootableMask))
        {
            this.gunLine.SetPosition(1, shootHit.point);
            Debug.Log(shootHit.transform.name);
        }
        else
        {
            this.gunLine.SetPosition(1, shootRay.origin + shootRay.direction * 100);
        }
        this.gunLine.SetPosition(0this.transform.position);
    }
 
    private void OnLine2()
    {
        this.gunLine.enabled = true;
        //어디서부터 어디까지 그릴꺼냐
        this.gunLine.SetPosition(0this.transform.position);
 
        this.shootRay.origin = transform.position;
        this.shootRay.direction = transform.forward;
        RaycastHit shootHit;
 
        if (Physics.Raycast(this.shootRay.origin, this.shootRay.direction, out shootHit, 100f, this.shootableMask))
        {
            this.gunLine.SetPosition(1, shootHit.point);
            Debug.Log(shootHit.transform.name);
        }
        else
        {
            this.gunLine.SetPosition(1, shootRay.origin + shootRay.direction * 100);
        }
        this.gunLine.SetPosition(0this.transform.position);
    }
 
    private void OnLight()
    {
        this.gunLight.enabled = true;
    }
 
    private void DisableEffect()
    {
        //이펙트 다 끄기
        this.gunLight.enabled = false;
        this.gunLine.enabled = false;
    }
}
 
cs



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
41
42
43
44
45
46
47
48
49
50
51
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class TestMoveCharacterWASD : MonoBehaviour
{
    private Animator anim;
 
    private void Awake()
    {
        this.anim = GetComponent<Animator>();
    }
 
    private void Start()
    {
        this.Move();
    }
 
    private void Move()
    {
        StartCoroutine(this.MoveImpl());
    }
 
    private IEnumerator MoveImpl()
    {
        while (true)
        {
            float keyX = Input.GetAxisRaw("Horizontal");
            float keyY = Input.GetAxisRaw("Vertical");
 
            Vector3 dir = new Vector3(keyX, 0, keyY).normalized;
            
            this.anim.SetBool("IsWalking", keyX != 0f || keyY != 0f);
            if (dir != Vector3.zero)
            {
                //움직임
                if (!this.anim.GetCurrentAnimatorStateInfo(0).IsName("Move"))
                {
                    this.anim.Play("Move");
                }
                this.transform.position += dir * 6 * Time.deltaTime;
            }
            else
            {
                this.anim.Play("Idle");
            }
            yield return null;
        }
    }
}
 
cs


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
41
42
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class TestLookMouse : MonoBehaviour
{
    private int floorMask;
 
    private void Awake()
    {
        this.floorMask = LayerMask.GetMask("Floor");
    }
 
    private void Start()
    {
        this.TrunCharacter();
    }
 
    private void TrunCharacter()
    {
        StartCoroutine(this.TrunChracterImpl());
    }
 
    private IEnumerator TrunChracterImpl()
    {
        while(true)
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
            RaycastHit rayFloorHit;
            if (Physics.Raycast(ray.origin, ray.direction, out rayFloorHit, this.floorMask))
            {
                var mousePos = rayFloorHit.point - this.transform.position;
                mousePos.y = 0f;
 
                this.transform.rotation = Quaternion.LookRotation(mousePos);
            }
            yield return null;
        }
    }
}
 
cs


'Unity > 수업내용' 카테고리의 다른 글

(1)BGLoop  (0) 2019.05.01
LayerMask  (0) 2019.05.01
<캐릭터 조작>화면 터치, WASD, 조이스틱  (0) 2019.04.25
HUDTest  (0) 2019.04.25
<죽어라돌덩이>  (0) 2019.04.12
: