Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by vinhuman · Aug 17, 2014 at 12:49 AM · networkingphotonlerp

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;
             }
             
         }
         
     }
 }
 



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vinhuman · Aug 17, 2014 at 01:11 AM 0
Share

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).

avatar image PaulOrac · Aug 17, 2014 at 01:32 AM 0
Share

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 ?

avatar image vinhuman · Aug 17, 2014 at 08:54 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges