Search Unity

Problem of movement multiplayer 3D Unity

Discussion in 'Multiplayer' started by chrisulfarks, Mar 23, 2015.

  1. chrisulfarks

    chrisulfarks

    Joined:
    Mar 23, 2015
    Posts:
    2
    Good evening, excuse I if my English is a little bit school, but I am going to try to explain you clearly my problem..

    I know that we already have certainly to speak about this subject, but even by following all the recommendations, I do not see or I have to put this end of code:

    Code (CSharp):
    1. if(networkView.isMine){
    2.  
    3. // Move scripts
    4.  
    5. }
    My problem is the following one: in solo mode(fashion) I travel(movement) is first-class, the game(set,play) works as I wish it, on the other hand in mode(fashion) multiplayer on a network, the waiter(server) checks(controls) all the characters I tried in vain to solve this problem for quite a while and I think that I puts the code above often in the bad script or still that I do not know how to place him(it) lol..

    If you can light me so that I can solve this concern :) thank you beforehand!

    I show you my code:


    Code (CSharp):
    1. // Require a character controller to be attached to the same game object
    2. @script RequireComponent(CharacterController)
    3. public var idleAnimation : AnimationClip;
    4. public var walkAnimation : AnimationClip;
    5. public var runAnimation : AnimationClip;
    6. public var jumpPoseAnimation : AnimationClip;
    7. public var walkMaxAnimationSpeed : float = 0.75;
    8. public var trotMaxAnimationSpeed : float = 1.0;
    9. public var runMaxAnimationSpeed : float = 1.0;
    10. public var jumpAnimationSpeed : float = 1.15;
    11. public var landAnimationSpeed : float = 1.0;
    12.  
    13. private var _animation : Animation;
    14.  
    15. enum CharacterState {
    16.     Idle = 0,
    17.     Walking = 1,
    18.     Trotting = 2,
    19.     Running = 3,
    20.     Jumping = 4,
    21. }
    22.  
    23. private var _characterState : CharacterState;
    24. // The speed when walking
    25. var walkSpeed = 2.0;
    26. // after trotAfterSeconds of walking we trot with trotSpeed
    27. var trotSpeed = 4.0;
    28. // when pressing "Fire3" button (cmd) we start running
    29. var runSpeed = 6.0;
    30. var inAirControlAcceleration = 3.0;
    31. // How high do we jump when pressing jump and letting go immediately
    32. var jumpHeight = 0.5;
    33. // The gravity for the character
    34. var gravity = 20.0;
    35. // The gravity in controlled descent mode
    36. var speedSmoothing = 10.0;
    37. var rotateSpeed = 500.0;
    38. var trotAfterSeconds = 3.0;
    39. var canJump = true;
    40. private var jumpRepeatTime = 0.05;
    41. private var jumpTimeout = 0.15;
    42. private var groundedTimeout = 0.25;
    43. // The camera doesnt start following the target immediately but waits for a split second to avoid too much waving around.
    44. private var lockCameraTimer = 0.0;
    45. // The current move direction in x-z
    46. private var moveDirection = Vector3.zero;
    47. // The current vertical speed
    48. private var verticalSpeed = 0.0;
    49. // The current x-z move speed
    50. private var moveSpeed = 0.0;
    51. // The last collision flags returned from controller.Move
    52. private var collisionFlags : CollisionFlags;
    53. // Are we jumping? (Initiated with jump button and not grounded yet)
    54. private var jumping = false;
    55. private var jumpingReachedApex = false;
    56. // Are we moving backwards (This locks the camera to not do a 180 degree spin)
    57. private var movingBack = false;
    58. // Is the user pressing any keys?
    59. private var isMoving = false;
    60. // When did the user start walking (Used for going into trot after a while)
    61. private var walkTimeStart = 0.0;
    62. // Last time the jump button was clicked down
    63. private var lastJumpButtonTime = -10.0;
    64. // Last time we performed a jump
    65. private var lastJumpTime = -1.0;
    66. // the height we jumped from (Used to determine for how long to apply extra jump power after jumping.)
    67. private var lastJumpStartHeight = 0.0;
    68. private var inAirVelocity = Vector3.zero;
    69. private var lastGroundedTime = 0.0;
    70. private var isControllable = true;
    71. function Awake ()
    72. {
    73.     if(transform.networkView.isMine==true && Camera.main.networkView.isMine==true){
    74.         moveDirection = transform.TransformDirection(Vector3.forward);
    75.     }
    76.     _animation = GetComponent(Animation);
    77.     if(!_animation)
    78.         Debug.Log("The character you would like to control doesn't have animations. Moving her might look weird.");
    79.    
    80.     /*
    81. public var idleAnimation : AnimationClip;
    82. public var walkAnimation : AnimationClip;
    83. public var runAnimation : AnimationClip;
    84. public var jumpPoseAnimation : AnimationClip;
    85.     */
    86.     if(!idleAnimation) {
    87.         _animation = null;
    88.         Debug.Log("No idle animation found. Turning off animations.");
    89.     }
    90.     if(!walkAnimation) {
    91.         _animation = null;
    92.         Debug.Log("No walk animation found. Turning off animations.");
    93.     }
    94.     if(!runAnimation) {
    95.         _animation = null;
    96.         Debug.Log("No run animation found. Turning off animations.");
    97.     }
    98.     if(!jumpPoseAnimation && canJump) {
    99.         _animation = null;
    100.         Debug.Log("No jump animation found and the character has canJump enabled. Turning off animations.");
    101.     }
    102.            
    103. }
    104. function UpdateSmoothedMovementDirection ()
    105. {
    106.     var cameraTransform = Camera.main.transform;
    107.     var grounded = IsGrounded();
    108.    
    109.     // Forward vector relative to the camera along the x-z plane  
    110.     var forward = cameraTransform.TransformDirection(Vector3.forward);
    111.     forward.y = 0;
    112.     forward = forward.normalized;
    113.     // Right vector relative to the camera
    114.     // Always orthogonal to the forward vector
    115.     var right = Vector3(forward.z, 0, -forward.x);
    116.     var v = Input.GetAxisRaw("Vertical");
    117.     var h = Input.GetAxisRaw("Horizontal");
    118.     // Are we moving backwards or looking backwards
    119.     if (v < -0.2)
    120.         movingBack = true;
    121.     else
    122.         movingBack = false;
    123.    
    124.     var wasMoving = isMoving;
    125.     isMoving = Mathf.Abs (h) > 0.1 || Mathf.Abs (v) > 0.1;
    126.        
    127.     // Target direction relative to the camera
    128.     var targetDirection = h * right + v * forward;
    129.    
    130.     // Grounded controls
    131.     if (grounded)
    132.     {
    133.         // Lock camera for short period when transitioning moving & standing still
    134.         lockCameraTimer += Time.deltaTime;
    135.         if (isMoving != wasMoving)
    136.             lockCameraTimer = 0.0;
    137.         // We store speed and direction seperately,
    138.         // so that when the character stands still we still have a valid forward direction
    139.         // moveDirection is always normalized, and we only update it if there is user input.
    140.         if (targetDirection != Vector3.zero)
    141.         {
    142.             // If we are really slow, just snap to the target direction
    143.             if (moveSpeed < walkSpeed * 0.9 && grounded)
    144.             {
    145.                 moveDirection = targetDirection.normalized;
    146.             }
    147.             // Otherwise smoothly turn towards it
    148.             else
    149.             {
    150.                 moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
    151.                
    152.                 moveDirection = moveDirection.normalized;
    153.             }
    154.         }
    155.        
    156.         // Smooth the speed based on the current target direction
    157.         var curSmooth = speedSmoothing * Time.deltaTime;
    158.        
    159.         // Choose target speed
    160.         //* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
    161.         var targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0);
    162.    
    163.         _characterState = CharacterState.Idle;
    164.        
    165.         // Pick speed modifier
    166.         if (Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift))
    167.         {
    168.             targetSpeed *= runSpeed;
    169.             _characterState = CharacterState.Running;
    170.         }
    171.         else if (Time.time - trotAfterSeconds > walkTimeStart)
    172.         {
    173.             targetSpeed *= trotSpeed;
    174.             _characterState = CharacterState.Trotting;
    175.         }
    176.         else
    177.         {
    178.             targetSpeed *= walkSpeed;
    179.             _characterState = CharacterState.Walking;
    180.         }
    181.        
    182.         moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
    183.        
    184.         // Reset walk time start when we slow down
    185.         if (moveSpeed < walkSpeed * 0.3)
    186.             walkTimeStart = Time.time;
    187.     }
    188.     // In air controls
    189.     else
    190.     {
    191.         // Lock camera while in air
    192.         if (jumping)
    193.             lockCameraTimer = 0.0;
    194.         if (isMoving)
    195.             inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration;
    196.     }
    197.    
    198.        
    199. }
    200. function ApplyJumping ()
    201. {
    202.     // Prevent jumping too fast after each other
    203.     if (lastJumpTime + jumpRepeatTime > Time.time)
    204.         return;
    205.     if (IsGrounded()) {
    206.         // Jump
    207.         // - Only when pressing the button down
    208.         // - With a timeout so you can press the button slightly before landing  
    209.         if (canJump && Time.time < lastJumpButtonTime + jumpTimeout) {
    210.             verticalSpeed = CalculateJumpVerticalSpeed (jumpHeight);
    211.             SendMessage("DidJump", SendMessageOptions.DontRequireReceiver);
    212.         }
    213.     }
    214. }
    215. function ApplyGravity ()
    216. {
    217.     if (isControllable) // don't move player at all if not controllable.
    218.     {
    219.         // Apply gravity
    220.         var jumpButton = Input.GetButton("Jump");
    221.        
    222.        
    223.         // When we reach the apex of the jump we send out a message
    224.         if (jumping && !jumpingReachedApex && verticalSpeed <= 0.0)
    225.         {
    226.             jumpingReachedApex = true;
    227.             SendMessage("DidJumpReachApex", SendMessageOptions.DontRequireReceiver);
    228.         }
    229.    
    230.         if (IsGrounded ())
    231.             verticalSpeed = 0.0;
    232.         else
    233.             verticalSpeed -= gravity * Time.deltaTime;
    234.     }
    235. }
    236. function CalculateJumpVerticalSpeed (targetJumpHeight : float)
    237. {
    238.     // From the jump height and gravity we deduce the upwards speed
    239.     // for the character to reach at the apex.
    240.     return Mathf.Sqrt(2 * targetJumpHeight * gravity);
    241. }
    242. function DidJump ()
    243. {
    244.     jumping = true;
    245.     jumpingReachedApex = false;
    246.     lastJumpTime = Time.time;
    247.     lastJumpStartHeight = transform.position.y;
    248.     lastJumpButtonTime = -10;
    249.    
    250.     _characterState = CharacterState.Jumping;
    251. }
    252. function Update() {
    253.    
    254.     if (!isControllable)
    255.     {
    256.         // kill all inputs if not controllable.
    257.         Input.ResetInputAxes();
    258.     }
    259.     if (Input.GetButtonDown ("Jump"))
    260.     {
    261.         lastJumpButtonTime = Time.time;
    262.     }
    263.     UpdateSmoothedMovementDirection();
    264.    
    265.     // Apply gravity
    266.     // - extra power jump modifies gravity
    267.     // - controlledDescent mode modifies gravity
    268.     ApplyGravity ();
    269.     // Apply jumping logic
    270.     ApplyJumping ();
    271.    
    272.     // Calculate actual motion
    273.     var movement = moveDirection * moveSpeed + Vector3 (0, verticalSpeed, 0) + inAirVelocity;
    274.     movement *= Time.deltaTime;
    275.    
    276.     // Move the controller
    277.     var controller : CharacterController = GetComponent(CharacterController);
    278.     collisionFlags = controller.Move(movement);
    279.    
    280.     // ANIMATION sector
    281.     if(_animation) {
    282.         if(_characterState == CharacterState.Jumping)
    283.         {
    284.             if(!jumpingReachedApex) {
    285.                 _animation[jumpPoseAnimation.name].speed = jumpAnimationSpeed;
    286.                 _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
    287.                 _animation.CrossFade(jumpPoseAnimation.name);
    288.             } else {
    289.                 _animation[jumpPoseAnimation.name].speed = -landAnimationSpeed;
    290.                 _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
    291.                 _animation.CrossFade(jumpPoseAnimation.name);            
    292.             }
    293.         }
    294.         else
    295.         {
    296.             if(controller.velocity.sqrMagnitude < 0.1) {
    297.                 _animation.CrossFade(idleAnimation.name);
    298.             }
    299.             else
    300.             {
    301.                 if(_characterState == CharacterState.Running) {
    302.                     _animation[runAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0, runMaxAnimationSpeed);
    303.                     _animation.CrossFade(runAnimation.name);  
    304.                 }
    305.                 else if(_characterState == CharacterState.Trotting) {
    306.                     _animation[walkAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0, trotMaxAnimationSpeed);
    307.                     _animation.CrossFade(walkAnimation.name);
    308.                 }
    309.                 else if(_characterState == CharacterState.Walking) {
    310.                     _animation[walkAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0, walkMaxAnimationSpeed);
    311.                     _animation.CrossFade(walkAnimation.name);
    312.                 }
    313.                
    314.             }
    315.         }
    316.     }
    317.     // ANIMATION sector
    318.    
    319.     // Set rotation to the move direction
    320.     if (IsGrounded())
    321.     {
    322.        
    323.         transform.rotation = Quaternion.LookRotation(moveDirection);
    324.            
    325.     }
    326.     else
    327.     {
    328.         var xzMove = movement;
    329.         xzMove.y = 0;
    330.         if (xzMove.sqrMagnitude > 0.001)
    331.         {
    332.             transform.rotation = Quaternion.LookRotation(xzMove);
    333.         }
    334.     }
    335.    
    336.     // We are in jump mode but just became grounded
    337.     if (IsGrounded())
    338.     {
    339.         lastGroundedTime = Time.time;
    340.         inAirVelocity = Vector3.zero;
    341.         if (jumping)
    342.         {
    343.             jumping = false;
    344.             SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
    345.         }
    346.     }
    347. }
    348. function OnControllerColliderHit (hit : ControllerColliderHit )
    349. {
    350. //  Debug.DrawRay(hit.point, hit.normal);
    351.     if (hit.moveDirection.y > 0.01)
    352.         return;
    353. }
    354. function GetSpeed () {
    355.     return moveSpeed;
    356. }
    357. function IsJumping () {
    358.     return jumping;
    359. }
    360. function IsGrounded () {
    361.     return (collisionFlags & CollisionFlags.CollidedBelow) != 0;
    362. }
    363. function GetDirection () {
    364.     return moveDirection;
    365. }
    366. function IsMovingBackwards () {
    367.     return movingBack;
    368. }
    369. function GetLockCameraTimer ()
    370. {
    371.     return lockCameraTimer;
    372. }
    373. function IsMoving ()  : boolean
    374. {
    375.     return Mathf.Abs(Input.GetAxisRaw("Vertical")) + Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.5;
    376. }
    377. function HasJumpReachedApex ()
    378. {
    379.     return jumpingReachedApex;
    380. }
    381. function IsGroundedWithTimeout ()
    382. {
    383.     return lastGroundedTime + groundedTimeout > Time.time;
    384. }
    385. function Reset ()
    386. {
    387.     gameObject.tag = "Player";
    388. }
     
  2. chrisulfarks

    chrisulfarks

    Joined:
    Mar 23, 2015
    Posts:
    2
    Nobody to help me ?