//int currentHealth;
var m_InterpolationBackTime: double = 0.1;
var m_ExtrapolationLimit: double = 0.5;
var TankCtl_Tower: Transform;
var TankCtl_Cannon: Transform;
class TankInfo {
var timestamp: double;
var TankBodyPos: Vector3;
var TankBodyRot: Quaternion;
var TankTowerPos: Vector3;
var TankTowerRot: Quaternion;
var TankCannonPos: Vector3;
var TankCannonRot: Quaternion;
var TankSpeed: float;
var TankWayLR: int;
var TankWayFB: int;
var TankOnFire: int;
var TankOnMachineGun: int;
}
private var bufferTankInfo: TankInfo[] = new TankInfo[20];
private var m_TimestampCount: int;
//private var iWayFB: int; //the way of tank go to.
//private var iWayLR: int;
//private var iSpeed: float;
private var bodyP: Vector3 = Vector3.zero;
private var bodyR: Quaternion = Quaternion.identity;
private var towerP: Vector3 = Vector3.zero;
private var towerR: Quaternion = Quaternion.identity;
private var cannoP: Vector3 = Vector3.zero;
private var cannoR: Quaternion = Quaternion.identity;
private var fltSpeed: float = 0.0;
private var w_FB : int = 0;
private var w_LR : int = 0;
function Start() {
// for (var i: int = 0; i<20; i++) {
// TankInfo[i].timestamp = 0;
// }
}
//function TankSpeed(i: float) {
// iSpeed=i;
//}
//function TankWayFB(i: int) {
// iWayFB = i;
//}
//function TankWayLR(i: int) {
// iWayLR = i;
//}
function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo)
{
if (stream.isWriting)
{
// int health = currentHealth;
// stream.Serialize(health);
bodyP = transform.position;
bodyR = transform.rotation;
towerP = TankCtl_Tower.position;
towerR = TankCtl_Tower.rotation;
cannoP = TankCtl_Cannon.position;
cannoR = TankCtl_Cannon.rotation;
fltSpeed = TankTransCtl_BodyScript.GoSpeed;
w_FB = TankTransCtl_BodyScript.iWayFB;
w_LR = TankTransCtl_BodyScript.iWayLR;
//Cannon
//MachineGun
stream.Serialize(bodyP);
stream.Serialize(bodyR);
stream.Serialize(towerP);
stream.Serialize(towerR);
stream.Serialize(cannoP);
stream.Serialize(cannoR);
stream.Serialize(fltSpeed);
stream.Serialize(w_FB);
stream.Serialize(w_LR);
}
else
{
bodyP = Vector3.zero;
bodyR = Quaternion.identity;
towerP = Vector3.zero;
towerR = Quaternion.identity;
cannoP = Vector3.zero;
cannoR = Quaternion.identity;
fltSpeed = 0.0;
w_FB = 0;
w_LR = 0;
//Cannon
//MachineGun
stream.Serialize(bodyP);
stream.Serialize(bodyR);
stream.Serialize(towerP);
stream.Serialize(towerR);
stream.Serialize(cannoP);
stream.Serialize(cannoR);
stream.Serialize(fltSpeed);
stream.Serialize(w_FB);
stream.Serialize(w_LR);
for (var i=bufferTankInfo.Length-1;i>=1;i--)
{
bufferTankInfo[i] = bufferTankInfo[i-1];
}
var t_Info: TankInfo;
print("info.timestamp=" + info.timestamp);
t_Info.timestamp = info.timestamp;
t_Info.TankBodyPos = bodyP;
t_Info.TankBodyRot = bodyR;
t_Info.TankTowerPos = towerP;
t_Info.TankTowerRot = towerR;
t_Info.TankCannonPos = cannoP;
t_Info.TankCannonRot = cannoR;
t_Info.TankSpeed = fltSpeed;
t_Info.TankWayFB = w_FB;
t_Info.TankWayLR = w_LR;
bufferTankInfo[0] = t_Info;
m_TimestampCount = Mathf.Min(m_TimestampCount + 1, bufferTankInfo.Length);
// Check if states are in order, if it is inconsistent you could reshuffel or
// drop the out-of-order state. Nothing is done here
for (var i1=0;i1<m_TimestampCount-1;i1++)
{
if (bufferTankInfo[i1].timestamp < bufferTankInfo[i1+1].timestamp){
Debug.Log("State inconsistent");
}
}
}
}
function Update() {
// This is the target playback time of the rigid body
var interpolationTime = Network.time - m_InterpolationBackTime;
// Use interpolation if the target playback time is present in the buffer
print("NetTankCtlScript.js, Update:" + bufferTankInfo.length +"++++++++++++++++++++++++++++++++");
var tmpInfo: TankInfo;
tmpInfo = bufferTankInfo[0];
//if (bufferTankInfo[0].timestamp > interpolationTime) {
if(tmpInfo.timestamp > interpolationTime) {
// Go through buffer and find correct state to play back
for (var i2=0;i2<m_TimestampCount;i2++) {
if (bufferTankInfo[i2].timestamp <= interpolationTime || i2 == m_TimestampCount-1) {
// The state one slot newer (<100ms) than the best playback state
var rhs: TankInfo = bufferTankInfo[Mathf.Max(i2-1, 0)];
// The best playback state (closest to 100 ms old (default time))
var lhs: TankInfo = bufferTankInfo[i2];
// Use the time between the two slots to determine if interpolation is necessary
var length = rhs.timestamp - lhs.timestamp;
var t: float = 0.0;
// As the time difference gets closer to 100 ms t gets closer to 1 in
// which case rhs is only used
// Example:
// Time is 10.000, so sampleTime is 9.900
// lhs.time is 9.910 rhs.time is 9.980 length is 0.070
// t is 9.900 - 9.910 / 0.070 = 0.14. So it uses 14% of rhs, 86% of lhs
if (length > 0.0001)
t = (interpolationTime - lhs.timestamp) / length * 1.0;
// if t=0 => lhs is used directly
transform.localPosition = Vector3.Lerp(lhs.TankBodyPos, rhs.TankBodyPos, t);
transform.localRotation = Quaternion.Slerp(lhs.TankBodyRot, rhs.TankBodyRot, t);
return;
}
}
}
// Use extrapolation
else
{
var latest: TankInfo = bufferTankInfo[0];
var extrapolationLength: float = (interpolationTime - latest.timestamp) * 1.0;
// Don't extrapolation for more than 500 ms, you would need to do that carefully
if (extrapolationLength < m_ExtrapolationLimit){
TankTransCtl_BodyScript.GoSpeed = latest.TankSpeed;
TankTransCtl_BodyScript.iWayFB = latest.TankWayFB;
TankTransCtl_BodyScript.iWayLR = latest.TankWayLR;
fltSpeed = latest.TankSpeed;
w_FB = latest.TankWayFB;
w_LR = latest.TankWayLR;
BroadcastMessage ("TankWayFB", w_FB);
BroadcastMessage ("TankWayLR", w_LR);
BroadcastMessage ("TankSpeed", fltSpeed);
}
}
}
Hello, anybody.
I wrote the NetTankCtlScript.js to control my tank over network. when I was using networkview to communication, the error message was occure.
Please help me.
Shawn.