Hello guys and thanks for your time. this is my first post but I have been reading this forum for ages and learnt a lot.
My problem is I cannot workout why the animation state arent getting shown to other clients.
I cannot for the life of me work out how to setup void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
using my controller it never works. I am a average at best programmer and have been fighting with this for days.
Here is my controller script, both scripts are attached to player prefab.
using UnityEngine;
using System.Collections;
public class PlayerControl : Photon.MonoBehaviour {
enum CharacterState {
Idle,
WalkingForward,
WalkingBackward,
Running,
Limping
};
public CharacterState _characterState;
private Transform _myTransform; //our cached transform
private CharacterController _controller; //our cached CharacterController
private Vector3 _moveDirection; //This is the direction our character is moving
public float walkSpeed = 1f;
public float runSpeed = 2f;
public float limpSpeed = 0.3f;
public bool _running = false;
public bool _limping = false;
public float gravity = 20f;
//Called before the script starts
public void Awake() {
_myTransform = transform; //cache our transform
_controller = GetComponent<CharacterController>(); //cache our charactercontroller
if (!photonView.isMine) {
GetComponentInChildren<Camera>().enabled = false; // disable the camera of the non-owned Player;
GetComponentInChildren<AudioListener>().enabled = false; // Disables AudioListener of non-owned Player - prevents multiple AudioListeners from being present in scene.
}
_characterState = CharacterState.Idle;
}
// Use this for initialization
void Start () {
_moveDirection = Vector3.zero; //zero our the vector3 we will use for moving our player
}
// Update is called once per frame
void Update () {
if(networkView.isMine) {
if(Input.GetKeyUp(KeyCode.R))
ToggleRun();
if(Input.GetKey(KeyCode.W)) {
if(!_running) {
if(!_limping) {
WalkForward();
} else {
LimpingForward();
}
}
if(_running && !_limping)
RunForward();
if(_limping)
LimpingForward();
} else {
_characterState = CharacterState.Idle;
}
CurrentAnimation();
}
}
public void ToggleRun() {
_running = !_running;
}
public void ToggleLimp() {
_limping = !_limping;
}
void WalkForward() {
// is the controller on the ground?
if (_controller.isGrounded) {
//Feed moveDirection with input.
_moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
_moveDirection = transform.TransformDirection(_moveDirection);
//Multiply it by speed.
_moveDirection *= walkSpeed;
_characterState = CharacterState.WalkingForward;
}
//Applying gravity to the controller
_moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
_controller.Move(_moveDirection * Time.deltaTime);
}
void RunForward() {
// is the controller on the ground?
if (_controller.isGrounded) {
//Feed moveDirection with input.
_moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
_moveDirection = transform.TransformDirection(_moveDirection);
//Multiply it by speed.
_moveDirection *= runSpeed;
_characterState = CharacterState.Running;
}
//Applying gravity to the controller
_moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
_controller.Move(_moveDirection * Time.deltaTime);
}
void LimpingForward() {
// is the controller on the ground?
if (_controller.isGrounded) {
//Feed moveDirection with input.
_moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
_moveDirection = transform.TransformDirection(_moveDirection);
//Multiply it by speed.
_moveDirection *= limpSpeed;
_characterState = CharacterState.Limping;
}
//Applying gravity to the controller
_moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
_controller.Move(_moveDirection * Time.deltaTime);
}
public void CurrentAnimation() {
switch(_characterState) {
case CharacterState.Idle:
IdleAnim();
break;
case CharacterState.WalkingForward:
WalkingForwardAnim();
break;
case CharacterState.WalkingBackward:
WalkingBackwardAnim();
break;
case CharacterState.Running:
RunningAnim();
break;
case CharacterState.Limping:
LimpingAnim();
break;
}
}
public void IdleAnim() {
animation.CrossFade("Idle");
}
public void WalkingForwardAnim() {
animation["Walk"].speed = 1f;
animation.CrossFade("Walk");
}
public void WalkingBackwardAnim() {
animation["Walk"].speed = -1f;
animation.CrossFade("Walk");
}
public void RunningAnim() {
animation.CrossFade("Run");
}
public void LimpingAnim() {
animation.CrossFade("injury on right leg and arm");
}
}
and this is my network script
using UnityEngine;
public class NetworkCharacter : 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)
{
transform.position = Vector3.Lerp(transform.position, this.correctPlayerPos, Time.deltaTime * 5);
transform.rotation = Quaternion.Lerp(transform.rotation, this.correctPlayerRot, Time.deltaTime * 5);
}
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
//We own this player: send the others our data
stream.SendNext((int)PlayerControl.CharacterState);
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else
{
//Network player, receive data
PlayerControl.CharacterState = (PlayerControl.CharacterState)(int)stream.ReceiveNext();
correctPlayerPos = (Vector3)stream.ReceiveNext();
correctPlayerRot = (Quaternion)stream.ReceiveNext();
}
}
}