Changed movement to be easier to optimize for networking.

This commit is contained in:
excitedneon 2017-05-07 22:53:01 +03:00
parent f74c7e1799
commit 2bb1df00ee
2 changed files with 29 additions and 5 deletions

View File

@ -6,11 +6,32 @@ public class Character : SyncBase {
public float MovementSpeed = 5.0f;
public CharacterController CharacterController;
private Vector3 MovementDirection = new Vector3();
/// <summary>
/// Moves the character in the wanted direction. Should be called on the physics tick. (FixedUpdate)
/// Moves the character in the wanted direction.
/// </summary>
/// <param name="Direction">Movement direction.</param>
public void Move(Vector3 Direction) {
CharacterController.Move(Direction.normalized * MovementSpeed * Time.fixedDeltaTime);
if (!Direction.Equals(MovementDirection)) {
MovementDirection = Direction.normalized;
}
}
/// <summary>
/// Stops the player from moving.
/// </summary>
public void Stop() {
if (Moving()) {
MovementDirection = new Vector3();
}
}
public bool Moving() {
return MovementDirection.sqrMagnitude != 0;
}
private void FixedUpdate() {
CharacterController.Move(MovementDirection * MovementSpeed * Time.fixedDeltaTime);
}
}

View File

@ -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();
}
}
}