From 2bb1df00eefd52963e10fbbe8faccd45ad8e1461 Mon Sep 17 00:00:00 2001 From: excitedneon Date: Sun, 7 May 2017 22:53:01 +0300 Subject: [PATCH] Changed movement to be easier to optimize for networking. --- Assets/Scripts/Character.cs | 25 +++++++++++++++++++++++-- Assets/Scripts/PlayerController.cs | 9 ++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Assets/Scripts/Character.cs b/Assets/Scripts/Character.cs index 5c2f906..26f9a73 100644 --- a/Assets/Scripts/Character.cs +++ b/Assets/Scripts/Character.cs @@ -6,11 +6,32 @@ public class Character : SyncBase { public float MovementSpeed = 5.0f; public CharacterController CharacterController; + private Vector3 MovementDirection = new Vector3(); + /// - /// Moves the character in the wanted direction. Should be called on the physics tick. (FixedUpdate) + /// Moves the character in the wanted direction. /// /// Movement direction. public void Move(Vector3 Direction) { - CharacterController.Move(Direction.normalized * MovementSpeed * Time.fixedDeltaTime); + if (!Direction.Equals(MovementDirection)) { + MovementDirection = Direction.normalized; + } + } + + /// + /// Stops the player from moving. + /// + public void Stop() { + if (Moving()) { + MovementDirection = new Vector3(); + } + } + + public bool Moving() { + return MovementDirection.sqrMagnitude != 0; + } + + private void FixedUpdate() { + CharacterController.Move(MovementDirection * MovementSpeed * Time.fixedDeltaTime); } } diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index bc0f415..a9883ec 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -5,9 +5,12 @@ using UnityEngine; public class PlayerController : MonoBehaviour { public Character Character; - void FixedUpdate() { + void Update() { Vector3 Move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); - Move = transform.TransformDirection(Move); - Character.Move(Move); + if (Move.sqrMagnitude != 0) { + Character.Move(transform.TransformDirection(Move)); + } else if (Character.Moving()) { + Character.Stop(); + } } }