Skip to content

Instantly share code, notes, and snippets.

@zaun
Last active March 12, 2017 03:07
Show Gist options
  • Save zaun/52520ebff602ec566da06be46662bb1a to your computer and use it in GitHub Desktop.
Save zaun/52520ebff602ec566da06be46662bb1a to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
[RequireComponent (typeof (Rigidbody))]
public class Player : LivingEntity {
[Header("Hoover")]
public Transform[] hooverPoints;
public Transform centerPoint;
public float hooverForce = 12500f;
public float hooverHeight = 2f;
public float groundedThreshold = 4f;
[Range(0, 5)]
public float maxTilt = 1;
[Header("Player")]
public float moveSpeed = 25;
public float angularSpeed = 35;
public float jumpSpeed = 6;
public float jumpTime = 0.4f;
[Header("Camera")]
public float cameraDistance = 80;
public float cameraHeight = 35;
private Camera _camera;
private CameraController _cameraController;
private GunController _gunController;
private Rigidbody _rigidbody;
private float _jumpTimeLeft = 0.4f;
private int _groundLayer;
[SyncVar]
private bool _active = false;
[SyncVar]
private bool _moveForward = false;
[SyncVar]
private bool _moveBackward = false;
[SyncVar]
private bool _turnLeft = false;
[SyncVar]
private bool _turnRight = false;
[SyncVar]
public bool _isGrounded = false;
[SyncVar]
private bool _isJumping = false;
[SyncVar]
private bool _isShooting = false;
[SyncVar]
private bool _jump = false;
[SyncVar]
private bool _shoot = false;
private Quaternion q;
void Start () {
_camera = Camera.main;
_gunController = GetComponent<GunController> ();
_cameraController = _camera.GetComponent<CameraController> ();
if (isLocalPlayer) {
_camera.transform.position = transform.position;
_camera.transform.rotation = transform.rotation;
_cameraController.target = transform;
_cameraController.distance = cameraDistance;
_cameraController.height = cameraHeight;
gameObject.AddComponent<AudioListener> ();
}
_rigidbody = gameObject.GetComponent<Rigidbody> ();
_groundLayer = LayerMask.NameToLayer("Ground");
q = transform.rotation;
// Set the player as active
if (isServer) {
_active = true;
}
}
void Update () {
if (!isLocalPlayer || !_active) {
return;
}
if (InputMaster.GetKeyDown ("forward")) {
CmdSetMoveForward (true);
}
if (InputMaster.GetKeyUp ("forward")) {
CmdSetMoveForward (false);
}
if (InputMaster.GetKeyDown ("backward")) {
CmdSetMoveBackward (true);
}
if (InputMaster.GetKeyUp ("backward")) {
CmdSetMoveBackward (false);
}
if (InputMaster.GetKeyDown ("left")) {
CmdSetTurnLeft (true);
}
if (InputMaster.GetKeyUp ("left")) {
CmdSetTurnLeft (false);
}
if (InputMaster.GetKeyDown ("right")) {
CmdSetTurnRight (true);
}
if (InputMaster.GetKeyUp ("right")) {
CmdSetTurnRight (false);
}
if (InputMaster.GetKeyDown ("jump")) {
CmdSetJump (true);
}
if (InputMaster.GetKeyUp ("jump")) {
CmdSetJump (false);
}
if (InputMaster.GetKeyDown ("fire")) {
CmdSetShooting(true);
}
if (InputMaster.GetKeyUp ("fire")) {
CmdSetShooting(false);
}
if (!isServer) {
return;
}
// Keep things level
if (q.x - transform.rotation.x > maxTilt) {
transform.rotation = new Quaternion (q.x + maxTilt, transform.rotation.y, transform.rotation.z, transform.rotation.w);
}
if (q.x - transform.rotation.x < -maxTilt) {
transform.rotation = new Quaternion (q.x - maxTilt, transform.rotation.y, transform.rotation.z, transform.rotation.w);
}
if (q.z - transform.rotation.z > maxTilt) {
transform.rotation = new Quaternion (transform.rotation.x, transform.rotation.y, q.z + maxTilt, transform.rotation.w);
}
if (q.z - transform.rotation.z < -maxTilt) {
transform.rotation = new Quaternion (transform.rotation.x, transform.rotation.y, q.z - maxTilt, transform.rotation.w);
}
}
/// <summary>
/// Only the server should update the player's location
/// the transform is synced to the clients
/// </summary>
void FixedUpdate () {
if (!isServer) {
return;
}
CheckGrounded ();
Hoover ();
if (_moveForward) {
float moveAmount = moveSpeed * Time.deltaTime;
_rigidbody.MovePosition (_rigidbody.position + _rigidbody.transform.forward * moveAmount);
}
if (_moveBackward) {
float moveAmount = (-moveSpeed * 0.6f) * Time.deltaTime;
_rigidbody.MovePosition (_rigidbody.position + _rigidbody.transform.forward * moveAmount);
}
if (_turnLeft) {
Quaternion rotateAmount = Quaternion.Euler (new Vector3 (0f, -angularSpeed, 0f) * Time.deltaTime);
_rigidbody.MoveRotation (_rigidbody.rotation * rotateAmount);
}
if (_turnRight) {
Quaternion rotateAmount = Quaternion.Euler (new Vector3 (0f, angularSpeed, 0f) * Time.deltaTime);
_rigidbody.MoveRotation (_rigidbody.rotation * rotateAmount);
}
if (_jump && _isGrounded) {
_isJumping = true;
}
if (_isJumping && _jumpTimeLeft > 0) {
float moveAmount = jumpSpeed * Time.deltaTime;
_rigidbody.MovePosition (_rigidbody.position + _rigidbody.transform.up * moveAmount);
_jumpTimeLeft -= Time.deltaTime;
} else if (_isJumping) {
_isJumping = false;
_jumpTimeLeft = jumpTime;
}
}
void CheckGrounded() {
Ray ray = new Ray(centerPoint.position, -centerPoint.up);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, groundedThreshold, _groundLayer)) {
_isGrounded = true;
} else {
_isGrounded = false;
}
}
void Hoover() {
foreach (Transform hoverPoint in hooverPoints) {
_rigidbody.AddForceAtPosition (-hoverPoint.up * hooverForce / 10, hoverPoint.position, ForceMode.Force);
Ray ray = new Ray (hoverPoint.position, -hoverPoint.up);
RaycastHit hitInfo;
if (Physics.Raycast (ray, out hitInfo, hooverHeight)) {
float distance = Vector3.Distance (hoverPoint.position, hitInfo.point);
if (distance < hooverHeight) {
_rigidbody.AddForceAtPosition (hoverPoint.up * hooverForce * (1f - distance / hooverHeight), hoverPoint.position, ForceMode.Force);
}
}
}
}
void OnDrawGizmos () {
foreach (Transform hoverPoint in hooverPoints) {
Gizmos.color = Color.yellow;
Gizmos.DrawSphere (hoverPoint.position, 0.25f);
Gizmos.DrawLine (hoverPoint.position, hoverPoint.position + (-hoverPoint.up * hooverHeight));
}
}
/// <summary>
/// Client -> Server
/// Move the player to a spawn location
/// </summary>
void CmdSpawn() {
// _rigidbody.position = _map.GetPlayerSpawn();
_rigidbody.velocity = Vector3.zero;
}
/// <summary>
/// Client -> Server
/// Set the forward move of the player on/off
/// </summary>
[Command]
void CmdSetMoveForward (bool active) {
_moveForward = active;
}
/// <summary>
/// Client -> Server
/// Set the backward of the player on/off
/// </summary>
[Command]
void CmdSetMoveBackward (bool active) {
_moveBackward = active;
}
/// <summary>
/// Client -> Server
/// Set the left turn of the player on/off
/// </summary>
[Command]
void CmdSetTurnLeft (bool active) {
_turnLeft = active;
}
/// <summary>
/// Client -> Server
/// Set the right turn of the player on/off
/// </summary>
[Command]
void CmdSetTurnRight (bool active) {
_turnRight = active;
}
/// <summary>
/// Client -> Server
/// Set the jumpping of the player on/off
/// </summary>
[Command]
void CmdSetJump (bool active) {
_jump = active;
}
/// <summary>
/// Client -> Server
/// Set shooting weapon on/off
/// </summary>
[Command]
void CmdSetShooting (bool active) {
_shoot = active;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment