2D 총게임 반동 표시

 


보시다시피 반동을 표시하는 간단한 indicator( 지시계 ) 다.


일단 만드는 공식은 간단하다


















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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class MouseIndicator : MonoBehaviour
{
    LineRenderer lineRenderer;
    [SerializeField]FireArm fireArm;
    [Range(6,60)]
    public int resolution = 100;
    public float width = 1f;
 
    private void Start() {
        lineRenderer = GetComponent<LineRenderer>();
        lineRenderer.loop = true;
    }
 
    private void Update() {
        VisualizeRecoil(fireArm);
    }
    public void VisualizeRecoil(FireArm fireArm)
    {
        
        Vector2 mousePos = Camera.main.ScreenToWorldPoint(Mouse.current.position.ReadValue());
        // Mouse.current.position.ReadValue()는 Input.mousePosition과 하는 일이 같다.
        float distance = Vector2.Distance(mousePos, fireArm.transform.position);
        float radian = fireArm.curRecoilAngle * Mathf.Deg2Rad;
        // 유니티의 Mathf.Sin,Cos,Tan은 Deg이 아닌 Radian의 값을 받기에 Mathf.Deg2Rad로 바꿀 필요가 있다,
        // 이게 싫다면 float radian = Deg * Mathf.PI / 180; 로 대신해도 된다.
        
        float radius = Mathf.Tan(radian) * distance;
        DrawCircle(mousePos, radius);
    }
 
    void DrawCircle(Vector3 mousePosition,float radius)
    {
        lineRenderer.startWidth = width;
        lineRenderer.endWidth = width;
        lineRenderer.loop = true;
        lineRenderer.positionCount = resolution;
        float theta = 2f * Mathf.PI / resolution;
        float angle = 0;
        for(int i =0; i < resolution; i++)
        {
            float x = radius * Mathf.Cos(angle);
            float y = radius * Mathf.Sin(angle);
 
            Vector3 newPosition = new Vector3(x, y, 0+ mousePosition;
            lineRenderer.SetPosition(i, newPosition);
            angle += theta;
        }
    }
}
cs

댓글

이 블로그의 인기 게시물

Simple Stupid Funnel 후기