2017-05-07 18:48:56 +02:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2017-05-08 21:59:35 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// A syncable component that all characters have. Controls the character's subsystems.
|
|
|
|
|
/// </summary>
|
2017-05-07 18:48:56 +02:00
|
|
|
|
public class Character : SyncBase {
|
2017-05-08 21:59:35 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// How fast the player should move in Unity's spatial units per second.
|
|
|
|
|
/// </summary>
|
2017-05-07 21:44:35 +02:00
|
|
|
|
public float MovementSpeed = 5.0f;
|
2017-05-08 21:59:35 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The character controller, used to move the character. Handles collisions.
|
|
|
|
|
/// </summary>
|
2017-05-07 21:44:35 +02:00
|
|
|
|
public CharacterController CharacterController;
|
2017-05-07 18:48:56 +02:00
|
|
|
|
|
2017-05-07 21:53:01 +02:00
|
|
|
|
private Vector3 MovementDirection = new Vector3();
|
|
|
|
|
|
2017-05-07 21:44:35 +02:00
|
|
|
|
/// <summary>
|
2017-05-08 21:59:35 +02:00
|
|
|
|
/// Moves the character in the given direction.
|
2017-05-07 21:44:35 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="Direction">Movement direction.</param>
|
|
|
|
|
public void Move(Vector3 Direction) {
|
2017-05-07 21:53:01 +02:00
|
|
|
|
if (!Direction.Equals(MovementDirection)) {
|
|
|
|
|
MovementDirection = Direction.normalized;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stops the player from moving.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Stop() {
|
|
|
|
|
if (Moving()) {
|
|
|
|
|
MovementDirection = new Vector3();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 21:59:35 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether the player is moving or not.
|
|
|
|
|
/// </summary>
|
2017-05-07 21:53:01 +02:00
|
|
|
|
public bool Moving() {
|
|
|
|
|
return MovementDirection.sqrMagnitude != 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FixedUpdate() {
|
|
|
|
|
CharacterController.Move(MovementDirection * MovementSpeed * Time.fixedDeltaTime);
|
2017-05-07 21:44:35 +02:00
|
|
|
|
}
|
2017-05-07 18:48:56 +02:00
|
|
|
|
}
|