Setting up a Multiplayer Project from Scratch
Net Spawning
Network System Concepts
Sync the Transform of the sphere – Protocol will be needed. Packet IDs:
Linear Interpololation First
Catmull Constant Velocity Second
480 Chapter 10 Interpolation Essential_Mathematics_for_Games_and_Interactive_Applications
MovingAlongCurveSpecifiedSpeed
using System.Collections.Generic; using UnityEngine; public enum eOrientationMode { NODE = 0, TANGENT } [AddComponentMenu( "Splines/Spline Controller" )] [RequireComponent( typeof(SplineInterpolator) )] public class SplineController : MonoBehaviour { public GameObject SplineRoot; public float Duration = 10; public eOrientationMode OrientationMode = eOrientationMode.NODE; public eWrapMode WrapMode = eWrapMode.ONCE; public bool AutoStart = true; public bool AutoClose = true; public bool HideOnExecute = true; public bool XAxisNodesOrdering = true; public float CurrentPosition = 0; public SplineInterpolator Interpolator { get { return GetComponent( typeof(SplineInterpolator) ) as SplineInterpolator; } } SplineInterpolator mSplineInterp; Transform[] mTransforms; void OnDrawGizmos() { if(CurrentPosition < 0) CurrentPosition = 0; if(CurrentPosition > Duration) CurrentPosition = Duration; Transform[] trans = GetTransforms( ); if(trans.Length < 2) return; SplineInterpolator interp = Interpolator; SetupSplineInterpolator( interp, trans ); interp.StartInterpolation( null, false, WrapMode ); Vector3 prevPos = trans[0].position; for(int c = 1; c <= 100; c++) { float currTime = c * Duration / 100; Vector3 currPos = interp.GetHermiteAtTime( currTime ); float mag = ( currPos - prevPos ).magnitude * 2; Gizmos.color = new Color( mag, 0, 0, 1 ); Gizmos.DrawLine( prevPos, currPos ); prevPos = currPos; } Gizmos.DrawSphere( interp.GetHermiteAtTime( CurrentPosition ), 1 ); foreach(Transform t in trans) { Gizmos.color = Color.yellow; Gizmos.DrawWireSphere( t.position, 0.5f ); // Gizmos.DrawIcon(t.position, "SplineNode.png" ); } } public Vector3 GetPointOnSpline( float point ) { return Interpolator.GetHermiteAtTime( Duration * point ); } public Vector3 GetHermiteAtTime( float point ) { return Interpolator.GetHermiteAtTime( point ); } void Start() { mSplineInterp = GetComponent( typeof(SplineInterpolator) ) as SplineInterpolator; mTransforms = GetTransforms( ); if(HideOnExecute) DisableTransforms( ); if(AutoStart) FollowSpline( ); } void SetupSplineInterpolator( SplineInterpolator interp, Transform[] trans ) { interp.Reset( ); float step = ( AutoClose ) ? Duration / trans.Length : Duration / ( trans.Length - 1 ); int c; for(c = 0; c < trans.Length; c++) { if(OrientationMode == eOrientationMode.NODE) { interp.AddPoint( trans.position, trans.rotation, step * c, new Vector2( 0, 1 ) ); } else if(OrientationMode == eOrientationMode.TANGENT) { Quaternion rot; if(c != trans.Length - 1) rot = Quaternion.LookRotation( trans.position - trans.position, trans.up ); else if(AutoClose) { rot = Quaternion.LookRotation( trans[0].position - trans.position, trans.up ); } else rot = trans.rotation; interp.AddPoint( trans.position, rot, step * c, new Vector2( 0, 1 ) ); } } if(AutoClose) interp.SetAutoCloseMode( step * c ); } /// <summary> /// Returns children transforms, sorted by name. /// </summary> Transform[] GetTransforms() { if(SplineRoot != null) { List<Component> components = new List<Component>( SplineRoot.GetComponentsInChildren( typeof(Transform) ) ); List<Transform> transforms = components.ConvertAll( c => (Transform)c ); transforms.Remove( SplineRoot.transform ); if(XAxisNodesOrdering) transforms.Sort( delegate( Transform a, Transform b ) { return (int)( b.position.x - a.position.x ); } ); else transforms.Sort( delegate( Transform a, Transform b ) { return a.name.CompareTo( b.name ); } ); return transforms.ToArray( ); } return null; } /// <summary> /// Disables the spline objects, we don't need them outside design-time. /// </summary> void DisableTransforms() { if(SplineRoot != null) { SplineRoot.SetActiveRecursively( false ); } } /// <summary> /// Starts the interpolation /// </summary> void FollowSpline() { if(mTransforms.Length > 0) { SetupSplineInterpolator( mSplineInterp, mTransforms ); mSplineInterp.StartInterpolation( null, true, WrapMode ); } } }