원본 글 : https://doc.photonengine.com/ko-kr/pun/current/demos-and-tutorials/pun-basics-tutorial/player-prefab
이번 파트는 말그대로 플레이어를 만드는 부분인데 원본글에 자세히 설명되어 있으므로 보고 따라하기를 추천
따라하다보면 헬스설정 부분에서
photonView.IsMine 부분에서 에러가 나는데 그때는 MonoBehaviour대신 Photon.PunBehaviour를 상속받으면 된다.
전체 코드
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 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerManager : Photon.PunBehaviour { public GameObject Beams; public float Health = 1f; bool IsFiring; private void Awake() { if(Beams==null) { Debug.LogError("<Color=Red><a>Missing</a></Color> Beams Reference.", this); } else { Beams.SetActive(false); } } // Update is called once per frame void Update () { ProcessInputs(); if(Beams != null && IsFiring != Beams.GetActive()) { Beams.SetActive(IsFiring); } if (Health <= 0f) { GameManager.Instance.LeaveRoom(); } } void ProcessInputs() { if (Input.GetButtonDown("Fire1")) { if (!IsFiring) { IsFiring = true; } } if (Input.GetButtonUp("Fire1")) { if (IsFiring) { IsFiring = false; } } } private void OnTriggerEnter(Collider other) { if(! photonView.isMine) { return; } if(! other.CompareTag("Beam")) { return; } Health -= 0.1f; } private void OnTriggerStay(Collider other) { if (!photonView.isMine) { return; } if (!other.CompareTag("Beam")) { return; } Health -= 0.1f * Time.deltaTime; } } | cs |
이번글도 5번 파트가 그냥 따라 만들면 되는부분이라 6번과 통합하였다.
이번 파트는 캐릭터를 따라다니는 카메라를 만드는 건데 이것도 그냥 보고 따라 만들면 끝...
카메라가 플레이어를 따라다니는 부분은 수학적인 계산이 많아서 완전히 이해하고 쓰지는 않아도 될듯하다.
전체 코드
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 | using System.Collections; using System.Collections.Generic; 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; Transform cameraTransform; bool isFollowing; private float heightVelocity = 0.0f; private float targetHeight = 100000.0f; 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; // Calculate the current & target rotation angles float originalTargetAngle = transform.eulerAngles.y; float currentAngle = cameraTransform.eulerAngles.y; // Adjust real target angle when camera is locked float targetAngle = originalTargetAngle; currentAngle = targetAngle; targetHeight = targetCenter.y + height; // Damp the height float currentHeight = cameraTransform.position.y; currentHeight = Mathf.SmoothDamp(currentHeight, targetHeight, ref heightVelocity, heightSmoothLag); // Convert the angle into a rotation, by which we then reposition the camera Quaternion currentRotation = Quaternion.Euler(0, currentAngle, 0); // Set the position of the camera on the x-z plane to: // distance meters behind the target cameraTransform.position = targetCenter; cameraTransform.position += currentRotation * Vector3.back * distance; // Set the height of the camera cameraTransform.position = new Vector3(cameraTransform.position.x, currentHeight, cameraTransform.position.z); // Always look at the target SetUpRotation(targetCenter); } void Cut() { float oldHeightSmooth = heightSmoothLag; heightSmoothLag = 0.001f; Apply(); heightSmoothLag = oldHeightSmooth; } /// <summary> /// Sets up the rotation of the camera to always be behind the target /// </summary> /// <param name="centerPos">Center position.</param> void SetUpRotation(Vector3 centerPos) { Vector3 cameraPos = cameraTransform.position; Vector3 offsetToCenter = centerPos - cameraPos; // Generate base rotation only around y-axis 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 |
'프로그래밍 공부 > Photon' 카테고리의 다른 글
8. 플레이어 네트워크 인스턴스 생성 (0) | 2018.08.07 |
---|---|
7. 네트워킹을 위한 플레이어 변경 (0) | 2018.08.07 |
3. 게임 씬 / 4. 게임 매니저와 레벨 (0) | 2018.08.06 |
2. 로비 UI (0) | 2018.08.06 |
1. 로비 만들기 (0) | 2018.08.06 |