2017-05-08 23:14:30 +02:00
|
|
|
|
using UnityEngine;
|
2017-05-08 23:03:02 +02:00
|
|
|
|
|
|
|
|
|
namespace Cyber.Entities {
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A syncable component that all characters have. Controls the character's subsystems.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Character : SyncBase {
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// How fast the player should move in Unity's spatial units per second.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public float MovementSpeed = 5.0f;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The character controller, used to move the character. Handles collisions.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public CharacterController CharacterController;
|
|
|
|
|
|
2017-05-09 12:11:36 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The head transform for looking around.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Transform Head;
|
|
|
|
|
|
2017-05-08 23:03:02 +02:00
|
|
|
|
private Vector3 MovementDirection = new Vector3();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Moves the character in the given direction.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="Direction">Movement direction.</param>
|
|
|
|
|
public void Move(Vector3 Direction) {
|
|
|
|
|
if (!Direction.Equals(MovementDirection)) {
|
|
|
|
|
MovementDirection = Direction.normalized;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stops the player from moving.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Stop() {
|
|
|
|
|
if (Moving()) {
|
|
|
|
|
MovementDirection = new Vector3();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-09 12:11:36 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the character's rotation.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="EulerAngles">Rotation in euler angles.</param>
|
|
|
|
|
public void SetRotation(Vector3 EulerAngles) {
|
2017-05-09 12:30:56 +02:00
|
|
|
|
Vector3 HeadRot = Head.localEulerAngles;
|
|
|
|
|
HeadRot.x = EulerAngles.x;
|
|
|
|
|
HeadRot.z = EulerAngles.z;
|
|
|
|
|
Head.localEulerAngles = HeadRot;
|
|
|
|
|
|
|
|
|
|
Vector3 BodyRot = transform.localEulerAngles;
|
|
|
|
|
BodyRot.y = EulerAngles.y;
|
|
|
|
|
transform.localEulerAngles = BodyRot;
|
2017-05-09 12:11:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 23:03:02 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether the player is moving or not.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool Moving() {
|
|
|
|
|
return MovementDirection.sqrMagnitude != 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-09 12:30:56 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The character's rotation. Intended to be given as an input to
|
|
|
|
|
/// <see cref="SetRotation"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The rotation.</returns>
|
|
|
|
|
public Vector3 GetRotation() {
|
2017-05-09 12:32:39 +02:00
|
|
|
|
return new Vector3(Head.localEulerAngles.x,
|
2017-05-09 12:30:56 +02:00
|
|
|
|
transform.localEulerAngles.y, Head.localEulerAngles.z);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 23:03:02 +02:00
|
|
|
|
private void FixedUpdate() {
|
|
|
|
|
CharacterController.Move(MovementDirection * MovementSpeed * Time.fixedDeltaTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|