팀프로젝트/R&D

2020.06.10. 조이스틱 + 조이스틱 카메라 회전 / Euler, Mathf.Clamp

dev_sr 2020. 6. 11. 15:20

좌표를 설정할 때

Euler를 써야 우리가 아는 그 좌표값이 나온다!!!!!!! (ex 35도)

rotation.x 이런 거 쓰면 안된다

 

 

Mathf.Clamp(적용할 대상, 최소값, 최대값)

적용할 대상의 최소값과 최대값을 지정한다

 

 

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Center : MonoBehaviour
{
    [SerializeField] private GameObject player;
    [SerializeField] private float speed;
 
    void Start()
    {
 
    }
 
    // Update is called once per frame
    void Update()
    {
        this.transform.position = Vector3.Lerp(this.transform.position, this.player.transform.position + new Vector3(0,3,0), this.speed);
    }
}

 

 

 

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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
 
public class CamStick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
    [SerializeField] private RectTransform rectBackground;
    [SerializeField] private RectTransform rectCamstick;
 
    private float radius;
 
    [SerializeField] private GameObject center;
    [SerializeField] private GameObject camera;
 
    [SerializeField] private float rotateSpeed;
    private bool isTouch;
 
    private float Vx;
    private float Vy;
 
 
    void Start()
    {
        this.radius = rectBackground.rect.width / 2;
    }
 
    // Update is called once per frame
    void Update()
    {
        if (isTouch == true)
        {
            
 
            if(this.Vx > 0.5f || this.Vx < -0.5f)
            {
                this.center.transform.Rotate(Vector3.up, this.Vx * this.rotateSpeed * -1f);
            }
 
            if (this.Vy > 0.5f || this.Vy < -0.5f)
            {
                this.camera.transform.Rotate(Vector3.left, this.Vy * this.rotateSpeed / 2);
                //카메라 x회전값이 35보다 크면  무조건 35
                //카메라 x회전값이 10보다 작으면 무조건 10
 
                var angleX = this.camera.transform.eulerAngles.x;
                var angleY = this.camera.transform.eulerAngles.y;
                var anglez = this.camera.transform.eulerAngles.z;
 
                this.camera.transform.rotation = Quaternion.Euler(Mathf.Clamp(angleX, 10f, 35f), angleY, anglez);
                
                
                    
            }
                       
        }
    }
 
    public void OnDrag(PointerEventData eventData)
    {
        Vector2 value = eventData.position - (Vector2)this.rectBackground.position;
 
        value = Vector2.ClampMagnitude(value, radius);
 
        this.rectCamstick.localPosition = value;
 
        float distance = Vector2.Distance(rectBackground.position, rectCamstick.position) / this.radius;
 
        var direction = value.normalized;
 
        this.Vx = direction.x * distance;
        this.Vy = direction.y * distance;
    }
 
    public void OnPointerDown(PointerEventData eventData)
    {
        this.isTouch = true;
    }
 
    public void OnPointerUp(PointerEventData eventData)
    {
        this.rectCamstick.localPosition = Vector3.zero;
        this.isTouch = false;
    }
}