Trail WIP
This commit is contained in:
@@ -6,6 +6,7 @@ using System.Linq;
|
||||
public class SpaceshipController : NetworkBehaviour
|
||||
{
|
||||
public Transform body;
|
||||
public TrailMgr trailMgr;
|
||||
public float movingSpeed = 0.1f;
|
||||
public float turningSmoothFactor = 0.1f;
|
||||
public Joystick joystick;
|
||||
@@ -26,11 +27,10 @@ public class SpaceshipController : NetworkBehaviour
|
||||
{
|
||||
if (joystick == null) { joystick = FindObjectOfType<Joystick>(); }
|
||||
FindObjectOfType<CameraFollower>().SetTarget(transform);
|
||||
GetComponent<NetworkTransform>().enabled = false;
|
||||
}
|
||||
if (isServer)
|
||||
{
|
||||
GetComponent<NetworkTransform>().enabled = true;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,9 +51,7 @@ public class SpaceshipController : NetworkBehaviour
|
||||
body.Translate(new Vector3(0, movingSpeed), body);
|
||||
if (joyInput != Vector2.zero)
|
||||
{
|
||||
//Turn
|
||||
var angle1 = Mathf.Atan2(-joyInput.y, -joyInput.x) * Mathf.Rad2Deg;
|
||||
body.rotation = Quaternion.Lerp(body.rotation, Quaternion.AngleAxis(angle1 + 90, Vector3.forward), turningSmoothFactor * joyInput.magnitude);
|
||||
Turn(joyInput);
|
||||
}
|
||||
}
|
||||
///Diff = rot1 . rot2
|
||||
@@ -65,7 +63,7 @@ public class SpaceshipController : NetworkBehaviour
|
||||
CmdUpdateJoyInput(joyInput);
|
||||
|
||||
//Fix Detours
|
||||
if (Mathf.Abs(Detour.magnitude) > 0.1f)
|
||||
if (Mathf.Abs(Detour.magnitude) > 0.1f || true)
|
||||
{
|
||||
Vector3 newPosition = body.position + Detour;
|
||||
Quaternion newRotation = body.rotation * RotationDetour;
|
||||
@@ -89,7 +87,7 @@ public class SpaceshipController : NetworkBehaviour
|
||||
lastClientUpdateTime = roundedTime;
|
||||
}
|
||||
}
|
||||
if (isServer)
|
||||
else if (isServer)
|
||||
{
|
||||
int lastUpdatedTime = serverStateBuffer.Count > 0 ? serverStateBuffer.Keys.Last() : 0;
|
||||
if (timeInMillis >= lastUpdatedTime + 100)
|
||||
@@ -100,10 +98,43 @@ public class SpaceshipController : NetworkBehaviour
|
||||
serverStateBuffer.Remove(serverStateBuffer.Keys.First());
|
||||
}
|
||||
}
|
||||
RpcUpdatePosition(joyInput, transform.position, transform.rotation, timeInMillis);
|
||||
RpcUpdateTrail(trailMgr.positions);
|
||||
}else{
|
||||
transform.position = Vector3.Lerp(transform.position, targetPosition, 0.1f);
|
||||
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, 0.1f);
|
||||
}
|
||||
}
|
||||
|
||||
Vector3 targetPosition;
|
||||
Quaternion targetRotation;
|
||||
[ClientRpc]
|
||||
void RpcUpdatePosition(Vector2 input, Vector3 position, Quaternion rotation, double sentTime)
|
||||
{
|
||||
if(isLocalPlayer || isServer){return;}
|
||||
double delay = (timeInMillis - sentTime)* 4;
|
||||
int numberOfFrames = (int)((float)(delay) / 20f);
|
||||
Vector3 newPosition = position;
|
||||
Quaternion newRotation = rotation;
|
||||
for (int i = 0; i < numberOfFrames; i++)
|
||||
{
|
||||
newPosition += new Vector3(0, movingSpeed);
|
||||
newRotation = Quaternion.Lerp(newRotation, getTurnAngle(input), turningSmoothFactor * input.magnitude);
|
||||
}
|
||||
|
||||
targetPosition = newPosition;
|
||||
targetRotation = newRotation;
|
||||
}
|
||||
|
||||
void Turn(Vector2 input)
|
||||
{
|
||||
body.rotation = Quaternion.Lerp(body.rotation, getTurnAngle(input), turningSmoothFactor * input.magnitude);
|
||||
}
|
||||
|
||||
Quaternion getTurnAngle(Vector2 input)
|
||||
{
|
||||
var angle1 = Mathf.Atan2(-input.y, -input.x) * Mathf.Rad2Deg;
|
||||
return Quaternion.AngleAxis(angle1 + 90, Vector3.forward);
|
||||
}
|
||||
|
||||
[Command]
|
||||
@@ -154,35 +185,67 @@ public class SpaceshipController : NetworkBehaviour
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
|
||||
Debug.Log($"RB : {positionDiff.magnitude} [{timeDiff}ms]");
|
||||
RpcRubberBand(serverStateBuffer.Values.Last().Position, serverStateBuffer.Values.Last().Rotation, timeInMillis);
|
||||
RpcRubberBand(joyInput, serverStateBuffer.Values.Last().Position, serverStateBuffer.Values.Last().Rotation, trailMgr.positions, timeInMillis);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
double lastRubberBandTime = 0;
|
||||
[ClientRpc]
|
||||
void RpcRubberBand(Vector3 position, Quaternion rotation, double sentTime)
|
||||
void RpcRubberBand(Vector2 input, Vector3 position, Quaternion rotation, Vector3[] trailPositions, double sentTime)
|
||||
{
|
||||
if (isLocalPlayer)
|
||||
if (true)
|
||||
{
|
||||
|
||||
if (sentTime < lastRubberBandTime) { Debug.Log("Old rubber band rpc, ignoree..."); return; }
|
||||
//Lag comprehension
|
||||
double delay = timeInMillis - sentTime + 10;
|
||||
Vector3 newPosition = position + new Vector3(0, movingSpeed * ((float)(delay) / 20f));
|
||||
int numberOfFrames = (int)((float)(delay) / 20f);
|
||||
Vector3 newPosition = position;
|
||||
Quaternion newRotation = rotation;
|
||||
for (int i = 0; i < numberOfFrames; i++)
|
||||
{
|
||||
newPosition += new Vector3(0, movingSpeed);
|
||||
newRotation = Quaternion.Lerp(newRotation, getTurnAngle(input), turningSmoothFactor * input.magnitude);
|
||||
}
|
||||
int distanceSinceSent = (int)Vector3.Distance(newPosition, position);
|
||||
|
||||
// Vector3[] newTrailPositions = new Vector3[trailPositions.Length];
|
||||
// for(int i=0; i < trailPositions.Length; i++){
|
||||
// if(i > trailPositions.Length - distanceSinceSent-1){
|
||||
// //fill with current data
|
||||
// newTrailPositions[i] = trailMgr.positions[i];
|
||||
// }else{
|
||||
// Vector3 newTrailPoint = trailPositions[i-distanceSinceSent];
|
||||
// if(trailMgr.positions.Length > i){
|
||||
// if(Vector3.Distance(newTrailPoint, trailMgr.positions[i]) < 0.4f){
|
||||
// newTrailPoint = trailMgr.positions[i];
|
||||
// }
|
||||
// }
|
||||
// newTrailPositions[i] = newTrailPoint;
|
||||
// }
|
||||
// }
|
||||
// trailMgr.trail.SetPositions(newTrailPositions);
|
||||
|
||||
// Vector3 newPosition = position + new Vector3(0, movingSpeed * );
|
||||
|
||||
// Vector3 newPosition = position;
|
||||
Detour = newPosition - transform.position;
|
||||
|
||||
// RotationDetour = rotation;
|
||||
RotationDetour = Quaternion.Inverse(transform.rotation) * rotation;
|
||||
RotationDetour = Quaternion.Inverse(transform.rotation) * newRotation;
|
||||
lastRubberBandTime = sentTime;
|
||||
|
||||
Debug.Log($"Rubber banded (Detour of {Detour}) you to {transform.position}, @ {sentTime} (delay: {delay}");
|
||||
// Debug.Log($"Rubber banded (Detour of pos:{Detour}, rotation: {RotationDetour}) you to {transform.position}, @ {sentTime} (delay: {delay}");
|
||||
}
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
void RpcUpdateTrail(Vector3[] positions){
|
||||
//trailMgr.trail.SetPositions(positions);
|
||||
}
|
||||
|
||||
void OnGUI()
|
||||
{
|
||||
if (!isLocalPlayer) { return; }
|
||||
@@ -210,6 +273,9 @@ public class SpaceshipController : NetworkBehaviour
|
||||
{
|
||||
body = transform;
|
||||
}
|
||||
if(trailMgr==null){
|
||||
trailMgr = GetComponent<TrailMgr>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
21
Assets/Game/Scripts/Minigame/TrailCollider.cs
Normal file
21
Assets/Game/Scripts/Minigame/TrailCollider.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class TrailCollider : MonoBehaviour
|
||||
{
|
||||
public Color gizmoColor = Color.red;
|
||||
public float radius;
|
||||
void Update(){
|
||||
RaycastHit2D hit = Physics2D.CircleCast(transform.position, radius, Vector2.up);
|
||||
if(hit.collider!=null){
|
||||
if(hit.transform == transform.root){return;} // <-- avoid eating myself
|
||||
transform.root.GetComponent<TrailMgr>().OnColliderHit(hit);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDrawGizmos() {
|
||||
Gizmos.color = gizmoColor;
|
||||
Gizmos.DrawWireSphere(transform.position,radius);
|
||||
}
|
||||
}
|
||||
11
Assets/Game/Scripts/Minigame/TrailCollider.cs.meta
Normal file
11
Assets/Game/Scripts/Minigame/TrailCollider.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: db40c499fec2f314fa2a37a1617d5a6c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
56
Assets/Game/Scripts/Minigame/TrailMgr.cs
Normal file
56
Assets/Game/Scripts/Minigame/TrailMgr.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
|
||||
public class TrailMgr : NetworkBehaviour
|
||||
{
|
||||
public TrailRenderer trail;
|
||||
public LineRenderer line;
|
||||
public Vector3[] positions;
|
||||
public Transform trailPoolParent;
|
||||
public GameObject trailColliderObj;
|
||||
public List<GameObject> trailsPool;
|
||||
|
||||
void Start(){
|
||||
trail.gameObject.SetActive(isServer);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if(!isServer){return;}
|
||||
|
||||
positions = new Vector3[trail.positionCount];
|
||||
int length = trail.GetPositions(positions);
|
||||
if(length > trailsPool.Count){
|
||||
//must create new trails
|
||||
int missingCount = length - trailsPool.Count;
|
||||
for(int i =0; i < missingCount; i++){
|
||||
GameObject newTrail = Instantiate(trailColliderObj, trailPoolParent);
|
||||
trailsPool.Add(newTrail);
|
||||
}
|
||||
}
|
||||
for(int i =0; i < trailsPool.Count; i++){
|
||||
if(i < length){
|
||||
trailsPool[i].SetActive(true);
|
||||
trailsPool[i].transform.position = positions[i];
|
||||
}else{
|
||||
trailsPool[i].SetActive(false);
|
||||
}
|
||||
}
|
||||
RpcUpdateTrail(positions);
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
void RpcUpdateTrail(Vector3[] new_positions){
|
||||
positions = new_positions;
|
||||
line.SetPositions(positions);
|
||||
}
|
||||
|
||||
|
||||
public void OnColliderHit(RaycastHit2D hit){
|
||||
Debug.Log($"{hit.collider.name} got hit by my trail");
|
||||
Destroy(hit.transform.gameObject);
|
||||
}
|
||||
}
|
||||
11
Assets/Game/Scripts/Minigame/TrailMgr.cs.meta
Normal file
11
Assets/Game/Scripts/Minigame/TrailMgr.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f3fd3790e46aca946913301ba3099ba0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user