At least to me, it is an advanced question. Im trying to smooth out the choppyness of my game when connected through photon, i got everything to work properly, the only thing is the stutter. i know its because its sending out coordinates every frame rather than velocity/speed ect. Im trying to send out the value for variable “speed” (found in the “void update” part) in the “void OnPhotonSerializeView” section, but it wont recognize it because its in “Void Update”. how do i make it recognize “speed” without ruining the algorithem? THANKS in advance.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (CharacterController))]
public class Movement : Photon.MonoBehaviour {
#region UpdateVariables
public float movementspeed = 5.0f;
public float Mousesensitivity = 5.0f;
public float updownLimit = 60.0f;
float verticalrotation = 0;
float verticalvelocity = 0;
public float jumpspeed = 5.0f;
CharacterController charactercontroller;
#endregion
//I added regions so i can see only what i'm working on if you dont like it just expand them all but don't get rid of them.
void Start () {
Screen.lockCursor = true;
charactercontroller = GetComponent<CharacterController>();
}
void Update ()
{
//i added this if statement so that the player can only controll his own prefab clone.
if (photonView.isMine){
//this is the rotation of my "player"
float rotX = Input.GetAxis("Mouse X") * Mousesensitivity ;
transform.Rotate(0, rotX, 0);
//this is the movement of my player via WASD
float Forwardspeed = Input.GetAxis("Vertical") * movementspeed;
float sidespeed = Input.GetAxis ("Horizontal") * movementspeed;
//this is a simple variable that puts all the movement in place to make the player move.
Vector3 speed = new Vector3 (sidespeed, verticalvelocity, Forwardspeed);
//this is the where i did gravity.
verticalvelocity += Physics.gravity.y * Time.deltaTime;
//this is where i make sure the movementspeed is not effect when on the ground.
if (transform.position.y < 1.9) {
movementspeed = 5.0f;
}
//this is where the jump happens, if both character is on the ground and the player presses "space" the player will jump.
if( charactercontroller.isGrounded && Input.GetButtonDown("Jump")) {
verticalvelocity = jumpspeed;
}
//if the player is in the air, movementspeed is reduced.
if(transform.position.y > 1.9){
movementspeed = 3.0f;
}
speed = transform.rotation * speed;
charactercontroller.Move ( speed * Time.deltaTime );
}//this is part of the begining iff statement
else {
transform.GetComponent<Movement>().enabled = false;
}
}
//this is where i want to send information back and forth through photon.
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
if(stream.isWriting){
stream.SendNext (transform.position);//the position sends correctly
stream.SendNext (transform.rotation);//the rotation sends correctly
stream.SendNext (speed);//because the "speed" variable is in the "void Update" i cant refer to it in this command. how do i make it available without messing up the algorithem?
}
else {
transform.position=(Vector3) stream.ReceiveNext ();
transform.rotation=(Quaternion) stream.ReceiveNext ();
}
}
}