Yang.공부방

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class TestShooting : MonoBehaviour
{
    public LineRenderer gunLine;
    public Button btn;
    private Coroutine routine;
    private float Length = 10f;
    
    private void Start()
    {
        this.shootingTest();
    }
    
    private void shootingTest()
    {
        btn.onClick.AddListener(() => 
        {
            if(this.routine != null)
            {
                StopCoroutine(this.routine);
            }
            this.routine = StartCoroutine(this.Shooting());
        });
    }
 
    private IEnumerator Shooting()
    {
        this.gunLine.SetPosition(0, transform.position);
        this.gunLine.SetPosition(1, transform.forward * this.Length);
 
        this.gunLine.enabled = true;
        yield return new WaitForSeconds(0.3f);
        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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class TestMove : MonoBehaviour
{
    private Vector3 MoveMent;
    public Animator anim;
    private float speed = 10f;
 
    private void Start()
    {
        this.anim.GetComponent<Animator>();
    }
 
    private void MoveTestGetAxisRaw(float h, float v) //int형 반환
    {
        //Debug.LogFormat("h : {0}, v : {1}", h, v);
        this.MoveMent = new Vector3(h, 0, v);
 
        if (h != 0f || v != 0f)
        {
            anim.SetBool("IsWalking"true);
        }
        else
        {
            anim.SetBool("IsWalking"false);
        }
        //var move = MoveMent.normalized * speed * Time.deltaTime; // 방향 * 속도 * 변위
 
        transform.Translate(MoveMent.normalized * speed * Time.deltaTime);  //직접적으로 값을 넣어줄수 있다.
    }
 
    private void MoveTestGetAxis() // float값 반환
    {
        var h = Input.GetAxis("Horizontal");
        var v = Input.GetAxis("Vertical");
 
        Debug.LogFormat("h : {0}, v : {1}", h, v);
 
        this.MoveMent = new Vector3(h, 0, v);
        //this.MoveMent.Set(h, 0, v);
 
        var move = MoveMent.normalized * speed * Time.deltaTime; // 방향 * 속도 * 변위
 
        transform.Translate(move);
    }
 
    void Animation(float h, float v) //원래 제작자가 만든 코드
    {
        bool isWalking = h != 0f || v != 0f;
        anim.SetBool("IsWalking", isWalking);
    }
    
    private void FixedUpdate()
    {
        var h = Input.GetAxisRaw("Horizontal"); //수평(키보드 좌,우 입력(좌-1,우1,안눌림0))
        var v = Input.GetAxisRaw("Vertical"); //수직(위 아래 입력(위1,아래-1,안눌림0))
 
        this.MoveTestGetAxisRaw(h, v);
        //this.MoveTestGetAxis();
        //this.Animation(h, v);
 
        var animPlayingMove = anim.GetAnimatorTransitionInfo(0).IsName("Move");
        var animPlayingIdle = anim.GetAnimatorTransitionInfo(0).IsName("Idle");
 
        Debug.LogFormat("animPlayingMove : {0}", animPlayingMove);
        Debug.LogFormat("animPlayingIdle : {0}", animPlayingIdle);
    }
}
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class TestRotation : MonoBehaviour
{
    public Rigidbody rigiBody;
 
    // Start is called before the first frame update
    public void look()
    {
        var rayPoint = Camera.main.ScreenPointToRay(Input.mousePosition);
        
        RaycastHit HitInfo;
        if(Physics.Raycast(rayPoint, out HitInfo, 100f))
        {
            var dir = HitInfo.point - this.rigiBody.transform.position;
            //내가 찍은 좌표 - 월드공간에서 트랜스폼
            dir.y = 0;
 
            var lookPosition = Quaternion.LookRotation(dir);
 
            this.rigiBody.MoveRotation(lookPosition);
        }
    }
 
    // Update is called once per frame
    void Update()
    {
        this.look();
    }
}
cs