Merge branch 'main' into Player-Animation

This commit is contained in:
juliuse98
2021-11-06 14:57:31 +01:00
78 changed files with 5849 additions and 1334 deletions

View File

@@ -13,10 +13,9 @@ public class GameMaster : MonoBehaviour
{
[Header("GameMaster")]
[SerializeField] private List<Player> Players = new List<Player>();
private void Start()
{
[SerializeField] private int countOfRounds = 10;
}
public GameObject localPlayer;
private void Update()
{
@@ -33,4 +32,7 @@ public class GameMaster : MonoBehaviour
Cursor.visible = false;
}
}
}

View File

@@ -0,0 +1,28 @@
using Mirror;
using UnityEngine;
public class JoinLeaveManager : MonoBehaviour
{
private NetworkManager networkManager;
private void Start()
{
networkManager = GetComponent<NetworkManager>();
}
public void Join(string ip, string username)
{
Debug.Log("[JoinLeaveManager] Trying to join server: " + ip + " as " + username);
networkManager.StartClient();
networkManager.networkAddress = ip;
Debug.Log("[JoinLeaveManager] " + username + " joined the server: " + ip);
}
public void Host()
{
networkManager.StartHost();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b62b46838717d934483010f517c5b772
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,15 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class Manager : NetworkManager
{
public override void OnClientConnect(NetworkConnection conn)
{
base.OnClientConnect(conn);
//Debug.Log(conn.identity.gameObject.GetComponent<Player>().username);
//conn.identity.gameObject.GetComponent<Player>().username = "Test";
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 58bd0a0557e21bf4e8ea0e0cd4e9d057
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -17,7 +17,6 @@ public class Headbob : NetworkBehaviour
[SerializeField] [Range(0.01f, 10.0f)] private float stepFrequency;
[SerializeField] private Transform Neck;
private Vector3 startPos;
private Vector3 lastPos;
private Vector3 newPos;
private float oldDist = 0;
@@ -25,8 +24,6 @@ public class Headbob : NetworkBehaviour
private void Start()
{
lastPos = this.transform.position;
startPos = this.transform.position;
}
private void Update()
@@ -56,7 +53,18 @@ public class Headbob : NetworkBehaviour
}
else
{
Neck.localPosition = startPos;
Neck.localPosition = Vector3.zero;
if (false) {
Neck.localPosition = Vector3.Lerp(newPos, Vector3.zero, lerpVal);
if (lerpVal < 1)
{
lerpVal = lerpVal + 0.01f;
}
else
{
Neck.position = Vector3.zero;
}
}
}

View File

@@ -5,23 +5,33 @@ using Mirror;
public class Player : NetworkBehaviour
{
public bool isAlive;
public bool isAlive = true;
public Team team;
[SerializeField] private const int defaultHp = 100;
public ulong clientId;
[SyncVar(hook = nameof(SetName))]
[SyncVar(hook = nameof(SetName))]
public string username;
[SerializeField] GameObject usernameTextObj;
private int health;
[SerializeField] [SyncVar]public int health = 100;
private int kills;
private int deaths;
private void Start()
{
if (isServer)
{
health = defaultHp;
}
}
public override void OnStartLocalPlayer()
{
base.OnStartClient();
}
public void SetName(string oldName, string newName)
@@ -56,8 +66,11 @@ public class Player : NetworkBehaviour
}
public void RemoveHealth(int value)
{
if (isAlive)
{
Debug.Log("yeet" + value);
health -= value;
if (health <= 0)
{

View File

@@ -8,157 +8,98 @@ using Mirror;
public class PlayerController : NetworkBehaviour
{
[Header("Mouse Look")]
[SerializeField] private Transform playerCamera = null;
[SerializeField] private Transform playerNeck = null;
[SerializeField] private float mouseSensitivity = 4.0f;
[SerializeField] private float maxCameraAngle = -90f;
[SerializeField] private float neckStartAngle = 0f;
[SerializeField] private float minCameraAngle = 90f;
[SerializeField] private float neckLength = 0.2f;
[SerializeField] [Range(0.0f, 0.5f)] private float mouseSmoothTime = 0.001f;
[SerializeField] private bool lockCursor = true;
[Header("Movement")]
[SerializeField] private float walkSpeed = 6.0f;
[SerializeField][Range(0.0f, 0.5f)] private float moveSmoothTime = 0.05f;
[SerializeField] float gravity = -13.0f;
[SerializeField] private float sprintSpeed = 10.0f;
[SerializeField][Range(0.0f, 0.5f)] private float moveSmoothTime = 0.001f;
[SerializeField] float gravity = -10.0f;
[SerializeField] private float jumpHeight;
private Vector3 inputDirection = Vector3.zero;
private Vector3 moveDirection;
[Header("Ground Check")]
[SerializeField] private Transform groundCheck;
[SerializeField] private float groundDistance = 0.4f;
[SerializeField] private LayerMask groundMask;
<<<<<<< Updated upstream
=======
[Header("Ground Angle")]
[SerializeField] private float groundAngle;
[SerializeField] private float moveGroundAngle;
[Header("bullets per minute")]
[SerializeField] private float bulletsPerMinute = 120f;
[SerializeField] private bool canFire = true;
[SerializeField] private GameObject gun;
>>>>>>> Stashed changes
public bool isGrounded;
private float fullPitch = 0f;
private float cameraPitch = 0f;
private float neckPitch = 0f;
public bool isSprinting;
private float movementSpeed;
private float velocityY = 0.0f;
private CharacterController controller;
[SerializeField]private Animator anim;
private Vector2 currentDir = Vector2.zero;
private Vector2 currentDirVelocity = Vector2.zero;
private Vector2 currentMouseDelta = Vector2.zero;
private Vector2 currentMouseDeltaVelocity = Vector2.zero;
private Vector3 currentDir = Vector3.zero;
private Vector3 currentDirVelocity = Vector3.zero;
private Vector3 velocity = Vector3.zero;
private void Start()
{
anim = gun.GetComponent<Animator>();
if (isLocalPlayer)
{
controller = GetComponent<CharacterController>();
if (lockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
else
{
playerCamera.gameObject.SetActive(false);
}
}
private void Update()
{
if (!isLocalPlayer) return;
UpdateMouseLook();
Grounded();
UpdateMovement();
if (isLocalPlayer)
{
Grounded();
CheckGoundAngle();
UpdateMovement();
}
}
private void Grounded()
{
//Check every frame if the player stands on the ground
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
isGrounded = Physics.CheckSphere(groundCheck.position + new Vector3(0, GetComponent<CharacterController>().radius - 0.1f, 0),GetComponent<CharacterController>().radius + 0.0f, groundMask);
}
private void UpdateMouseLook()
public bool isMoving()
{
Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); //Get the axis of the mouse
currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime);
fullPitch -= currentMouseDelta.y * mouseSensitivity;
fullPitch = Mathf.Clamp(fullPitch,-maxCameraAngle,-minCameraAngle);
if (fullPitch >= neckStartAngle) {
playerNeck.localEulerAngles = Vector3.right * (fullPitch - neckStartAngle);
}
else {
playerCamera.localEulerAngles = Vector3.right * fullPitch;
<<<<<<< Updated upstream
}
playerCamera.position = playerNeck.position;
playerCamera.position += playerNeck.up * neckLength;
transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity); //Rotate the hole player if looked sideways (Rotates the player left and right)
if (velocity.x == 0 && velocity.y == 0 && velocity.z == 0) return true;
else return false;
}
private void CheckGoundAngle()
{
//Check every frame if the player stands on the ground
RaycastHit hit;
if (Physics.Raycast(groundCheck.position + new Vector3(0, 0.4f, 0), Vector3.down, out hit))
{
moveDirection = Quaternion.Euler(0, transform.eulerAngles.y + 90f, 0) * inputDirection;
moveDirection = Vector3.Cross(moveDirection, hit.normal);
if (isMoving())
{
moveGroundAngle = -(Vector3.Angle(moveDirection, transform.up) - 90f);
}
else
{
moveGroundAngle = 0f;
}
groundAngle = Vector3.Angle(hit.normal,transform.up);
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawRay(new Ray(transform.position, moveDirection * 50));
}
private void UpdateMovement()
{
=======
IEnumerator OnGunReset()
{
//gun.GetComponent<Animator>().SetTrigger("shootTrigger");
//gun.GetComponent<Animator>().Play("Shoot");
anim.Play("Shoot");
yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).normalizedTime);
Debug.Log(anim.GetCurrentAnimatorStateInfo(0).speed);
//Debug.Log(anim.GetNextAnimatorStateInfo(0).speed);
yield return new WaitForSeconds(0.5f/3f);
canFire = true;
}
IEnumerator reset()
{
yield return new WaitForSeconds(60f / bulletsPerMinute);
Debug.Log(60f / bulletsPerMinute);
canFire = true;
}
private void UpdateMovement()
{
if (Input.GetButton("Fire1") && canFire)
{
canFire = false;
anim.PlayInFixedTime("Base Layer.Shoot", 0, bulletsPerMinute);
StartCoroutine("reset");
//StartCoroutine("OnGunReset");
//Debug.Log(anim.GetCurrentAnimatorStateInfo(0).speed);
//Debug.Log(anim.GetCurrentAnimatorClipInfo(0).Length);
//Debug.Log("mult " + anim.GetNextAnimatorStateInfo(0).speedMultiplier);
}
// Debug.Log("2 " + canFire);
if (Input.GetAxisRaw("Sprint") > 0 && isGrounded)
{
Debug.Log("Sprint");
@@ -171,9 +112,15 @@ public class PlayerController : NetworkBehaviour
isSprinting = false;
}
>>>>>>> Stashed changes
//Grounded
velocityY += gravity * Time.deltaTime;
if (velocityY < 0)
{
velocityY += gravity * Time.deltaTime;
}
else
{
velocityY += gravity * 1.0f * Time.deltaTime;
}
if (isGrounded && velocityY < 0)
velocityY = 0.0f;
@@ -184,13 +131,21 @@ public class PlayerController : NetworkBehaviour
velocityY += Mathf.Sqrt(jumpHeight * -2f * gravity);
}
Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); //Get Inputs
targetDir.Normalize(); //Damit schr<68>g laufen nicht schneller ist
inputDirection = new Vector3(Input.GetAxisRaw("Horizontal"),0, Input.GetAxisRaw("Vertical")); //Get Inputs
inputDirection.Normalize(); //Damit schr<68>g laufen nicht schneller ist
currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime); //Smooth movement change
if (isGrounded)
{
currentDir = moveDirection;
}
else
{
moveDirection = new Vector3(moveDirection.x, 0, moveDirection.z);
currentDir = moveDirection;
}
velocity = currentDir * movementSpeed + new Vector3(0, velocityY, 0);
Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + (Vector3.up * velocityY);
controller.Move(velocity * Time.deltaTime);

View File

@@ -70,7 +70,6 @@ public class PlayerMouseLook : NetworkBehaviour
}
else
{
playerNeck.localEulerAngles = Vector3.right * 0f;
playerCamera.localEulerAngles = Vector3.right * fullPitch;

View File

@@ -0,0 +1,29 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveData : MonoBehaviour
{
[SerializeField] PlayerData _PlayerData = new PlayerData();
private void Start()
{
gameObject.GetComponent<Button>().onClick.AddListener(SavePlayerToJson);
}
public void SavePlayerToJson()
{
string playerData = JsonUtility.ToJson(_PlayerData);
System.IO.File.WriteAllText(Application.persistentDataPath + "/PlayerData.json", playerData);
Debug.Log(Application.persistentDataPath);
}
}
[System.Serializable]
public class PlayerData
{
public string username;
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 039d8e4cae766214180c87269a6218db
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -11,6 +11,6 @@ public class HostBtnScript : MonoBehaviour
public void HostServer()
{
NetworkClient.ConnectHost();
GameObject.Find("GameManager").GetComponent<JoinLeaveManager>().Host();
}
}

View File

@@ -8,13 +8,18 @@ public class JoinBtnScript : MonoBehaviour
[SerializeField] private TMP_InputField inputIp;
[SerializeField] private TMP_InputField inputUsername;
private JoinLeaveManager joinLeaveManager;
private void Start()
{
gameObject.GetComponent<Button>().onClick.AddListener(JoinServer);
joinLeaveManager = GameObject.Find("GameManager").GetComponent<JoinLeaveManager>();
}
public void JoinServer()
{
NetworkClient.Connect(inputIp.text);
joinLeaveManager.Join(inputIp.text, inputUsername.text);
}
}

View File

@@ -0,0 +1,46 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class Shoot : NetworkBehaviour
{
[SerializeField] GameObject muzzle;
private void Update()
{
if (isLocalPlayer)
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
//CmdFireBullet();
//RpcOnFire();
CmdFireBullet();
}
}
}
[Command]
// This code will be executed on the server.
private void CmdFireBullet()
{
GameObject dedplayer;
RaycastHit hit;
if (Physics.Raycast(muzzle.transform.position, muzzle.transform.forward, out hit))
{
if (hit.transform.gameObject.GetComponent<Player>() != null)
{
dedplayer = hit.transform.gameObject;
dedplayer.GetComponent<Player>().RemoveHealth(20);
}
}
}
[Client]
// This code will be executed on the Client.
void RpcOnFire()
{
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bc5bc2b49bd326e4db460a6a3af59311
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,84 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
public enum weaponKinds
{
Rifle, Pistole, Knife, Granade
}
[SerializeField] weaponKinds weaponKind;
[SerializeField] bool active = false;
[SerializeField] float damage = 0;
[SerializeField] float firerate = 0;
[SerializeField] float recoilStrength = 0;
[SerializeField] int currentAmmunition = 0;
[SerializeField] int magazinSize = 0;
[SerializeField] int totalAmmunition = 0;
[SerializeField] ParticleSystem flash;
[SerializeField] GameObject bulletExit;
private bool allowShoot = true;
public bool Active { get => active; set => active = value; }
public weaponKinds WeaponKind { get => weaponKind; set => weaponKind = value; }
private void Start()
{
currentAmmunition = magazinSize;
}
private void FixedUpdate()
{
if (Input.GetButton("Fire") && allowShoot && currentAmmunition > 0)
{
fire();
StartCoroutine(fireRate());
currentAmmunition--;
}
if (Input.GetButton("Reload"))
{
if (allowShoot && totalAmmunition > 0)
{
allowShoot = false;
int dif = magazinSize - currentAmmunition;
if(totalAmmunition >= dif) {
currentAmmunition += dif;
totalAmmunition -= dif;
}
else{
currentAmmunition += totalAmmunition;
totalAmmunition = 0;
}
allowShoot = true;
}
}
if (Input.GetButton("Aim"))
{
}
}
private void fire()
{
allowShoot = false;
flash.Play();
RaycastHit hit;
if(Physics.Raycast(bulletExit.transform.position,bulletExit.transform.forward, out hit))
{
Debug.DrawLine(bulletExit.transform.position, hit.point);
}
}
IEnumerator fireRate()
{
allowShoot = false;
yield return new WaitForSeconds(firerate);
allowShoot = true;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c98f5c47a8b7dd64f86fd6f42c4d6e5e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,63 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponManager : MonoBehaviour
{
public int currentWeaponIndex = 0;
public List<GameObject> allWeapons = new List<GameObject>();
public GameObject[] activeWeapons;
[SerializeField] GameObject cam;
private void Awake()
{
activeWeapons = new GameObject[4];
}
void Update() {
if(Input.GetAxis("Mouse ScrollWheel") > 0f){ // Scroll up
if (currentWeaponIndex <= 0)
{ currentWeaponIndex = activeWeapons.Length; }
else { currentWeaponIndex--; }
}
if (Input.GetAxis("Mouse ScrollWheel") < 0f){ // Scroll down
if (currentWeaponIndex >= activeWeapons.Length)
{ currentWeaponIndex = 0; }
else { currentWeaponIndex++; }
}
if (Input.GetButton("Interact")) // e
{
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out RaycastHit hit))
{
Debug.DrawLine(cam.transform.position, hit.point);
if (allWeapons.Contains(hit.transform.gameObject) && !searchInArray(activeWeapons, hit.transform.gameObject)) // If Object is a weapon and the weapon is not in the current active weapons
{
switch (hit.transform.GetComponent<Weapon>().WeaponKind.ToString()) // Adding weapon to inventory slot
{
case "Rifle": activeWeapons[0] = hit.transform.gameObject; break;
case "Pistole": activeWeapons[1] = hit.transform.gameObject; break;
case "Knife": activeWeapons[2] = hit.transform.gameObject; break;
case "Granade": activeWeapons[3] = hit.transform.gameObject; break;
default: break;
}
}
}
}
if (Input.GetButton("Drop")) // q Droping weapon
{
activeWeapons[currentWeaponIndex] = null;
}
}
private bool searchInArray(GameObject[] arr, GameObject searchObj)
{
foreach(GameObject obj in arr)
{
if (obj == searchObj) return true;
}
return false;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 98dadd30ba25ec34db3b45d0dca2827b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: