Прошу совета, относительно недавно начал изучать Фотон, естественно начал с теории и уроков. И вот уже несколько дней бьюсь над задачей, которую в теории могу решить, на практике не очень. В общем, делаю по уроку Marco Polo Tutorial с оффсайта фотона и дошел до момента, когда игрок игрока видит как оба плавно движутся в сети. У меня же при движении игрок видит, что второй дергается, а при остановке возвращается на позицию первого. В теории мне понятно, что обновляется одна и та же позиция при остановке движения у обоих игроков, т.е. тот кто двигался "прилетит" в ту же позицию, где стоял первый.
Код фотона (скрипт на игроке):
Синтаксис:
Используется csharp
using UnityEngine;
public class PlayerPhotonData : Photon.MonoBehaviour {
private Vector3 correctPlayerPos = Vector3.zero; //We lerp towards this
private Quaternion correctPlayerRot = Quaternion.identity; //We lerp towards this
// Update is called once per frame
void Update () {
if (!photonView.isMine){
//Update remote player (smooth this, this looks good, at the cost of some accuracy)
transform.position = Vector3.Lerp(transform.position, correctPlayerPos, Time.deltaTime * 5);
transform.rotation = Quaternion.Lerp(transform.rotation, correctPlayerRot, Time.deltaTime * 5);
}
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
if (stream.isWriting){
// We own this player: send the others our data
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}else{
// Network player, receive data
correctPlayerPos = (Vector3) stream.ReceiveNext();
correctPlayerRot = (Quaternion) stream.ReceiveNext();
}
}
}
public class PlayerPhotonData : Photon.MonoBehaviour {
private Vector3 correctPlayerPos = Vector3.zero; //We lerp towards this
private Quaternion correctPlayerRot = Quaternion.identity; //We lerp towards this
// Update is called once per frame
void Update () {
if (!photonView.isMine){
//Update remote player (smooth this, this looks good, at the cost of some accuracy)
transform.position = Vector3.Lerp(transform.position, correctPlayerPos, Time.deltaTime * 5);
transform.rotation = Quaternion.Lerp(transform.rotation, correctPlayerRot, Time.deltaTime * 5);
}
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
if (stream.isWriting){
// We own this player: send the others our data
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}else{
// Network player, receive data
correctPlayerPos = (Vector3) stream.ReceiveNext();
correctPlayerRot = (Quaternion) stream.ReceiveNext();
}
}
}
И код движения игрока:
Синтаксис:
Используется csharp
using UnityEngine;
public class PlayerControl : Photon.MonoBehaviour {
public float speed; // максимальная скорость корабля в секунду
public float speedDamping; // дампинг скорости
public float rotationSpeed; // максимальный поворот в секунду
public float rotationDamping; // дампинг вращения корпуса
// текущая скорость
private float _shipSpeed;
private Transform playerObj; // оптимизация
private float axisX, axisZ;
public void Start(){
playerObj = transform;
}
public void Update(){
axisZ = Input.GetAxis("Vertical");
axisX = Input.GetAxis("Horizontal");
_shipSpeed = Mathf.Lerp(_shipSpeed, Mathf.Max(axisZ, 0), speedDamping);
playerObj.position += playerObj.forward * _shipSpeed * speed * Time.deltaTime;
playerObj.rotation *= Quaternion.AngleAxis(axisX * rotationSpeed * Time.deltaTime, transform.up);
}
}
public class PlayerControl : Photon.MonoBehaviour {
public float speed; // максимальная скорость корабля в секунду
public float speedDamping; // дампинг скорости
public float rotationSpeed; // максимальный поворот в секунду
public float rotationDamping; // дампинг вращения корпуса
// текущая скорость
private float _shipSpeed;
private Transform playerObj; // оптимизация
private float axisX, axisZ;
public void Start(){
playerObj = transform;
}
public void Update(){
axisZ = Input.GetAxis("Vertical");
axisX = Input.GetAxis("Horizontal");
_shipSpeed = Mathf.Lerp(_shipSpeed, Mathf.Max(axisZ, 0), speedDamping);
playerObj.position += playerObj.forward * _shipSpeed * speed * Time.deltaTime;
playerObj.rotation *= Quaternion.AngleAxis(axisX * rotationSpeed * Time.deltaTime, transform.up);
}
}
И код при создании объекта (дабы управлять своим игроком):
Синтаксис:
Используется csharp
void OnJoinedRoom(){
GameObject player = PhotonNetwork.Instantiate("Ship", Vector3.zero, Quaternion.identity, 0);
PlayerControl controller = player.GetComponent<PlayerControl>();
controller.enabled = true;
}
GameObject player = PhotonNetwork.Instantiate("Ship", Vector3.zero, Quaternion.identity, 0);
PlayerControl controller = player.GetComponent<PlayerControl>();
controller.enabled = true;
}