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 anomaly_a · Oct 21, 2012 at 10:20 PM · movementmultiplayernetworkinputsauthoritative

Movement axis different on client than on server?

So I have an authoritative third person multiplayer scene working almost perfectly. I have adapted the Unity Networking authoritative third person example to get to this point but their example is broken (as many people have noted already). Here is the problem I am seeing:

When I hit W, the ThirdPersonController.js takes that input and converts it into forward motion (forward in respect to the camera).

From here the NetworkController.js is called and gets the exact same input and sends it to the remote ThirdPersonController script on the server.

The server then converts it to NORTH facing motion. It doesn't matter what direction you are facing it always goes the same direction.

So as you would expect, if you wanted to walk up, you would face North and hit W and it would work great, but if you wanted to go South you would face South and hit W. On client side you walk South and on the server side you walk....North.

So my assumption is that the exact ThirdPersonController exists on client and server and when you give the server your input it SHOULD compute your motion EXACTLY the same as your client does. But it appears to be skipping everything in the ThirdPersonController script and taking the horizontal and vertical inputs as exactly N, S, E and W.

So if someone could please guide me and/or enlighten me as to what I am doing wrong or what appears to be happening.

Heres the code. I cut out quite a bit to shorten it but they key thing is that the inputs in the ThirdPersonController go through some smoothing and conversion that I don't think the server is doing.

cNetworkController

 var targetController : cThirdPersonController;
 private var jumpButton : boolean;
 private var verticalInput : float;
 private var horizontalInput : float;
 private var lastJumpButton : boolean;
 private var lastVerticalInput : float;
 private var lastHorizontalInput : float;
 
 function Update()
 {
     // Sample user input
     verticalInput = Input.GetAxisRaw("Vertical");
     horizontalInput = Input.GetAxisRaw("Horizontal");
     Debug.Log("Server Input V: " + verticalInput + " H: " + horizontalInput);    
     
     
     jumpButton = Input.GetButton("Jump");
 
     var tmpVal : int = 0;
     if (jumpButton) tmpVal = 1;
 
     if (verticalInput != lastVerticalInput || horizontalInput != lastHorizontalInput || lastJumpButton != jumpButton) {
         if (networkView != NetworkViewID.unassigned)
             networkView.RPC("SendUserInput", RPCMode.Server, horizontalInput, verticalInput, tmpVal);
     }
     
     lastJumpButton = jumpButton;
     lastVerticalInput = verticalInput;
     lastHorizontalInput = horizontalInput;    
 }
 
 @RPC
 function SendUserInput(h : float, v : float, j : int)
 {
     targetController.horizontalInput = h;
     targetController.verticalInput = v;
     if (j==1) 
         targetController.jumpButton = true;
     else 
         targetController.jumpButton = false;
 }

cThirdPersonController

 // The speed when walking
 var walkSpeed = 3.0;
 // after trotAfterSeconds of walking we trot with trotSpeed
 var trotSpeed = 4.0;
 // when pressing "Fire3" button (cmd) we start running
 var runSpeed = 6.0;
 
 var inAirControlAcceleration = 3.0;
 
 // How high do we jump when pressing jump and letting go immediately
 var jumpHeight = 0.5;
 // We add extraJumpHeight meters on top when holding the button down longer while jumping
 var extraJumpHeight = 2.5;
 
 // The gravity for the character
 var gravity = 20.0;
 // The gravity in cape fly mode
 var capeFlyGravity = 2.0;
 var speedSmoothing = 10.0;
 var rotateSpeed = 500.0;
 var trotAfterSeconds = 3.0;
 
 var canJump = true;
 var canCapeFly = true;
 var canWallJump = false;
 
 private var jumpRepeatTime = 0.05;
 private var wallJumpTimeout = 0.15;
 private var jumpTimeout = 0.15;
 private var groundedTimeout = 0.25;
 
 // The camera doesnt start following the target immediately but waits for a split second to avoid too much waving around.
 private var lockCameraTimer = 0.0;
 
 // The current move direction in x-z
 private var moveDirection = Vector3.zero;
 // The current vertical speed
 private var verticalSpeed = 0.0;
 // The current x-z move speed
 private var moveSpeed = 0.0;
 
 // The last collision flags returned from controller.Move
 private var collisionFlags : CollisionFlags; 
 
 // Are we jumping? (Initiated with jump button and not grounded yet)
 private var jumping = false;
 private var jumpingReachedApex = false;
 
 // Are we moving backwards (This locks the camera to not do a 180 degree spin)
 private var movingBack = false;
 // Is the user pressing any keys?
 public var isMoving = false;
 // When did the user start walking (Used for going into trot after a while)
 private var walkTimeStart = 0.0;
 // Last time the jump button was clicked down
 private var lastJumpButtonTime = -10.0;
 // Last time we performed a jump
 private var lastJumpTime = -1.0;
 // Average normal of the last touched geometry
 private var wallJumpContactNormal : Vector3;
 private var wallJumpContactNormalHeight : float;
 
 // the height we jumped from (Used to determine for how long to apply extra jump power after jumping.)
 private var lastJumpStartHeight = 0.0;
 // When did we touch the wall the first time during this jump (Used for wall jumping)
 private var touchWallJumpTime = -1.0;
 
 private var inAirVelocity = Vector3.zero;
 
 private var lastGroundedTime = 0.0;
 
 private var lean = 0.0;
 
 // The vertical/horizontal input axes and jump button from user input, synchronized over network
 public var verticalInput : float;
 public var horizontalInput : float;
 public var jumpButton : boolean;
 
 public var getUserInput : boolean = true;
 
 function Awake ()
 {
     //moveDirection = transform.TransformDirection(Vector3.forward);
 }
 
 function UpdateSmoothedMovementDirection ()
 {
     var cameraTransform = Camera.main.transform;
     var grounded = IsGrounded();
     
     // Forward vector relative to the camera along the x-z plane    
     var forward = cameraTransform.TransformDirection(Vector3.forward);
     forward.y = 0;
     forward = forward.normalized;
 
     // Right vector relative to the camera
     // Always orthogonal to the forward vector
     var right = Vector3(forward.z, 0, -forward.x);
 
     if (getUserInput) {
         verticalInput = Input.GetAxisRaw("Vertical");
         horizontalInput = Input.GetAxisRaw("Horizontal");
         Debug.Log("Client Input V: " + verticalInput + " H: " + horizontalInput);
     }
 
     // Are we moving backwards or looking backwards
     if (verticalInput < -0.2)
         movingBack = true;
     else
         movingBack = false;
     
     var wasMoving = isMoving;
     isMoving = Mathf.Abs (horizontalInput) > 0.1 || Mathf.Abs (verticalInput) > 0.1;
         
     // Target direction relative to the camera
     var targetDirection = horizontalInput * right + verticalInput * forward;
     
     // Grounded controls
     if (grounded)
     {
         // Lock camera for short period when transitioning moving & standing still
         lockCameraTimer += Time.deltaTime;
         if (isMoving != wasMoving)
             lockCameraTimer = 0.0;
 
         // We store speed and direction seperately,
         // so that when the character stands still we still have a valid forward direction
         // moveDirection is always normalized, and we only update it if there is user input.
         if (targetDirection != Vector3.zero)
         {
             // If we are really slow, just snap to the target direction
             if (moveSpeed < walkSpeed * 0.9 && grounded)
             {
                 moveDirection = targetDirection.normalized;
             }
             // Otherwise smoothly turn towards it
             else
             {
                 moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
                 
                 moveDirection = moveDirection.normalized;
             }
         }
         
         // Smooth the speed based on the current target direction
         var curSmooth = speedSmoothing * Time.deltaTime;
         
         // Choose target speed
         //* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
         var targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0);
     
         // Pick speed modifier
         if (Input.GetButton ("Fire3"))
         {
             targetSpeed *= runSpeed;
         }
         else if (Time.time - trotAfterSeconds > walkTimeStart)
         {
             targetSpeed *= trotSpeed;
         }
         else
         {
             targetSpeed *= walkSpeed;
         }
         
         moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
         
         // Reset walk time start when we slow down
         if (moveSpeed < walkSpeed * 0.3)
             walkTimeStart = Time.time;
     }
     // In air controls
     else
     {
         // Lock camera while in air
         if (jumping)
             lockCameraTimer = 0.0;
 
         if (isMoving)
             inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration;
     }
 }
 
     
 function Update() {
 
     if (getUserInput) {
         if (Input.GetButtonDown ("Jump"))
             lastJumpButtonTime = Time.time;
     }
     else {
         if (jumpButton)
             lastJumpButtonTime = Time.time;
     }
     
 
     UpdateSmoothedMovementDirection();
     
     // Apply gravity
     // - extra power jump modifies gravity
     // - capeFly mode modifies gravity
     ApplyGravity ();
 
     // Perform a wall jump logic
     // - Make sure we are jumping against wall etc.
     // - Then apply jump in the right direction)
     if (canWallJump)
         ApplyWallJump();
 
     // Apply jumping logic
     ApplyJumping ();
     
     // Calculate actual motion
     var movement = moveDirection * moveSpeed + Vector3 (0, verticalSpeed, 0) + inAirVelocity;
     movement *= Time.deltaTime;
     
     // Move the controller
     var controller : CharacterController = GetComponent(CharacterController);
     wallJumpContactNormal = Vector3.zero;
     collisionFlags = controller.Move(movement);
     
     //networkView.RPC("SendLocation", RPCMode.Server, movement);
     
     // Set rotation to the move direction
     if (IsGrounded() && moveDirection != Vector3.zero)
     {
         transform.rotation = Quaternion.LookRotation(moveDirection);
         //networkView.RPC("SendRotation", RPCMode.Server, Quaternion.LookRotation(moveDirection));
     }    
     else
     {
         var xzMove = movement;
         xzMove.y = 0;
         if (xzMove.magnitude > 0.001)
         {
             transform.rotation = Quaternion.LookRotation(xzMove);
             //networkView.RPC("SendRotation", RPCMode.Server, Quaternion.LookRotation(xzMove));
         }
     }    
     
     // We are in jump mode but just became grounded
     if (IsGrounded())
     {
         lastGroundedTime = Time.time;
         inAirVelocity = Vector3.zero;
         if (jumping)
         {
             jumping = false;
             SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
         }
     }
 }
 
 @RPC
 function SendLocation(v : Vector3)
 {
     var controller : CharacterController = GetComponent(CharacterController);
     wallJumpContactNormal = Vector3.zero;
     collisionFlags = controller.Move(v);
 }

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 anomaly_a · Oct 26, 2012 at 12:02 AM

So if anyone is interested in what the problem is, I figured it out.

 // Forward vector relative to the camera along the x-z plane   
 var forward = cameraTransform.TransformDirection(Vector3.forward);


As the comment mentions, this sets the motion relative to the Camera. Since the "camera" in unity is the same camera for everyone, when I do this I make the server camera the axis for the motion on the server. Since I have the server camera mounted in a static position, forward was always the same and thus saw the problem.

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

9 People are following this question.

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

Related Questions

Auth Server or not? -1 Answers

[Solved]NetWork not smooth 1 Answer

Most efficient way to structure a huge online "virtual world" 0 Answers

Optimize iPad multiplayer? 1 Answer

Different movement speeds over network 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