Networking Physics Script.Please help!

Hello,

Ok.I have my semi truck simulation and my multiplayer kinda working.I have it so when i start the game you log in to the lobby,then click new game and it starts the game.It starts my semi truck game where you can drive around and all that fun stuff.Only problem is if someone else logs into the same room they cant see my truck.Now i know this needs to be scripted.But the only scripts i can find is for fps games and doesn’t help me.For some reason when i attach that script to my truck,the truck touches the ground and freaks out and goes flying in the air.

So if someone could point me in a good direction to fix this so the other player can see my truck and i can see his truck that would be wonderful!Maybe even if you have a script i can have that would be even better.The problem is the server is not communicating the trucks.But i have tried creating my own scripts but don’t get far.

I do have a network manager already attached to the truck witch was something also attached to the player in the fps networking demo.

And the code i tried attaching to the truck.I made 2 prefabs of the truck and made one the enemy and one the player.I figured that would work.I was wrong.ha.I was just trying to find an easy way out but then i remembered there i never an easy way out.

Codes are attached to long for the code embedder!

Thanks!

469159–16455–$NetworkManager.cs (10.8 KB)
469159–16456–$PlayerManager.cs (4.59 KB)

Are you using the built-in networking? I’ve seen you post a fair bit about smartfox… the answer to this question could vary wildly depending on what you’re using.

Nope im on smart fox.Please read the edited post up top!

Oh and sorry if its sloppy.I was kinda in a hurry to get it posted.Had to do something!

So no one knows how to help?This seems like a pretty straight forward problem.

If you were using standard Unity Networking you’d want ot use a NetworkInstantiate instead of an Instantiate. Maybe that can give you a lead?

Hmm ok i will check around.At least i know what im searching for now.Thanks

I don’t know much about smartfox, but was under the impression that when using smartfox stuff like this is handled on the server (smartfox) side using their API or whatever, so I think this would be more of a smartfox question and less of a unity question.

The code for doing things with Unity’s built-in networking is vastly different than using a middleware solution.

the networkmanager and playermanager are not attached to a player, they reside in the scene on an empty gameobject. they don’t provide any transform sync. you need the network transform sender script for the localplayer and the receiver script for the remoteplayer.
you will have to look better at the FPS demo.

Ok appels.I looked through the tutorial and i cant find any scripts containing the variables you explained.

The only script i can find containing the variables you explained is in the player manager script.

Edit i found the scripts thanks!I looked at the prefabs of the enemy and player and the scripts are attached to the prefabs.Thanks im gonna go try this out with my trucks!

Indeed they are attached to the players since those need to send the transforms.

Ok i have made prefabs of my big rig one called enemy and another called player.I have attached all the same scripts the fps demo has on their prefabs(enemy and player).My problem is when i start the game in unity engine i check to see if player is loaded into the project when i create a new room and it isnt.Nothing gets loaded.Why is this?

If i open the fps tutorial and try replacing the prefabs in the “game” game object unity crashes.All the right scripts are attached to the prefabs so something is going wrong somewhere because the “player” prefab isnt even being loaded into the project when i create a room.

Not sure what you mean by that but if your prefab doesn’t get spawned in the scene, that usualy means that you havent assigned the prefabs to the spawn script. What errors are you getting ?

Ok let me try to explain this again and maybe it will help.

When i first start the game(i start it in the unity editor)I create a new room.Once the room loads i then check back in my hierarchy to see if the player(prefab) has loaded.It doesnt load.

In the original fps demo(made by a member of smart fox server forum)the player prefab loads and of course you play from there.

I have all the scripts applied to my player prefab as the fps demo player prefab.I also have went through the fps demo and have replicated it in my game.Just for testing i put my enemy(my semi truck with physics applied)prefab and player prefab into the fps demo,i then drag and drop them into the game object named game.When i try to play the demo unity crashes!

So im not real sure why this is.Im guessing its because the network receiver and sender scripts arent made for physics rigged semi trucks.Haha.

Ok well i got it to work by adding in the prefabs into the hierarchy,now the player prefab(clone) loads!!.And i can drive it.I did the same for the enemy.Just to make sure i was driving the car the script was loading i moved the cars i put in the game outside the map.But the cars still load in the map!!But when i tried playing the game with a friend we still cant see each other.

Attached are pictures of what the prefabs look like(scripts attached)I have all the right scripts attached,i should be able to see his car…Please note the cars you see are just test cars.My actual big rig game was to big for me to worry about trying to make multiplayer work with it just yet.

Ive got to leave my desk right now.As you can see im spending to many hours here then i should be.Its 3 32 am here so im off.Hopefully you all can make some sense of this issue.

Dont use networked physics! Not unless you really really really know what you are doing.

How is the physics engine supposed to synchronize its internal states, when all it gets are forced transforms?

Unless you plan for moving all physics server side (and then only send transforms to the clients with no physics on the client side) - then let the primary player object run physics, send the resulting transform to the server - that then distributes it to all clients and puts a dummy avatar (the enemy prefab) in the place where its supposed to be. (1:1 on how the FPS example does it).

So try to start removing the rigidbody from the enemy prefab and see if that doesnt help.

You can later then add physics to things that are visual only - wheels or similar on the remote avatar. But not for movement

And you will spend many more on here.

I cant remove the rigidbody from my enemy prefab because my car script and camera rely on them to work properly.

Ok i removed the rigidbody and car script from the enemy.But when i start 2 games on my computer the first game i started is playable until i login with the other game then both windows,and cars in the windows,stop working and just sit in the air.

A plus is i can see both cars!But i cant drive them.

Edit;I have been fulling with it and now when another player joins it loads a duplicate of your car and you drive both of them.So if you drive one car forward the other car goes forward.Still cant see the other player.

Actually, the M2H tutorial has a great little script, that I translated to JavaScript…let me find it…ah, here we go!

NetworkRigidbody.js

class State
{
  var timestamp : float;
  var position : Vector3;
  var velocity : Vector3;
  var rotation : Quaternion;
  var angularVelocity : Vector3;
}

var interpolationBackTime : float = 0.1;
var extrapolationLimit : float = 0.5;

private var timestampCount : int;
private var currentState : State;
private var stateBuffer : State[] = new State[20];
private var interpolationTime : float;
private var extrapolationLength : float;

function OnSerializeNetworkView( stream : BitStream, info : NetworkMessageInfo ) : void
{
  // Writing data.
  if( stream.isWriting )
  {
    currentState.position = transform.position;
	currentState.rotation = transform.rotation;
	
	stream.Serialize( currentState.position );
	stream.Serialize( currentState.rotation );
  }
  else
  // Reading data.
  {
    currentState.position = Vector3.zero;
	currentState.rotation = Quaternion.identity;
	currentState.velocity = Vector3.zero;
	currentState.angularVelocity = Vector3.zero;
	
	stream.Serialize( currentState.position );
	stream.Serialize( currentState.rotation );
	
	// Shift the buffer sideways, removing state 20.
	for( var ctr : int = stateBuffer.length - 1; ctr >= 1; ctr-- )
	{
	  stateBuffer[ctr] = stateBuffer[ctr-1];
	}
	
	// Record the current state in slot 0.
	currentState.timestamp = info.timestamp;
	stateBuffer[0] = currentState;
	
	// Update the used slot count.
	timestampCount = Mathf.Min( timestampCount - 1, stateBuffer.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( ctr = 0; ctr < timestampCount - 1; ctr++ )
    {
      if( stateBuffer[ctr].timestamp < stateBuffer[ctr+1].timestamp )
	  {
	    Debug.Log( "State inconsistent" );
	  }
    }
	
  }
}

function FixedUpdate() : void
{
  // Calculate rigidbody's target playback time.
  interpolationTime = Network.time - interpolationBackTime;
  
  // If the target is present in the buffer, use the
  // target playback time.
  if( ! stateBuffer[0] || stateBuffer[0] == null )
  {
    Debug.Log( "The first index of the State Buffer is null!" );
	return;
  }
  
  if( stateBuffer[0].timestamp > interpolationTime )
  {
    // Search the buffer and find the right state to replay.
    for( var ctr : int = 0; ctr < timestampCount; ctr++ )
	{
	  if( stateBuffer[ctr].timestamp <= interpolationTime || ctr == timestampCount - 1 )
	  {
	    // Read the newer state (rhs) and the "best" state (lhs).
	    var rhs : State = stateBuffer[ Mathf.Max( ctr-1, 0 ) ];
		var lhs : State = stateBuffer[ ctr ];
		
		// Determine if a interpolation is necessary by comparing
		// lhs and rhs.
		var length : float = rhs.timestamp - lhs.timestamp;
		var t : float = 0.0f;
		
		// The slot used depends on how close the time difference
		// is to 0ms or 100ms.  If it's closer to 100ms, rhs gets
		// used.  Otherwise, lhs is used for the interpolation
		// calculations.
		if( length > 0.00001f )
		{
		  t = ( interpolationTime - lhs.timestamp ) / length;
		}
		
		transform.localPosition = Vector3.Lerp( lhs.position, rhs.position, t );
		transform.localRotation = Quaternion.Slerp( lhs.rotation, rhs.rotation, t );
		return;
	  }
	}
  }
  else
  // ...otherwise, use Extrapolation.
  {
    var latest : State = stateBuffer[0];
	
	// Extrapolation beyond 500ms needs to be handled VERY carefully!
	// Avoid setting extrapolationLimit beyond 500ms.
	extrapolationLength = interpolationTime - latest.timestamp;
	if( extrapolationLength < extrapolationLimit )
	{
	  var axisLength : float = extrapolationLength * latest.angularVelocity.magnitude * Mathf.Rad2Deg;
	  var angularRotation : Quaternion = Quaternion.AngleAxis( axisLength, latest.angularVelocity );
	  
	  transform.position = latest.position + latest.velocity * extrapolationLength;
	  transform.rotation = latest.rotation * latest.rotation;
	}
  }
}