void FixedUpdate() { if (m_game.m_state == Game.State.ST_Running) { if (TimeMgr.getTimeStampMicro() > m_curTime) { if (!m_game.m_bSer) { Vector2 cltPos = new Vector2(m_game.m_ball.position.x, m_game.m_ball.position.z); Vector2 cltVel = new Vector2(m_game.m_ball.rigidbody.velocity.x, m_game.m_ball.rigidbody.velocity.z); float deltaTime = (TimeMgr.getTimeStampMicro() - m_curTime) / 1000.0f; Vector2 posB = m_svrPos + m_svrVel * deltaTime; Vector2 pos = cltPos * (1 - m_rate) + posB * m_rate; m_game.m_ball.position = new Vector3(pos.x, m_game.m_ball.position.y, pos.y); Vector2 vel = cltVel * (1 - m_rate) + m_svrVel * m_rate; Debug.Log(cltVel.y + "," + cltVel.y * (1 - m_rate)); m_game.m_ball.rigidbody.velocity = new Vector3(vel.x, m_game.m_ball.rigidbody.velocity.y, vel.y); Debug.Log("CltPos:" + cltPos + ",SvrPos:" + m_svrPos + ",CltVel:" + cltVel + ",SvrVel:" + m_svrVel); } } if (m_shots.Count > 0) { Vector2 dir = m_shots.Dequeue(); Vector3 vec = m_game.m_ball.rigidbody.velocity; if (m_game.m_bottom && m_game.m_ball.position.z >= 0 || !m_game.m_bottom && m_game.m_ball.position.z <= 0) { Vector3 direction = new Vector3(dir.x, 0, dir.y); m_game.m_ball.rigidbody.velocity = direction; } } } }
void Update() { if (m_state == State.ST_Start) { m_state = State.ST_Running; m_time = 0; m_input.m_enabled = true; if (m_bottom) { m_other.position = m_tranUp.position; m_other.rotation = m_tranUp.rotation; Camera.main.transform.position = m_tranBottom.position; Camera.main.transform.rotation = m_tranBottom.rotation; } else { m_other.position = m_tranBottom.position; m_other.rotation = m_tranBottom.rotation; Camera.main.transform.position = m_tranUp.position; Camera.main.transform.rotation = m_tranUp.rotation; } m_ball.position = m_tranBall.position; m_ball.rotation = m_tranBall.rotation; } if (m_state == State.ST_Running) { if (m_time > m_gameTime) { m_state = State.ST_End; CmdPacket packet = new CmdPacket(); //packet.WriteUShort(Proto.C_AutoMatch); //m_client.m_client.Send(packet); } m_time += Time.deltaTime; //同步位置和速度 if (m_bSer) { //if (Time.realtimeSinceStartup - m_lstSyncTime > Time.deltaTime) { CmdPacket packet = new CmdPacket(); packet.WriteUShort(Proto.Synch_Pos); packet.WriteUInt64(TimeMgr.getTimeStampMicro()); packet.WriteFloat(m_ball.position.x); packet.WriteFloat(m_ball.position.z); packet.WriteFloat(m_ball.rigidbody.velocity.x); packet.WriteFloat(m_ball.rigidbody.velocity.z); m_client.send(packet); m_lstSyncTime = Time.realtimeSinceStartup; } } } }