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 Erisat · Feb 22, 2014 at 05:06 PM · cameranetworkingrotateauthoritative

Rotating character on server using camera angle on client

I have an authoritative server/client setup, the client sends its inputs to the server, which performs all movements/rotations and the end result is streamed back to the client using OnSerializeNetworkView. Keyboard movement (transform.position) is working just fine, whats giving me a headache is the rotation, as the rotation is determined by the camera.eulerangle.y. So when i send the inputs to the server (through an RPC called in Update() on the client), i set the eulerangle.y of the camera to a variable, i send this variable inside the RPC, and then the server rotates the charactercontroller transform using the camera's euler angles sent from the client. When i move around, things run smoothly only sometimes. During rotation on the server side, the player snaps around a little bit, and sometimes gets stuck completely till i rotate more. on the client side, no rotation is applied at all.

A Video of the problem: http://www.youtube.com/watch?v=7TJKkJ2npEc&feature=youtu.be

If somebody could offer some advice, thatd be great

 void Awake()
     {
         CharacterController = GetComponent("CharacterController") as CharacterController;
         if (Network.isClient)
         {
             enabled = false;
 
         }
 
     }
     void Update()
     {
         if (Network.isServer)
         {
             DetermineServerMoveDirection();
             ProcessServerMotion();
 
         }
         else if (Network.isClient)
         {
             if ((owner != null) && (Network.player == owner))
             {
                 targetLookAtTransform = transform.FindChild ("targetLookAt");
                 PlayerCam.UseExistingOrCreateNewMainCamera(targetLookAtTransform);
                 if(Camera.main == null)
                     return;
 
 
                 MoveVector = Vector3.zero;
                 if(Input.GetAxis("Vertical") > DeadZone || Input.GetAxis("Vertical") < -DeadZone)
                     MoveVector += new Vector3 (0,0,Input.GetAxis("Vertical"));
                 if(Input.GetAxis("Horizontal") > DeadZone || Input.GetAxis("Horizontal") < -DeadZone)
                     MoveVector += new Vector3 (Input.GetAxis("Horizontal"),0,0);
 
                 
 
                 CameraEulerY = Camera.main.transform.eulerAngles.y;
                     networkView.RPC("SendMotionInput", RPCMode.Server, MoveVector, CameraEulerY);
 
                 
 
 
 
             }
 
 
         }
     }
     [RPC]
     void SendMotionInput(Vector3 MoveVector, float CameraEulerY)
     {  
         newMoveVector = MoveVector;
         ServerEulerY = CameraEulerY;
     }
 
     
     void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
     {
         Vector3 positionReceive = Vector3.zero;
         Quaternion rotationReceive = Quaternion.identity;
         Vector3 position = Vector3.zero;
         Quaternion rotation = Quaternion.identity;
         if ((stream.isWriting))
         {
             position = transform.position;
             rotation = transform.rotation;
             stream.Serialize(ref rotation);
             stream.Serialize(ref position);
         }
         else
         {
 
             stream.Serialize(ref rotationReceive);
             transform.rotation = rotationReceive;
             stream.Serialize(ref positionReceive);
             transform.position = positionReceive;
         }
     }
 
     void ProcessServerMotion()
     {
         newMoveVector = transform.TransformDirection(newMoveVector);
         if (newMoveVector.magnitude > 1) newMoveVector = Vector3.Normalize(newMoveVector);
         newMoveVector *= MoveSpeed();
         newMoveVector = new Vector3(newMoveVector.x, newMoveVector.y, newMoveVector.z);
         CharacterController.Move(newMoveVector * Time.deltaTime);
         newServerRotation = Quaternion.Euler(CharacterController.transform.eulerAngles.x,ServerEulerY,CharacterController.transform.eulerAngles.z);
         CharacterController.transform.rotation = Quaternion.Slerp(transform.rotation, newServerRotation, 0.1f * Time.deltaTime);
     }
 
     float MoveSpeed()
     {
         var moveSpeed = 0f;
         switch (MoveDirection)
         {
         case Direction.Stationary:
             moveSpeed = 0;
             break;
         case Direction.Forward:
             moveSpeed = ForwardSpeed;
             break;
         case Direction.Backward:
             moveSpeed = BackwardSpeed;
             break;
         case Direction.Left:
             moveSpeed = StrafingSpeed;
             break;
         case Direction.Right:
             moveSpeed = StrafingSpeed;
             break;
         case Direction.LeftForward:
             moveSpeed = ForwardSpeed;
             break;
         case Direction.RightForward:
             moveSpeed = ForwardSpeed;
             break;
         case Direction.LeftBackward:
             moveSpeed = BackwardSpeed;
             break;
         case Direction.RightBackward:
             moveSpeed = BackwardSpeed;
             break;
         }
         return moveSpeed;
     }
     public enum Direction
     {
         Stationary, Forward, Backward, Left, Right, LeftForward, RightForward, LeftBackward, RightBackward
     }
     public void DetermineServerMoveDirection()
     {
         bool forward = false;
         bool backward = false;
         bool left = false;
         bool right = false;
         if(newMoveVector.z > 0)
             forward = true;
         if(newMoveVector.z < 0)
             backward = true;
         if(newMoveVector.x > 0)
             right = true;
         if(newMoveVector.x < 0)
             left = true;
         if(forward)
         {
             if(left)
                 MoveDirection = Direction.LeftForward;
             else if(right)
                 MoveDirection = Direction.RightForward;
             else
                 MoveDirection = Direction.Forward;
         }
         else if(backward)
         {
             if(left)
                 MoveDirection = Direction.LeftBackward;
             else if(right)
                 MoveDirection = Direction.RightBackward;
             else
                 MoveDirection = Direction.Backward;
         }
         else if(left)
             MoveDirection = Direction.Left;
         else if(right)
             MoveDirection = Direction.Right;
         else
             MoveDirection = Direction.Stationary;
     }
     
     [RPC]
     void setOwner ( NetworkPlayer player  )
     {
         owner = player;
         if(player == Network.player)
         {
             enabled=true;
         }
     }
     public NetworkPlayer getOwner ()
     {
         return owner;
     }
 
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

Answer by pettymr · Dec 19, 2016 at 08:41 PM

I think if I am correct, you want to be using a "Command" not an "RPC". RPCs typically allow the server to do something to the client, while a Command is a way for the client to request to do something on the server. Try switching up how you have it tagged perhaps?

https://docs.unity3d.com/Manual/class-NetworkBehaviour.html

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

20 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

Related Questions

Can the Camera Smoothly rotate AFTER a button is pressed? (Javascript) 1 Answer

Why is my camera switching angles? 0 Answers

Set Max Rotation On Weapon Sway 0 Answers

How to rotate the camera to the gameobject position. 3 Answers

NetworkIdentity.clientAuthorityOwner no set on NetworkLobbyManager's playerPrefab 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