You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am looking to setup a server auth elevator in my game, but am struggling to get it to work.
This was my plan:
Have animated elevator on the server, which syncs to clients. The animation drives the movement, and it's sent to clients.
Parent player to elevator (local gravity)
Video example:
chrome_IKSmjHRbGt.mp4
But it's not working. If you could create an example script that I could expand, would hugely appreciate it. Just need pointing in the right direction.
Here's my code:
usingSystem.Collections.Generic;usingUnityEngine;usingLiteNetLibManager;namespaceMultiplayerARPG{/// <summary>/// This script synchronizes the platform's transform across the network/// and ensures that any players standing on it move and rotate seamlessly/// with the platform without lag or poor interpolation./// Attach this script to any platform GameObject./// </summary>[RequireComponent(typeof(LiteNetLibIdentity))][RequireComponent(typeof(Collider))]publicclassPlatformSync:MonoBehaviour{privateLiteNetLibIdentityidentity;// Store the platform's last position and rotation on the serverprivateVector3lastPosition;privateQuaternionlastRotation;// List of players currently on the platformprivateList<BasePlayerCharacterEntity>playersOnPlatform=newList<BasePlayerCharacterEntity>();privatevoidAwake(){identity=GetComponent<LiteNetLibIdentity>();// Initialize last known position and rotationlastPosition=transform.position;lastRotation=transform.rotation;// Ensure the Collider is set as a TriggerCollidercollider=GetComponent<Collider>();if(!collider.isTrigger){collider.isTrigger=true;Debug.LogWarning($"{gameObject.name}: Collider was not set as Trigger. Automatically set to Trigger.");}}privatevoidFixedUpdate(){// Only the server should handle synchronizationif(!identity.IsServer)return;// Calculate movement and rotation deltasVector3currentPosition=transform.position;QuaternioncurrentRotation=transform.rotation;Vector3deltaPosition=currentPosition-lastPosition;QuaterniondeltaRotation=currentRotation*Quaternion.Inverse(lastRotation);// If the platform has moved or rotatedif(deltaPosition!=Vector3.zero||deltaRotation!=Quaternion.identity){// Apply movement and rotation deltas to all players on the platformforeach(varplayerinplayersOnPlatform){if(player==null)continue;// Apply positional deltaplayer.transform.position+=deltaPosition;// Apply rotational deltaplayer.transform.rotation=deltaRotation*player.transform.rotation;// Optionally, you can adjust the player's velocity or other movement parameters here// to ensure smooth movement. This depends on how your character controller handles movement.}// Update last known position and rotationlastPosition=currentPosition;lastRotation=currentRotation;// Optionally, you can notify clients about the platform's movement here// if not handled automatically by LiteNetLibIdentity}}privatevoidOnTriggerEnter(Colliderother){// Detect when a player steps onto the platformBasePlayerCharacterEntityplayer=other.GetComponent<BasePlayerCharacterEntity>();if(player!=null&&!playersOnPlatform.Contains(player)){playersOnPlatform.Add(player);// Optional: Notify the player that they're on a platform// This can be used for additional effects or state changes}}privatevoidOnTriggerExit(Colliderother){// Detect when a player steps off the platformBasePlayerCharacterEntityplayer=other.GetComponent<BasePlayerCharacterEntity>();if(player!=null&&playersOnPlatform.Contains(player)){playersOnPlatform.Remove(player);// Optional: Notify the player that they've left the platform}}/// <summary>/// Optional: Visualize the trigger area in the Unity Editor for debugging purposes./// </summary>privatevoidOnDrawGizmosSelected(){Gizmos.color=Color.green;Gizmos.DrawWireCube(transform.position,GetComponent<Collider>().bounds.size);}}}
The text was updated successfully, but these errors were encountered:
I am looking to setup a server auth elevator in my game, but am struggling to get it to work.
This was my plan:
Video example:
chrome_IKSmjHRbGt.mp4
But it's not working. If you could create an example script that I could expand, would hugely appreciate it. Just need pointing in the right direction.
Here's my code:
The text was updated successfully, but these errors were encountered: