I’ve searched, I’ve scripted, I’ve failed
I have engine particle effects that grow and shrink depending on throttle %. What I would like to do, is see a remote players particles do the same.
I’ve tried all sorts of syntax with PhotonView and [RPC], sometimes with errors and sometimes not. With or without errors, I cannot find a way to do this.
Is it possible?
The code so far even though it doesn’t work…
using UnityEngine;
using System.Collections;
public class ShipControl : Photon.MonoBehaviour {
private Transform ThisTransform;
private float Throttle;
private float Thrust;
private Vector3 RotateVector; // Contains Joystick Or Mouse Movement Information
private Vector4 MoveVector;
public Vector3 ShipVelocityPYR = new Vector3(40,40,60); //Pitch Yaw & Roll speed Deg per sec
public Vector2 ShipVelocityFR = new Vector2(60,0); //Speed Forward & Reverse
public Vector4 ShipDamp = new Vector4(10,10,10,10); //Bigger numbers = more responsive, Smaller numbers = more sluggish
private GUIText enginePercentageText;
public float enginePowerValue = 0f;
public ParticleSystem leftTExhaustFX;
public ParticleSystem rightTExhaustFX;
public ParticleSystem leftBExhaustFX;
public ParticleSystem rightBExhaustFX;
void Start () {
ThisTransform = this.transform;
enginePercentageText = GameObject.Find ("XWEnginePowerLabel").guiText;
}
// Update is called once per frame
void Update () {
OnEnginePowerChange ();
photonView.RPC ("AdjustEngineFX", PhotonTargets.All);
float DeltaTime = Time.deltaTime;
//Get The Imput Value From The Joystick
MoveVector = new Vector4(Input.GetAxisRaw("Vertical"), Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Roll"), Input.GetAxisRaw("Thrust"));
//Smooth The Joystick Inputs Over Time To Simulate Inertia
RotateVector = new Vector3(
Mathf.Lerp(RotateVector.x, MoveVector.x * (ShipVelocityPYR.y * DeltaTime), ShipDamp.y * DeltaTime),
Mathf.Lerp(RotateVector.y, MoveVector.y * (ShipVelocityPYR.x * DeltaTime), ShipDamp.x * DeltaTime),
Mathf.Lerp(RotateVector.z, MoveVector.z * (ShipVelocityPYR.z * DeltaTime), ShipDamp.z * DeltaTime));
// Calculate The Amount Of Thrust From The Current Throttle Position. Are We Going Forward Or Reverse
if(MoveVector.w == 0){
Thrust = Mathf.MoveTowards(Thrust, MoveVector.w * 0, ShipDamp.w * DeltaTime);
}else if(MoveVector.w > 0){
Thrust = Mathf.MoveTowards(Thrust, MoveVector.w * ShipVelocityFR.x, ShipDamp.w * DeltaTime);
}else if (MoveVector.w < 0){
Thrust = Mathf.MoveTowards(Thrust, MoveVector.w * ShipVelocityFR.y, ShipDamp.w * DeltaTime);
}
//Apply Rotation & Translation To This Transform
ThisTransform.Rotate(RotateVector);
ThisTransform.Translate(ThisTransform.TransformDirection(new Vector3(0,0,1)) * (Thrust * DeltaTime), Space.World);
GameObject engineAudioTL = GameObject.Find ("LocalSHIP/Engines/EngineTopLeft");
GameObject engineAudioTR = GameObject.Find ("LocalSHIP/Engines/EngineTopRight");
GameObject engineAudioBL = GameObject.Find ("LocalSHIP/Engines/EngineBottomLeft");
GameObject engineAudioBR = GameObject.Find ("LocalSHIP/Engines/EngineBottomRight");
AudioSource EngineTL = engineAudioTL.GetComponent<AudioSource> ();
AudioSource EngineTR = engineAudioTR.GetComponent<AudioSource> ();
AudioSource EngineBL = engineAudioBL.GetComponent<AudioSource> ();
AudioSource EngineBR = engineAudioBR.GetComponent<AudioSource> ();
EngineTL.pitch = Mathf.Abs (Thrust/100)/2;
EngineTR.pitch = Mathf.Abs (Thrust/100)/2;
EngineBL.pitch = Mathf.Abs (Thrust/100)/2;
EngineBR.pitch = Mathf.Abs (Thrust/100)/2;
}
void OnEnginePowerChange() {
if (enginePercentageText != null)
{
enginePowerValue = (Thrust / 17) / 10;
enginePercentageText.text = "Throttle" + " " + (enginePowerValue * 100).ToString("f0") + "%";
}
}
[RPC] void AdjustEngineFX() {
if(leftTExhaustFX != null && rightTExhaustFX != null && rightBExhaustFX != null && rightBExhaustFX != null)
{
leftTExhaustFX.startSize = 0.2f + enginePowerValue * 1.5f;
rightTExhaustFX.startSize = 0.2f + enginePowerValue * 1.5f;
leftBExhaustFX.startSize = 0.2f + enginePowerValue * 1.5f;
rightBExhaustFX.startSize = 0.2f + enginePowerValue * 1.5f;
leftTExhaustFX.startSpeed = 0.2f + enginePowerValue * 1.5f;
rightTExhaustFX.startSpeed = 0.2f + enginePowerValue * 1.5f;
leftBExhaustFX.startSpeed = 0.2f + enginePowerValue * 1.5f;
rightBExhaustFX.startSpeed = 0.2f + enginePowerValue * 1.5f;
}
}
}