-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.cs
124 lines (109 loc) · 3.41 KB
/
Player.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
115
116
117
118
119
120
121
122
123
124
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using TMPro;
public class Player : MonoBehaviour, IPunObservable
{
CharacterController characterController;
Transform Cam;
public Animator animator;
bool chatBubbleSync;
public float timerDuration = 5;
public float timerCounter;
PhotonView view;
public TMP_Text PlayerNameDisplay;
public GameObject ChatBubble;
public TMP_Text messageDisplay;
float x;
float z;
public float speed = 3;
Vector3 move;
// Start is called before the first frame update
void Start()
{
timerCounter = timerDuration;
view = GetComponent<PhotonView>();
if (view.IsMine)
{
PlayerNameDisplay.gameObject.SetActive(false); // if you want to enable the player name PlayerNameDisplay.text=NetworkScript.playerName;
characterController = GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
Cam = Camera.main.transform;
}
}
// Update is called once per frame
void Update()
{
if (!view.IsMine)
{
PlayerNameDisplay.transform.parent.LookAt(Camera.main.transform);
return;
}
if (NetworkScript.CheckChatBox.isFocused)
return;
Movement();
CameraSync();
Animations();
chatBubbleFunction();
}
void Movement()
{
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
move = Cam.forward * z + Cam.right * x;
characterController.Move(move * speed * Time.deltaTime);
characterController.Move(Vector3.down * 5 * Time.deltaTime);
}
void CameraSync()
{
Cam.parent.position = transform.position;
transform.rotation = Quaternion.AngleAxis(Cam.parent.rotation.eulerAngles.y, Vector3.up);
}
void Animations()
{
if (x != 0 || z != 0)
animator.SetBool("Motion", true);
else
animator.SetBool("Motion", false);
if (Input.GetKeyDown(KeyCode.Alpha1))
animator.SetBool("Hi",true);
if(Input.GetKeyDown(KeyCode.Alpha2))
animator.SetBool("Dancing", true);
}
void chatBubbleFunction()
{
messageDisplay.transform.localRotation = Quaternion.Euler(0, 180, 0);
if (ChatBubble.activeInHierarchy)
messageDisplay.text = NetworkScript.message;
if (NetworkScript.IsChatBubble)
{
if(timerCounter <= 0)
{
NetworkScript.IsChatBubble = false;
timerCounter = timerDuration;
}
else
{
timerCounter = timerCounter - Time.deltaTime;
}
}
chatBubbleSync = NetworkScript.IsChatBubble;
ChatBubble.SetActive(chatBubbleSync);
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(NetworkScript.playerName);
stream.SendNext(NetworkScript.message);
stream.SendNext(chatBubbleSync);
}
else if (stream.IsReading)
{
PlayerNameDisplay.text = (string)stream.ReceiveNext();
messageDisplay.text = (string)stream.ReceiveNext();
ChatBubble.SetActive((bool)stream.ReceiveNext());
}
}
}