반응형
사용엔진: Unity 2017.3
요즘 PUN(포톤 유니티 네트워크), Photon(포톤) 이라는 네트워크 솔루션을 활용하여 3D Multiplayer RPG스타일의 게임을 만들고 있다.
일단은 가제목을 Raid로 했는데 나중에 바꿀수도 있을것 같다.
Player의 움직임 스크립트를 짜고있는 와중에 카메라를 기준으로 플레이어의 움직임을 어떻게 구현할까 궁굼했는데
구글링을 열심히 한 결과 Joystick을 활용하여 카메라가 바라보는 기준으로 앞으로 나가겠금 구현을 하는데 성공했다.
혹시 나중에 쓸 수도 있으니 여기에 스크립트 일부를 올려야겠다.
아직 전혀 다듬지 않은 스크립트이기 때문에 나중에 리팩토링하는 시간을 가져야겠다.
-핵심 스크립트
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | void Move(float hor, float ver) { Vector3 targetDirection = new Vector3(hor, 0f, ver); targetDirection = CameraManager.instance.cVC.transform.TransformDirection(targetDirection); targetDirection.y = 0.0f; movement = targetDirection.normalized * playerSpeed * Time.deltaTime; playerRigidbody.MovePosition(transform.position + movement); } void Turn(float hor, float ver) { if (!acting) rotationSpeed = ROTATION_DEFAULT + 1f; else rotationSpeed = ROTATION_DEFAULT - 1f; Vector3 targetDirection = new Vector3(hor, 0f, ver); targetDirection = CameraManager.instance.cVC.transform.TransformDirection(targetDirection); targetDirection.y = 0.0f; Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up); Quaternion newTargetRotation = Quaternion.Lerp(playerRigidbody.rotation, targetRotation, rotationSpeed * Time.deltaTime); playerRigidbody.MoveRotation(newTargetRotation); } | 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 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 108 109 110 111 112 113 114 | using System.Collections; using System.Collections.Generic; using UnityEngine; using Photon.Pun; public class PlayerMovement : MonoBehaviourPun { private Vector3 movement; private Rigidbody playerRigidbody; public float playerSpeed; public float rotationSpeed; private float h; private float v; public bool moving; //basic movement public bool acting; //dash , skill attacks, etc public bool tumbled; //tumbled when hit by enemy public bool alive; private const float SPEED_DEFAULT = 5f; private const float ROTATION_DEFAULT = 4f; private const float ADDFORCE_DEFAULT = 1000f; public static bool controllerActive; void Awake() { playerRigidbody = this.gameObject.GetComponent<Rigidbody>(); } void Start() { controllerActive = true; alive = true; playerSpeed = SPEED_DEFAULT; rotationSpeed = ROTATION_DEFAULT; } void Update() { if (photonView.IsMine == false && PhotonNetwork.IsConnected == true) return; if (controllerActive) { if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) { h = JoystickManager.instance.x; v = JoystickManager.instance.y; } else //for PC in editor { if (JoystickManager.instance.joystickActivation == true) { h = JoystickManager.instance.x; v = JoystickManager.instance.y; } else { h = Input.GetAxisRaw("Horizontal"); v = Input.GetAxisRaw("Vertical"); } } } } void LateUpdate() { if (photonView.IsMine == false && PhotonNetwork.IsConnected == true) return; if (alive && controllerActive) { if (h != 0 || v != 0) { if (!tumbled) { moving = true; Move(h, v); Turn(h, v); } } else { moving = false; } } } void Move(float hor, float ver) { Vector3 targetDirection = new Vector3(hor, 0f, ver); targetDirection = CameraManager.instance.cVC.transform.TransformDirection(targetDirection); targetDirection.y = 0.0f; movement = targetDirection.normalized * playerSpeed * Time.deltaTime; playerRigidbody.MovePosition(transform.position + movement); } void Turn(float hor, float ver) { if (!acting) rotationSpeed = ROTATION_DEFAULT + 1f; else rotationSpeed = ROTATION_DEFAULT - 1f; Vector3 targetDirection = new Vector3(hor, 0f, ver); targetDirection = CameraManager.instance.cVC.transform.TransformDirection(targetDirection); targetDirection.y = 0.0f; Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up); Quaternion newTargetRotation = Quaternion.Lerp(playerRigidbody.rotation, targetRotation, rotationSpeed * Time.deltaTime); playerRigidbody.MoveRotation(newTargetRotation); } } | cs |
-이번 프로젝트에서는 Unity의 Camera로 Cinemachine을 사용했는데 상당히 괜찮은것 같다 (그전에 유니티 5.6에서 작업했을때는 사용불가...)
-기본적인 포톤네트워크 접속 셋팅이 어느정도 된 상태이다. (밑에 동영상을 보면 서버처리 없이 그냥 Local에서 Instantiate한것 처럼 보이는데
사실 네트워크처리에 의해 PhotonNetwork.Instantiate이 된거다 )
반응형
'Game Development > 프로젝트: 레이드' 카테고리의 다른 글
네트워크 RPG게임 'Raid' 개발일지 6 (0) | 2019.01.09 |
---|---|
네트워크 RPG게임 'Raid' 개발일지 5 (0) | 2018.12.23 |
네트워크 RPG게임 'Raid' 개발일지 4 (0) | 2018.12.08 |
네트워크 RPG게임 'Raid' 개발일지 3 (1) | 2018.12.04 |
네트워크 RPG게임 'Raid' 개발일지 2 (0) | 2018.11.30 |