- Home /
Smooth movement of non-player objects/vehicles over the network? (Photon)
Hey guys. I finally got a vehicle working in single player but when I try multiplayer the movement looks very choppy. I'm following along quill18's multiplayer FPS tutorial. My vehicle is instantiated in a setup like this:
void OnCreatedRoom{
SpawnMyVehicle();
}
void SpawnMyVehicle(){
GameObject Vehicle = PhotonNetwork.Instantiate ("Vehicle", vehicleSpawnspot.transform.position, vehicleSpawnspot.transform.rotation, 0);
}
. Quill18's script instantiates players when they join the room, but I do not want to instantiate a vehicle for every player that enters. I only want it to be instantiated at the beginning of the room's creation.
Two problems exist. The first is the vehicle deletes itself when the player who created the room leaves. The second is the movement is very choppy for any player that didn't create the room. Any help would be greatly appreciated. My vehicle script that the photon view follows looks like this:
using UnityEngine;
using System.Collections;
public class NetworkVehicle: Photon.MonoBehaviour {
Vector3 realPosition = Vector3.zero;
Quaternion realRotation = Quaternion.identity;
bool gotFirstUpdate = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if( photonView.isMine ) {
// Do nothing
}
else {
transform.position = Vector3.Lerp(transform.position, realPosition, 0.1f);
transform.rotation = Quaternion.Lerp(transform.rotation, realRotation, 0.1f);
}
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {
if(stream.isWriting) {
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else {
realPosition = (Vector3)stream.ReceiveNext();
realRotation = (Quaternion)stream.ReceiveNext();
if(gotFirstUpdate == false) {
transform.position = realPosition;
transform.rotation = realRotation;
gotFirstUpdate = true;
}
}
}
}
Answer by PaulOrac · Aug 17, 2014 at 01:02 AM
As you are moving a vehicle I bet you are moving it with physics then you should also serialize the rigidbody.velocity. That should solve the chopines problem. About your other issue... Try calling SpawnMyVehicle() from inside OnJoinedRoom() insted of OnCreatedRoom() since that is only called by the masterclient.
I will try the second suggestion, but I am actually currently moving the vehicle simply using a transform.translate bit of code. I do plan on moving it to a rigidbody setup when I figure out how to get that to play nice with my character controller (or if I switch my character controller out for rigidbody movement).
Wow that's weird... in my experience it should work by doing just this:
void Update () {
if( photonView.is$$anonymous$$ine ) {
// Do nothing
}
else {
transform.position = Vector3.Lerp(transform.position, realPosition, 0.1f);
transform.rotation = Quaternion.Lerp(transform.rotation, realRotation, 0.1f);
}
}
if(stream.isWriting) {
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else {
realPosition = (Vector3)stream.ReceiveNext();
realRotation = (Quaternion)stream.ReceiveNext();
}
}
Why do you want to get the first Update ?
It was an unnecessary bit of code that was originally implemented when an animator was called. I did however find my problem. OnCreatedRoom() actually does appear to be the correct time to call the Spawn$$anonymous$$yVehicle function for my particular case.
However, I needed to use PhotonNetwork.InstantiateSceneObject ins$$anonymous$$d of simply PhotonNetwork.Instantiate. This allows the object to remain after the original room creator leaves, and it also almost entirely fixes the movement problem.
There is still a little bit of lag in terms of how other players look on the vehicle, but the vehicle itself moves smoothly on all screens now.
Answer by J_Troher · Sep 11, 2014 at 09:40 AM
My problem with this is that you can't sync player gameobject transforms directly. If you were able to parent the player to the mount point, and cut their pos/rot sync. It would be perfect. I also disable character controller on anyone but my player. As well as u tagged vs player tag, only on me. It works quite well.
If I don't think up a way to trick clients into seeing others parenting to vehicles. I will probably delete and spawn new prefabs on entering/editing. May be less code and way more stable.
Your answer

Follow this Question
Related Questions
Using lerp to smooth network movement causes rubber banding 1 Answer
PhotonNetwork and Vector3.Lerp resets the player back to (0, 0, 0) 1 Answer
[Photon]Player vs. Player Collision 0 Answers
Correct way to design turn based multiplayer using Photon Unity Networking. 0 Answers
how do i apply damage to objects other that the player 1 Answer