cp attempt

This commit is contained in:
2022-07-16 11:38:06 +05:30
parent 214e01fa0f
commit d4f6dbd13c
107 changed files with 20030 additions and 6186 deletions

View File

@@ -1,4 +1,4 @@
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using Mirror;
using UnityEngine;
@@ -32,8 +32,9 @@ public class PlayerController : NetworkBehaviour
void Start()
{
jumpT = jumpDuration;
if(!isServer){
rigidbody.simulated=false;
if (!isServer)
{
rigidbody.simulated = false;
}
}
@@ -52,14 +53,16 @@ public class PlayerController : NetworkBehaviour
public float moveInput = 0;
[Command]
void CmdUpdateInput(float _horizontal, bool jump, bool interact){
void CmdUpdateInput(float _horizontal, bool jump, bool interact)
{
HorizontalAxis = _horizontal;
JumpKeyPressed=jump;
InteractionKeyPressed=interact;
JumpKeyPressed = jump;
InteractionKeyPressed = interact;
}
void ListenInput(){
HorizontalAxis=0;
void ListenInput()
{
HorizontalAxis = 0;
if (Input.GetKey(InputManager.data().leftInput)) { HorizontalAxis = -1; } else if (Input.GetKey(InputManager.data().rightInput)) { HorizontalAxis = 1; }
JumpKeyPressed = Input.GetKey(InputManager.data().jumpInput);
InteractionKeyPressed = Input.GetKey(InputManager.data().interactingKey);
@@ -67,20 +70,53 @@ public class PlayerController : NetworkBehaviour
void FixedUpdate()
{
if(!isServer){
if(isLocalPlayer){
//Command Inputs to server
ListenInput();
CmdUpdateInput(HorizontalAxis, JumpKeyPressed, InteractionKeyPressed);
}
return;
}
if(isLocalPlayer){
if (isLocalPlayer)
{ // <-- First Listen to inputs no matter in server or not, but if the lcoal player
ListenInput();
}
Movement();//SImulate everywhere
if (isServer)
{ //Simulate physics on server
RpcUpdatePosition(transform.position, HorizontalAxis, rigidbody.velocity, NetworkTime.time);
}
else
{
if (isLocalPlayer)
{ //I aint the server, but the local player. I move!
CmdUpdateInput(HorizontalAxis, JumpKeyPressed, InteractionKeyPressed);
}
}
}
[ClientRpc]
void RpcUpdatePosition(Vector3 position, float horizontalAxis, Vector3 velocity, double time)
{
double latency = NetworkTime.time - time;
int framesSkipped = Mathf.FloorToInt((float)(latency * 50));
Debug.Log($"Missed {framesSkipped} frames due to network");
if (Vector3.Distance(transform.position, position) > 0.15f)
{
transform.position = position;
rigidbody.velocity = velocity;
}
// if(isLocalPlayer){
// //Client prediction
// if(Vector3.Distance(transform.position, position) > 0.5f){
// transform.position = position;
// rigidbody.velocity= velocity;
// }
// }else{
// //Just update the position
// transform.position = Vector3.Lerp(transform.position, position, 0.1f);
// rigidbody.velocity=velocity;
// }
}
void Movement()
{
isGrounded = getGrounded();
if (_grounded != isGrounded)
@@ -115,7 +151,7 @@ public class PlayerController : NetworkBehaviour
//Update In-Air value on animation
animator.SetBool("inAir", !isGrounded);
animator.SetBool("isOnWater", inWater);
// if (GameManager.isPaused) { HorizontalAxis = 0; }
//Move according to input
@@ -142,7 +178,7 @@ public class PlayerController : NetworkBehaviour
{
//Change moveInput while in-air | IF there is an input
// if (Input.GetKey(InputManager.data().leftInput) || Input.GetKey(InputManager.data().rightInput))
if (HorizontalAxis!=0)
if (HorizontalAxis != 0)
{
moveInput = Mathf.Lerp(moveInput, HorizontalAxis, 0.2f);
}
@@ -190,9 +226,12 @@ public class PlayerController : NetworkBehaviour
// }
// }
rigidbody.transform.Translate(new Vector2(moveSpeed * moveInput, 0));
if(moveInput == 0){
rigidbody.constraints = RigidbodyConstraints2D.FreezePositionX|RigidbodyConstraints2D.FreezeRotation;
}else{
if (moveInput == 0)
{
rigidbody.constraints = RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezeRotation;
}
else
{
rigidbody.constraints = RigidbodyConstraints2D.FreezeRotation;
}
@@ -221,9 +260,7 @@ public class PlayerController : NetworkBehaviour
else { b = false; }
_isSwimming = inWater;
}
bool b;
bool _isSwimming;
public bool waterBoost;
@@ -286,12 +323,6 @@ public class PlayerController : NetworkBehaviour
}
// public static class SettingsInstance{
// public static Settings instance;
// }
public static class AudioSingleton
{
private static AudioSource music;

View File

@@ -0,0 +1,74 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControls : MonoBehaviour
{
[Header("Physics")]
public Vector3 colliderOffset;
public Vector3 colliderSize;
public Vector2 castDirection;
public float castDistance;
public Vector3 gravity;
public Vector3 velocity;
public float maxVelocity = 100;
[Header("Monitor")]
public bool stuckBottom;
public bool stuckRight;
public bool stuckLeft;
public bool stuckUp;
void Start()
{
}
void FixedUpdate()
{
Simulate();
}
void Simulate(){
RaycastHit2D[] overlappingColliders= Physics2D.BoxCastAll(transform.position + colliderOffset, colliderSize,0,castDirection,castDistance);
stuckBottom = stuckLeft = stuckRight = stuckUp = false; // <-- Reset all stuck states.
if(overlappingColliders.Length > 0){
for(int i =0; i < overlappingColliders.Length; i++){
RaycastHit2D hit = overlappingColliders[i];
Debug.Log(hit.collider.name);
Vector2 offset = (Vector2)(transform.position+colliderOffset) - hit.point;
float xBoundary = (colliderSize.x /2f) * 0.9f;
float yBoundary = (colliderSize.y /2f) * 0.1f;
if(offset.x >= xBoundary){
stuckLeft=true;
}else if(offset.x <= -xBoundary) {
stuckRight=true;
}
if(offset.y >= yBoundary){
stuckBottom=true;
}else if(offset.y <= -yBoundary){
stuckUp=true;
}
}
}else{
//No contacts... freefalling
velocity+=gravity;
}
//Apply stucks
velocity = new Vector3(
Mathf.Clamp(velocity.x, (stuckLeft) ? 0: -maxVelocity, (stuckRight) ? 0: maxVelocity),
Mathf.Clamp(velocity.y, (stuckBottom) ? 0: -maxVelocity, (stuckUp)?0: maxVelocity)
);
transform.Translate(velocity); // <-- Apply the calculated velocity
}
private void OnDrawGizmos() {
Gizmos.color = Color.green;
Gizmos.DrawWireCube(transform.position + colliderOffset, colliderSize);
}
}

View File

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