VioletaBabel

60. Photon Tutorial (6) 본문

BCA/8. Photon
60. Photon Tutorial (6)
Beabletoet 2018. 8. 6. 16:01

카메라가 플레이어를 따라가게 하기 위해

CameraWork.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
//CameraWork.cs
using System.Collections;
using UnityEngine;
 
public class CameraWork : MonoBehaviour {
 
    public float distance = 7.0f;
    public float height = 3.0f;
    public float heightSmoothLag = 0.3f;
    public Vector3 centerOffset = Vector3.zero;
    public bool followOnStart = false;
 
    private Transform cameraTransform;
    private bool isFollowing;
    private float heightVelocity = 0f;
    private float targetHeight = 100000f;
    // Use this for initialization
    void Start () {
        if (followOnStart)
            OnStartFollowing();
    }
 
    private void LateUpdate()
    {
        if (cameraTransform == null && isFollowing)
            OnStartFollowing();
        if (isFollowing)
            Apply();
    }
    public void OnStartFollowing()
    {
        cameraTransform = Camera.main.transform;
        isFollowing = true;
        Cut();
    }
    void Apply()
    {
        Vector3 targetCenter = transform.position + centerOffset;
        float originalTargetAngle = transform.eulerAngles.y;
        float currentAngle = cameraTransform.eulerAngles.y;
        float targetAngle = originalTargetAngle;
        currentAngle = targetAngle;
        targetHeight = targetCenter.y + height;
        float currentHeight = cameraTransform.position.y;
        currentHeight = Mathf.SmoothDamp(currentHeight, targetHeight, ref heightVelocity, heightSmoothLag);
        Quaternion currentRotation = Quaternion.Euler(0, currentAngle, 0);
        cameraTransform.position = targetCenter;
        cameraTransform.position += currentRotation * Vector3.back * distance;
        cameraTransform.position = new Vector3(cameraTransform.position.x, currentHeight, cameraTransform.position.z);
        SetUpRotation(targetCenter);
    }
 
    void Cut()
    {
        float oldHeightSmooth = heightSmoothLag;
        heightSmoothLag = 0.001f;
        Apply();
        heightSmoothLag = oldHeightSmooth;
    }
 
    void SetUpRotation(Vector3 centerPos)
    {
        Vector3 cameraPos = cameraTransform.position;
        Vector3 offsetToCenter = centerPos - cameraPos;
        Quaternion yRotation = Quaternion.LookRotation(new Vector3(offsetToCenter.x, 0, offsetToCenter.z));
        Vector3 relativeOffset = Vector3.forward * distance + Vector3.down * height;
        cameraTransform.rotation = yRotation * Quaternion.LookRotation(relativeOffset);
    }
}
 
cs


그 다음 My Kyle Robot에 컴포넌트로 붙여준 후, Follow On Start를 On 한다. (단, 네트워크가 시작되면 Off 해야 함. 아직은 네트워크 아니니까 On)

Center Offset 프로퍼티는 0,4,0으로 하고 실행시켜본다.

'BCA > 8. Photon' 카테고리의 다른 글

62. Photon Tutorial (8)  (0) 2018.08.07
61. Photon Tutorial (7)  (0) 2018.08.06
59. Photon Tutorial (5)  (0) 2018.08.06
58. Photon Tutorial (4)  (0) 2018.08.06
57. Photon Tutorial (3)  (0) 2018.08.06
Comments