Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by RainMomo · May 19, 2016 at 11:05 AM · networkingnetwork

How to move an object over the network. If only the client controlls/moves that non-player object?

Hello all, I need some help with my code to move an object over the network. in the code i have, the object that i want to move moves on the network and on the clients. This is done by use a command and a RPC call to a syncvar (Vector3) on my movingobject. The strange thing is that that same syncvar is set to the right value i want with the command and RPC but then it also go back to the old value again. So the object goes back to its old position. (not wat i want). i cant find it where that is happening that the syncvar is going back to its old position. so i want a object that can move and its getting controlled by a client. I think i do something wrong in the code that i have and i hope someone can help me with wat i am doing wrong here.

This is the script on the player object for the client:

 public Vector3 newPosition;
 public GameObject gameObjectTodrag;
   
 void Awake()
     {
         gameObjectTodrag= GameObject.FindGameObjectWithTag("movingObject");
         newPosition = gameObjectTodrag.transform.position;
     }
 
 void Update()
     {
         if (isClient && isLocalPlayer)
         {
             MyMove();
         }
     }
 
  void MyMove()
    {
      CmdMoveTouchOnServer(gameObjectTodrag,  movingObjectsWithPosition[gameObjectTodrag]);   
    }
 
 [Command]
 void CmdMoveTouchOnServer(GameObject obj, Vector3 newPos)
     {
         objNetId = obj.GetComponent<NetworkIdentity>();        
         if (!hasAuthority)
         {
             objNetId.AssignClientAuthority(connectionToClient);   
         }
          RpcMoveTouchOnServer(obj, newPos);
     }
 
 [ClientRpc]
 void RpcMoveTouchOnServer(GameObject obj, Vector3 newPos)
     {
         obj.GetComponent<MoveObject>().newPosition = newPos;      
     }

On the MoveObject script it will handle the movement to look at the "newPosition" defined like this:

  [SyncVar]
     public Vector3 newPosition;
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by dazapa2894 · May 19, 2016 at 03:57 PM

do you have NetworkTransform component attached to the object you want to move?

highly recommend you this tutorial:

https://unity3d.com/es/learn/tutorials/topics/multiplayer-networking/introduction-simple-multiplayer-example?playlist=29690

look at the 7 point: NETWORKING PLAYER MOVEMENT https://unity3d.com/es/learn/tutorials/topics/multiplayer-networking/networking-player-movement?playlist=29690

Comment
Add comment · Show 1 · 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 RainMomo · May 20, 2016 at 12:08 PM 0
Share

Thanks for the information/tutorials. yes i have the networkTransfom components on the objects.

i have found the solution also: the mistake was here

 if (isClient && isLocalPlayer)
          {
              $$anonymous$$y$$anonymous$$ove();
          }

This is on both clients true. so if 1 client change the value the other one changed it back. since i work with 1 pc and 1 tablet i can make it work on only 1 client to check on what device its running. i also found that the RPC calls are not needed if you work with syncVar and that you can do obj.GetComponent<$$anonymous$$oveObject>().newPosition = newPos; in the command.

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

77 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

When Do SyncLists get Initialized? 0 Answers

Synchronize scene loading networking 1 Answer

Establishing a local connection to server 1 Answer

Teleporting a NetworkTransform 0 Answers

Network commands sent from clients not working. 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