Author Topic: Character controller  (Read 6708 times)

Noi14

  • Jr. Member
  • **
  • Thank You
  • -Given: 10
  • -Receive: 0
  • Posts: 57
    • View Profile
Character controller
« on: February 03, 2014, 09:24:34 AM »
I'm doing a MMO game online and I do in this mode the movement of characters, but sometimes movement of other characters aren't perfect, and I have to insert also TN Auto Synic at 1second in order to make sure that the characters are exactly where they should be. Is it normal this problem?
  1. using UnityEngine;
  2. using System.Collections;
  3. using TNet;
  4.  
  5. public class GamePlayer : TNBehaviour {
  6.         static public GamePlayer istance;
  7.        
  8.         public float speed = 80.0F;
  9.         public float gravity = 20.0F;
  10.  
  11.         // Movimenti
  12.         private CharacterController controller;
  13.         private Vector3 moveDirection = Vector3.zero;
  14.         private Vector3 movimento = new Vector3(0,0,0);
  15.  
  16.         //
  17.         void Awake()
  18.         {
  19.                 istance = this;
  20.                 controller = GetComponent<CharacterController>();
  21.         }
  22.  
  23.         void Start()
  24.         {
  25.                 //
  26.                 if(!tno.isMine)
  27.                 {
  28.                         Camera[] allChildren = GetComponentsInChildren<Camera>();
  29.                         foreach (Camera child in allChildren) {
  30.                                 child.enabled = false;
  31.                         }
  32.                 }
  33.         }
  34.  
  35.         //
  36.         void Update()
  37.         {
  38.                 if(tno.isMine)
  39.                 {
  40.                         Movimenti();
  41.                 }
  42.  
  43.  
  44.                 // Movimento
  45.                 if(Mathf.Abs(movimento.x) > 0.8 || Mathf.Abs(movimento.z) > 0.8)
  46.                 {
  47.                         controller.Move(movimento * Time.deltaTime);
  48.                 }
  49.         }
  50.         void Movimenti() {
  51.                 moveDirection = new Vector3(0, 0, 0);
  52.                 float input_v = Input.GetAxis("Vertical");
  53.  
  54.                 //
  55.                 if(input_v != 0)
  56.                 {
  57.                         // Muovo avanti/indietro in base all'input
  58.                         moveDirection = new Vector3(0, 0, input_v);
  59.                         moveDirection = transform.TransformDirection(moveDirection);
  60.  
  61.                         // Velocità di movimento
  62.                         moveDirection *= speed;
  63.                 }
  64.  
  65.  
  66.  
  67.                 // Making the character move
  68.                 if(moveDirection != Vector3.zero)
  69.                 {
  70.                         // Gravity
  71.                         moveDirection.y -= gravity;
  72.  
  73.                         tno.Send("OnSetDestinazioneMovimento", TNet.Target.Others, moveDirection);
  74.                         OnSetDestinazioneMovimento(moveDirection);
  75.                 }
  76.         }
  77.         [RFC]
  78.         void OnSetDestinazioneMovimento(Vector3 destinazione)
  79.         {
  80.                 movimento = destinazione;
  81.  
  82.         }
  83. }
  84.  
  85.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Character controller
« Reply #1 on: February 04, 2014, 03:56:19 AM »
Yes, that's the advised approach. Sync the movement direction when it changes, and periodically sync the position/rotation of the object.

Noi14

  • Jr. Member
  • **
  • Thank You
  • -Given: 10
  • -Receive: 0
  • Posts: 57
    • View Profile
Re: Character controller
« Reply #2 on: February 04, 2014, 05:19:28 AM »
Thank for your reply. I have another problem. I'm using controller.Move(movimento * Time.deltaTime); into Update(); in order to move character.
Sometimes movimento.x e movimento.z keeps values like 0.4 and they don't decrease so it will causing a continous movement even if I don't have input from keyboard. Is it normal?
In order to work around the problem I have added "IF", but sometimes there is this problem.
I noticed that it doesn't happen when I work without Tnet.

Noi14

  • Jr. Member
  • **
  • Thank You
  • -Given: 10
  • -Receive: 0
  • Posts: 57
    • View Profile
Re: Character controller
« Reply #3 on: February 04, 2014, 11:57:37 AM »
I found the problem and there isn't a problem of multiplayer. Character remains in movement because when I click off the Webplayer and I hold inputs for the movement

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Character controller
« Reply #4 on: February 05, 2014, 04:47:49 PM »
That's because "run in background" must be checked in the build options.