- Home /
Question by
killa247 · Sep 18, 2014 at 09:41 PM ·
animationnetworkingphotonrpc
More efficient way to call these RPC's (Code works)
Hey im just wanting to know if there is a more efficient way to send these rpc's because i know that the dash,ladder and crouch are all getting sent in the update all the time. but it shouldnt be but i cant figure a better way of doing it :l
using UnityEngine.UI;
using UnityEngine;
using System.Collections;
public class P_to_P : Photon.MonoBehaviour {
public GameObject can;
public Slider healthBarSlider;
public GameObject player_mesh;
public GameObject camera_1;
public GameObject right_arm;
public GameObject cube;
public GameObject Force;
public Transform temp_place;
private bool menu_open = false;
private bool ll = false;
private bool rr = false;
public bool dash = false;
public bool on_Ladder = false;
MouseLook mL;
Animator anim;
Menu_right mR;
public void Start (){
mL = camera_1.GetComponent<MouseLook>();
anim = player_mesh.GetComponent<Animator>();
mR = right_arm.GetComponent<Menu_right>();
}
public void Reduce(float p){
healthBarSlider.value -= p;
}
public void Showing_mn(){
can.SetActive(true);
mR.Menu = true;
anim.SetBool ("Menu", true);
}
public void Not_Showing_mn(){
can.SetActive(false);
mR.Menu = false;
anim.SetBool ("Menu", false);
}
public void Angery_cm(){
anim.SetInteger ("Mood", 1);
}
public void Blank_cm(){
anim.SetInteger ("Mood", 0);
}
public void Happy_cm(){
anim.SetInteger ("Mood", 2);
}
public void Left_cm(){
anim.SetBool ("Left_hold", true);
}
public void Right_cm(){
anim.SetBool ("Right_hold", true);
}
public void Left_N_cm(){
anim.SetBool ("Left_hold", false);
}
public void Right_N_cm(){
anim.SetBool ("Right_hold", false);
}
public void Reset_life_m(){
healthBarSlider.value = 100;
}
public void Dead(){
healthBarSlider.value -= 1000;
}
public void Im_crouch(){
CapsuleCollider capsule = GetComponent<CapsuleCollider>();
capsule.height = Mathf.Lerp(capsule.height, 0.75f, Time.deltaTime*10f);
anim.SetBool ("Crouching", true);
}
public void Im_Ncrouch(){
CapsuleCollider capsule = GetComponent<CapsuleCollider>();
capsule.height = Mathf.Lerp(capsule.height, 2f, Time.deltaTime*10f);
anim.SetBool ("Crouching", false);
}
public void D_T(){
anim.SetBool ("Dodgeing", true);
}
public void D_F(){
anim.SetBool ("Dodgeing", false);
}
public void L_T(){
anim.SetBool ("Ladder", true);
}
public void L_F(){
anim.SetBool ("Ladder", false);
}
public void Update(){
PhotonView photonView = this.photonView;
CapsuleCollider capsule = GetComponent<CapsuleCollider>();
if (photonView.isMine) {
if (dash == true) {
photonView.RPC ("Dash_true", PhotonTargets.OthersBuffered);
anim.SetBool ("Dodgeing", true);
} else {
photonView.RPC ("Dash_false", PhotonTargets.OthersBuffered);
anim.SetBool ("Dodgeing", false);
}
if (on_Ladder == true) {
photonView.RPC ("Ladder_true", PhotonTargets.OthersBuffered);
anim.SetBool ("Ladder", true);
}else {
photonView.RPC ("Ladder_false", PhotonTargets.OthersBuffered);
anim.SetBool ("Ladder", false);
Debug.Log("kdshgfh");
}
}
if (Input.GetButton ("Crouch")) {
if (photonView.isMine) {
photonView.RPC ("Im_crouching", PhotonTargets.OthersBuffered);
anim.SetBool ("Crouching", true);
capsule.height = Mathf.Lerp (capsule.height, 0.75f, Time.deltaTime * 10f);
}
} else { if (photonView.isMine) {
photonView.RPC ("Im_Ncrouching", PhotonTargets.OthersBuffered);
anim.SetBool ("Crouching", false);
}}
if (healthBarSlider.value <= 0 && photonView.isMine) {
photonView.RPC ("Reset_life", PhotonTargets.AllBufferedViaServer);
transform.position = new Vector3(0,200,0);
}
if (Input.GetMouseButtonDown(0)) {
if (photonView.isMine) {
GameObject Damage = (GameObject)PhotonNetwork.Instantiate (this.cube.name, temp_place.position, temp_place.rotation, 0);
Add_force script = Damage.GetComponent <Add_force>();
script.id = photonView.ownerId;
}
}
if (Input.GetMouseButtonDown(1)) {
if (photonView.isMine) {
GameObject Forcer = (GameObject)PhotonNetwork.Instantiate(this.Force.name, temp_place.position, temp_place.rotation, 0);
}
}
if (Input.GetKeyDown (KeyCode.Escape)) {
if (photonView.isMine) {
if (menu_open == false) {
mL.enabled = false;
photonView.RPC ("Show_menu", PhotonTargets.AllBufferedViaServer);
menu_open = true;
} else{
mL.enabled=true;
photonView.RPC ("Not_Showing_menu", PhotonTargets.AllBufferedViaServer);
menu_open = false;
}}
}
if (Input.GetKeyDown (KeyCode.R)) {
if (photonView.isMine) {//Blank
photonView.RPC ("Angery", PhotonTargets.AllBufferedViaServer);
}}
if (Input.GetKeyDown (KeyCode.T)) {
if (photonView.isMine) {//Angery
photonView.RPC ("Blank", PhotonTargets.AllBufferedViaServer);
}}
if (Input.GetKeyDown (KeyCode.Y)) {
if (photonView.isMine) {//Happy
photonView.RPC ("Happy", PhotonTargets.AllBufferedViaServer);
}}
if (Input.GetKeyDown (KeyCode.Q)) {
if (ll == false) {
if (photonView.isMine) {
photonView.RPC ("Left", PhotonTargets.AllBufferedViaServer);
ll = true;
}} else {if (photonView.isMine) {
photonView.RPC ("Left_N", PhotonTargets.AllBufferedViaServer);
ll = false;
}}
}
if (Input.GetKeyDown (KeyCode.E)) {
if (rr == false) {
if (photonView.isMine) {
photonView.RPC ("Right", PhotonTargets.AllBufferedViaServer);
rr = true;
}} else {if (photonView.isMine) {
photonView.RPC ("Right_N", PhotonTargets.AllBufferedViaServer);
rr = false;
}}
}
}
private void OnTriggerEnter(Collider other)
{
PhotonView photonView = this.photonView;
if (other.name == "Pain(Clone)") {
Add_force script = other.GetComponent <Add_force>();
if (photonView.isMine && script.id != photonView.ownerId) {
photonView.RPC ("ReduceHealth", PhotonTargets.AllBufferedViaServer);
}
}
}
private void OnCollisionEnter(Collision other){
PhotonView photonView = this.photonView;
if (photonView.isMine && other.gameObject.name == ("Deadz")) {
photonView.RPC ("Dead_zone", PhotonTargets.AllBufferedViaServer);
}
}
[RPC]
public void ReduceHealth()
{
Reduce(1);
}
[RPC]
public void Im_crouching()
{
Im_crouch();
}
[RPC]
public void Im_Ncrouching()
{
Im_Ncrouch();
}
[RPC]
public void Not_Showing_menu()
{
Not_Showing_mn();
}
[RPC]
public void Show_menu()
{
Showing_mn();
}
[RPC]
public void Angery()
{
Angery_cm();
}
[RPC]
public void Blank()
{
Blank_cm();
}
[RPC]
public void Happy()
{
Happy_cm();
}
[RPC]
public void Left()
{
Left_cm();
}
[RPC]
public void Right()
{
Right_cm();
}
[RPC]
public void Left_N()
{
Left_N_cm();
}
[RPC]
public void Right_N()
{
Right_N_cm();
}
[RPC]
public void Reset_life()
{
Reset_life_m();
}
[RPC]
public void Dead_zone()
{
Dead();
}
[RPC]
public void Dash_true()
{
D_T();
}
[RPC]
public void Dash_false()
{
D_F();
}
[RPC]
public void Ladder_true()
{
L_T();
}
[RPC]
public void Ladder_false()
{
L_F();
}
}
Comment
Best Answer
Answer by killa247 · Sep 19, 2014 at 12:40 PM
For anyone else in the Future, i figured it out, for frequent changed things, (crouch, dodge and ladder)
i added this to my mesh, and linked the player prefab, and added a photon view to the player mesh and observed this script.
using UnityEngine;
using System.Collections;
public class Anims_net : Photon.MonoBehaviour {
Animator anim;
animation anims;
CapsuleCollider capsule;
public GameObject player;
private float realSpeed = 0f;
private float realSpeed_X = 0f;
private float realSpeed_Z = 0f;
private float realJumpSpeed = 0f;
private bool Jumping = false;
private bool crouching = false;
private bool dodgeing = false;
private bool ladder = false;
private Vector3 correctPlayerPos = Vector3.zero;//We lerp towards this
private Quaternion correctPlayerRot = Quaternion.identity;
void Start() {
anim = GetComponent<Animator>();
anims = GetComponent<animation>();
capsule = player.GetComponent<CapsuleCollider>();
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(player.transform.position);
stream.SendNext(player.transform.rotation);
stream.SendNext(anim.GetFloat("Speed"));
stream.SendNext(anim.GetFloat("Moving_X"));
stream.SendNext(anim.GetFloat("Moving_Z"));
stream.SendNext(anim.GetFloat("Jumping_speed"));
stream.SendNext(anim.GetBool("Jumping"));
stream.SendNext(anim.GetBool("Crouching"));
stream.SendNext(anim.GetBool("Dodgeing"));
stream.SendNext(anim.GetBool("Ladder"));
}
else
{
correctPlayerPos = (Vector3)stream.ReceiveNext();
correctPlayerRot = (Quaternion)stream.ReceiveNext();
realSpeed = (float)stream.ReceiveNext();
realSpeed_X = (float)stream.ReceiveNext();
realSpeed_Z = (float)stream.ReceiveNext();
realJumpSpeed = (float)stream.ReceiveNext();
Jumping = (bool)stream.ReceiveNext();
crouching = (bool)stream.ReceiveNext();
dodgeing = (bool)stream.ReceiveNext();
ladder = (bool)stream.ReceiveNext();
}
}
void Update()
{
PhotonView pv = PhotonView.Get(player);
if (!pv.isMine)
{
player.transform.position = Vector3.Lerp(player.transform.position, correctPlayerPos, Time.deltaTime * 5);
player.transform.rotation = Quaternion.Lerp(player.transform.rotation, correctPlayerRot, Time.deltaTime * 5);
anim.SetFloat("Speed" ,(Mathf.Lerp(anim.GetFloat("Speed"), realSpeed, Time.deltaTime * 5)));
anim.SetFloat("Moving_X" ,(Mathf.Lerp(anim.GetFloat("Moving_X"), realSpeed_X, Time.deltaTime * 5)));
anim.SetFloat("Moving_Z" ,(Mathf.Lerp(anim.GetFloat("Moving_Z"), realSpeed_Z, Time.deltaTime * 5)));
anim.SetFloat("Jumping_speed" ,(Mathf.Lerp(anim.GetFloat("Jumping_speed"), realJumpSpeed, Time.deltaTime * 5)));
anim.SetBool("Jumping" , Jumping);
anim.SetBool("Crouching" , crouching);
if (crouching){capsule.height = 0.75f;}else{capsule.height = 2f;}
anim.SetBool("Dodgeing" , dodgeing);
anim.SetBool("Ladder" , ladder);
}
}
}