├── .gitignore ├── Assets ├── Model.meta ├── Model │ ├── Artwork.meta │ └── Artwork │ │ ├── Lerpz.fbx │ │ ├── Lerpz.fbx.meta │ │ ├── Materials.meta │ │ ├── Materials │ │ ├── lerpz-blinn1.mat │ │ ├── lerpz-blinn1.mat.meta │ │ ├── lerpzuv.mat │ │ └── lerpzuv.mat.meta │ │ ├── lerpzBump.tif │ │ ├── lerpzBump.tif.meta │ │ ├── lerpzUV.tif │ │ └── lerpzUV.tif.meta ├── Prefab.meta ├── Prefab │ ├── ConnectionGUI.prefab │ ├── ConnectionGUI.prefab.meta │ ├── Cube.prefab │ ├── Cube.prefab.meta │ ├── Third Person Player.prefab │ └── Third Person Player.prefab.meta ├── Scene.meta ├── Scene │ ├── Third Person.unity │ ├── Third Person.unity.meta │ ├── loader.unity │ └── loader.unity.meta ├── Scripts.meta └── Scripts │ ├── CharacterControls-(No Networking code).meta │ ├── CharacterControls-(No Networking code) │ ├── SpringFollowCamera.cs │ ├── SpringFollowCamera.cs.meta │ ├── ThirdPersonController.cs │ ├── ThirdPersonController.cs.meta │ ├── ThirdPersonPushBodies.cs │ ├── ThirdPersonPushBodies.cs.meta │ ├── ThirdPersonSimpleAnimation.cs │ └── ThirdPersonSimpleAnimation.cs.meta │ ├── Networking Scripts.meta │ ├── Networking Scripts │ ├── ConnectGui.cs │ ├── ConnectGui.cs.meta │ ├── ConnectGuiMasterServer.cs │ ├── ConnectGuiMasterServer.cs.meta │ ├── CreateConnectionGUI.cs │ ├── CreateConnectionGUI.cs.meta │ ├── NetworkInterpolatedTransform.cs │ ├── NetworkInterpolatedTransform.cs.meta │ ├── NetworkLevelLoad.cs │ ├── NetworkLevelLoad.cs.meta │ ├── NetworkRigidbody.cs │ ├── NetworkRigidbody.cs.meta │ ├── NetworkSyncAnimation.cs │ ├── NetworkSyncAnimation.cs.meta │ ├── SpawnPrefab.cs │ ├── SpawnPrefab.cs.meta │ ├── ThirdPersonNetworkInit.cs │ └── ThirdPersonNetworkInit.cs.meta │ ├── ThirdPersonAction.cs │ └── ThirdPersonAction.cs.meta ├── Library ├── AudioManager.asset ├── DynamicsManager.asset ├── EditorBuildSettings.asset ├── InputManager.asset ├── NetworkManager.asset ├── ProjectSettings.asset ├── QualitySettings.asset ├── TagManager.asset └── TimeManager.asset └── README.textile /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | Library/* 3 | Temp 4 | !EditorBuildSettings.asset 5 | !InputManager.asset 6 | !ProjectSettings.asset 7 | !QualitySettings.asset 8 | !TagManager.asset 9 | !TimeManager.asset 10 | !AudioManager.asset 11 | !DynamicsManager.asset 12 | !NetworkManager.asset -------------------------------------------------------------------------------- /Assets/Model.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 0f3619f6363454878a8dcb048bb8c417 3 | -------------------------------------------------------------------------------- /Assets/Model/Artwork.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 2c03dee7bd11ae3d110035ea44295342 3 | -------------------------------------------------------------------------------- /Assets/Model/Artwork/Lerpz.fbx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Assets/Model/Artwork/Lerpz.fbx -------------------------------------------------------------------------------- /Assets/Model/Artwork/Lerpz.fbx.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 4a461e42cd11fe64a0002e294ada8c59 3 | ModelImporter: 4 | importerVersion: 7 5 | meshes: 6 | scaleFactor: 1 7 | useFileUnits: 1 8 | meshCompression: 0 9 | generateColliders: 0 10 | swapUVs: 0 11 | tangentSpace: 12 | normalImportMode: 0 13 | tangentImportMode: 1 14 | smoothingAngle: 60 15 | splitTangents: 0 16 | materials: 17 | generation: 1 18 | animations: 19 | generation: 3 20 | bakeAnimations: 0 21 | animationCompression: 1 22 | animationWrapMode: 0 23 | splitAnimations: 1 24 | clips: 25 | walk: 26 | firstFrame: 1 27 | lastFrame: 33 28 | loop: 0 29 | wrapMode: 0 30 | run: 31 | firstFrame: 41 32 | lastFrame: 57 33 | loop: 0 34 | wrapMode: 0 35 | kick: 36 | firstFrame: 81 37 | lastFrame: 97 38 | loop: 0 39 | wrapMode: 0 40 | punch: 41 | firstFrame: 101 42 | lastFrame: 117 43 | loop: 0 44 | wrapMode: 0 45 | jump: 46 | firstFrame: 121 47 | lastFrame: 129 48 | loop: 0 49 | wrapMode: 0 50 | jumpfall: 51 | firstFrame: 149 52 | lastFrame: 157 53 | loop: 0 54 | wrapMode: 0 55 | idle: 56 | firstFrame: 201 57 | lastFrame: 401 58 | loop: 0 59 | wrapMode: 0 60 | gotbit: 61 | firstFrame: 411 62 | lastFrame: 419 63 | loop: 0 64 | wrapMode: 0 65 | gothit: 66 | firstFrame: 421 67 | lastFrame: 429 68 | loop: 0 69 | wrapMode: 0 70 | walljump: 71 | firstFrame: 431 72 | lastFrame: 448 73 | loop: 0 74 | wrapMode: 0 75 | deathfall: 76 | firstFrame: 451 77 | lastFrame: 459 78 | loop: 0 79 | wrapMode: 0 80 | jetpackjump: 81 | firstFrame: 149 82 | lastFrame: 150 83 | loop: 0 84 | wrapMode: 0 85 | ledgefall: 86 | firstFrame: 149 87 | lastFrame: 150 88 | loop: 0 89 | wrapMode: 0 90 | buttstomp: 91 | firstFrame: 137 92 | lastFrame: 145 93 | loop: 0 94 | wrapMode: 0 95 | jumpland: 96 | firstFrame: 161 97 | lastFrame: 177 98 | loop: 0 99 | wrapMode: 0 100 | nodeInfo: 101 | Lerpz: 102 | - 100000 103 | - 400000 104 | rootJoint: 105 | - 100002 106 | - 400002 107 | - 13700000 108 | torso: 109 | - 100004 110 | - 400004 111 | shoulders: 112 | - 100006 113 | - 400006 114 | shoulderRight: 115 | - 100008 116 | - 400008 117 | elbowRight: 118 | - 100010 119 | - 400010 120 | wristRight: 121 | - 100012 122 | - 400012 123 | handRight: 124 | - 100014 125 | - 400014 126 | thumbRight1: 127 | - 100016 128 | - 400016 129 | thumbRight2: 130 | - 100018 131 | - 400018 132 | thumbRight3: 133 | - 100020 134 | - 400020 135 | pinkyFingerRight1: 136 | - 100022 137 | - 400022 138 | pinkyFingerRight2: 139 | - 100024 140 | - 400024 141 | pinkyFingerRight3: 142 | - 100026 143 | - 400026 144 | pinkyFingerRight4: 145 | - 100028 146 | - 400028 147 | middleFingerRight1: 148 | - 100030 149 | - 400030 150 | middleFingerRight2: 151 | - 100032 152 | - 400032 153 | middleFingerRight3: 154 | - 100034 155 | - 400034 156 | middleFingerRight4: 157 | - 100036 158 | - 400036 159 | indexFingerRight1: 160 | - 100038 161 | - 400038 162 | indexFingerRight2: 163 | - 100040 164 | - 400040 165 | indexFingerRight3: 166 | - 100042 167 | - 400042 168 | indexFingerRight4: 169 | - 100044 170 | - 400044 171 | shoulderLeft: 172 | - 100046 173 | - 400046 174 | elbowLeft: 175 | - 100048 176 | - 400048 177 | wristLeft: 178 | - 100050 179 | - 400050 180 | handLeft: 181 | - 100052 182 | - 400052 183 | thumbLeft1: 184 | - 100054 185 | - 400054 186 | thumbLeft2: 187 | - 100056 188 | - 400056 189 | thumbLeft3: 190 | - 100058 191 | - 400058 192 | pinkyFingerLeft1: 193 | - 100060 194 | - 400060 195 | pinkyFingerLeft2: 196 | - 100062 197 | - 400062 198 | pinkyFingerLeft3: 199 | - 100064 200 | - 400064 201 | pinkyFingerLeft4: 202 | - 100066 203 | - 400066 204 | middleFingerLeft1: 205 | - 100068 206 | - 400068 207 | middleFingerLeft2: 208 | - 100070 209 | - 400070 210 | middleFingerLeft3: 211 | - 100072 212 | - 400072 213 | middleFingerLeft4: 214 | - 100074 215 | - 400074 216 | indexFingerLeft1: 217 | - 100076 218 | - 400076 219 | indexFingerLeft2: 220 | - 100078 221 | - 400078 222 | indexFingerLeft3: 223 | - 100080 224 | - 400080 225 | indexFingerLeft4: 226 | - 100082 227 | - 400082 228 | head: 229 | - 100084 230 | - 400084 231 | aNTeNNa_trEE: 232 | - 100086 233 | - 400086 234 | antennaRight1: 235 | - 100088 236 | - 400088 237 | antennaRight2: 238 | - 100090 239 | - 400090 240 | antennaRight3: 241 | - 100092 242 | - 400092 243 | antennaLeft1: 244 | - 100094 245 | - 400094 246 | antennaLeft2: 247 | - 100096 248 | - 400096 249 | antennaLeft3: 250 | - 100098 251 | - 400098 252 | mouthRight1: 253 | - 100100 254 | - 400100 255 | mouthRight2: 256 | - 100102 257 | - 400102 258 | mouthTop1: 259 | - 100104 260 | - 400104 261 | mouthTop2: 262 | - 100106 263 | - 400106 264 | mouthLeft1: 265 | - 100108 266 | - 400108 267 | mouthLeft2: 268 | - 100110 269 | - 400110 270 | mouthBottom1: 271 | - 100112 272 | - 400112 273 | mouthBottom2: 274 | - 100114 275 | - 400114 276 | hipRight: 277 | - 100116 278 | - 400116 279 | kneeRight: 280 | - 100118 281 | - 400118 282 | ankleRight: 283 | - 100120 284 | - 400120 285 | footRight: 286 | - 100122 287 | - 400122 288 | toeRight: 289 | - 100124 290 | - 400124 291 | toeRightTip: 292 | - 100126 293 | - 400126 294 | hipLeft: 295 | - 100128 296 | - 400128 297 | kneeLeft: 298 | - 100130 299 | - 400130 300 | ankleLeft: 301 | - 100132 302 | - 400132 303 | footLeft: 304 | - 100134 305 | - 400134 306 | toeLeft: 307 | - 100136 308 | - 400136 309 | toeLeftTip: 310 | - 100138 311 | - 400138 312 | //RootNode: 313 | - 100140 314 | - 400140 315 | - 11100000 316 | AssetImporter: 317 | importerVersion: 1 318 | fileIDToRecycleName: 319 | 4300000: Lerpz 320 | 7400000: Take 001 //// Lerpz 321 | 7400002: walk //// Lerpz 322 | 7400004: run //// Lerpz 323 | 7400006: kick //// Lerpz 324 | 7400008: punch //// Lerpz 325 | 7400010: jump //// Lerpz 326 | 7400012: jumpfall //// Lerpz 327 | 7400014: idle //// Lerpz 328 | 7400016: gotbit //// Lerpz 329 | 7400018: gothit //// Lerpz 330 | 7400020: walljump //// Lerpz 331 | 7400022: deathfall //// Lerpz 332 | 7400024: jumpcapefly //// Lerpz 333 | 7400026: ledgefall //// Lerpz 334 | 7400028: buttstomp //// Lerpz 335 | 7400030: jumpland //// Lerpz 336 | 7400032: jetpackjump //// Lerpz 337 | -------------------------------------------------------------------------------- /Assets/Model/Artwork/Materials.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 0fe0dee7bd11ae3d110035ea44295342 3 | -------------------------------------------------------------------------------- /Assets/Model/Artwork/Materials/lerpz-blinn1.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Assets/Model/Artwork/Materials/lerpz-blinn1.mat -------------------------------------------------------------------------------- /Assets/Model/Artwork/Materials/lerpz-blinn1.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 9f77451bbd1149e81100b26844295342 3 | -------------------------------------------------------------------------------- /Assets/Model/Artwork/Materials/lerpzuv.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Assets/Model/Artwork/Materials/lerpzuv.mat -------------------------------------------------------------------------------- /Assets/Model/Artwork/Materials/lerpzuv.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 852cbd72cd11fe64a0002e294ada8c59 3 | -------------------------------------------------------------------------------- /Assets/Model/Artwork/lerpzBump.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Assets/Model/Artwork/lerpzBump.tif -------------------------------------------------------------------------------- /Assets/Model/Artwork/lerpzBump.tif.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 6c5199bacd1113a2a0008cea4ada8c59 3 | TextureImporter: 4 | importerVersion: 1 5 | maxTextureSize: 256 6 | textureFormat: 10 7 | grayscaleToAlpha: 0 8 | npotScale: 0 9 | generateCubemap: 0 10 | isReadable: 0 11 | mipmaps: 12 | generation: 1 13 | correctGamma: 0 14 | border: 0 15 | filter: 0 16 | fadeout: 0 17 | fadeoutStart: 2 18 | fadeoutEnd: 3 19 | bumpmap: 20 | generation: 1 21 | bumpyness: 0.02419355 22 | filter: 0 23 | textureSettings: 24 | filterMode: 1 25 | anisoLevel: 3 26 | mipmapBias: 0 27 | wrapMode: 1 28 | -------------------------------------------------------------------------------- /Assets/Model/Artwork/lerpzUV.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Assets/Model/Artwork/lerpzUV.tif -------------------------------------------------------------------------------- /Assets/Model/Artwork/lerpzUV.tif.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 1702c13dcd1113a2a0008cea4ada8c59 3 | TextureImporter: 4 | importerVersion: 1 5 | maxTextureSize: 512 6 | textureFormat: 12 7 | grayscaleToAlpha: 0 8 | npotScale: 0 9 | generateCubemap: 0 10 | isReadable: 0 11 | mipmaps: 12 | generation: 1 13 | correctGamma: 0 14 | border: 0 15 | filter: 0 16 | fadeout: 0 17 | fadeoutStart: 2 18 | fadeoutEnd: 3 19 | bumpmap: 20 | generation: 0 21 | bumpyness: 0.25 22 | filter: 0 23 | textureSettings: 24 | filterMode: 1 25 | anisoLevel: 3 26 | mipmapBias: 0 27 | wrapMode: 1 28 | -------------------------------------------------------------------------------- /Assets/Prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 2618093179be24aea99ed8a10e2bca8d 3 | -------------------------------------------------------------------------------- /Assets/Prefab/ConnectionGUI.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Assets/Prefab/ConnectionGUI.prefab -------------------------------------------------------------------------------- /Assets/Prefab/ConnectionGUI.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: f817d943cf2b649179f9237091516588 3 | -------------------------------------------------------------------------------- /Assets/Prefab/Cube.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Assets/Prefab/Cube.prefab -------------------------------------------------------------------------------- /Assets/Prefab/Cube.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: f4b7808d5c4dd44089508c197426bf94 3 | -------------------------------------------------------------------------------- /Assets/Prefab/Third Person Player.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Assets/Prefab/Third Person Player.prefab -------------------------------------------------------------------------------- /Assets/Prefab/Third Person Player.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 48fae782bd11b53d1100116b44295342 3 | -------------------------------------------------------------------------------- /Assets/Scene.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 824a862cabf4341a591b5a6bb6111105 3 | -------------------------------------------------------------------------------- /Assets/Scene/Third Person.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Assets/Scene/Third Person.unity -------------------------------------------------------------------------------- /Assets/Scene/Third Person.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 4038d149bd1153be1100e69a44295342 3 | -------------------------------------------------------------------------------- /Assets/Scene/loader.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Assets/Scene/loader.unity -------------------------------------------------------------------------------- /Assets/Scene/loader.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: a484cc565e5bf4587a031761d010b3e5 3 | -------------------------------------------------------------------------------- /Assets/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 6507876cedaa8462195b8b415d0e8bdd 3 | -------------------------------------------------------------------------------- /Assets/Scripts/CharacterControls-(No Networking code).meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 0d5c6452bd11753d11000feb44295342 3 | -------------------------------------------------------------------------------- /Assets/Scripts/CharacterControls-(No Networking code)/SpringFollowCamera.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | [AddComponentMenu("Third Person Camera/Spring Follow Camera")] 5 | public class SpringFollowCamera : MonoBehaviour 6 | { 7 | // This camera is similar to the one used in Jak & Dexter 8 | public Transform target; 9 | public float distance = 4.0f; 10 | public float height = 1.0f; 11 | public float smoothLag = 0.2f; 12 | public float maxSpeed = 10.0f; 13 | public float snapLag = 0.3f; 14 | public float clampHeadPositionScreenSpace = 0.75f; 15 | public LayerMask lineOfSightMask = 0; 16 | 17 | private bool isSnapping = false; 18 | private Vector3 headOffset = Vector3.zero; 19 | private Vector3 centerOffset = Vector3.zero; 20 | private ThirdPersonController controller; 21 | private Vector3 velocity = Vector3.zero; 22 | private float targetHeight = 100000.0f; 23 | 24 | void Awake () 25 | { 26 | DidChangeTarget (); 27 | } 28 | 29 | void DidChangeTarget () 30 | { 31 | if (target) { 32 | Collider characterController = target.collider; 33 | if (characterController) { 34 | centerOffset = characterController.bounds.center - target.position; 35 | headOffset = centerOffset; 36 | headOffset.y = characterController.bounds.max.y - target.position.y; 37 | } 38 | 39 | if (target) { 40 | controller = target.GetComponent (); 41 | } 42 | 43 | if (!controller) 44 | Debug.Log ("Please assign a target to the camera that has a super mario controller."); 45 | } 46 | } 47 | 48 | void Apply (Transform dummyTarget, Vector3 dummyCenter) 49 | { 50 | Vector3 targetCenter = target.position + centerOffset; 51 | Vector3 targetHead = target.position + headOffset; 52 | 53 | // When jumping don't move camera upwards but only down! 54 | if (controller.IsJumping ()) { 55 | // We'd be moving the camera upwards, do that only if it's really high 56 | float newTargetHeight = targetCenter.y + height; 57 | if (newTargetHeight < targetHeight || newTargetHeight - targetHeight > 5) 58 | targetHeight = targetCenter.y + height; 59 | // When walking always update the target height 60 | } else { 61 | targetHeight = targetCenter.y + height; 62 | } 63 | 64 | // We start snapping when user pressed Fire2! 65 | if (Input.GetButton ("Fire2") && !isSnapping) { 66 | velocity = Vector3.zero; 67 | isSnapping = true; 68 | } 69 | 70 | if (isSnapping) { 71 | ApplySnapping (targetCenter); 72 | } else { 73 | ApplyPositionDamping (new Vector3 (targetCenter.x, targetHeight, targetCenter.z)); 74 | } 75 | 76 | SetUpRotation (targetCenter, targetHead); 77 | } 78 | 79 | void LateUpdate () 80 | { 81 | if (target) 82 | Apply (null, Vector3.zero); 83 | } 84 | 85 | void ApplySnapping (Vector3 targetCenter) 86 | { 87 | Vector3 position = transform.position; 88 | Vector3 offset = position - targetCenter; 89 | offset.y = 0; 90 | float currentDistance = offset.magnitude; 91 | 92 | float targetAngle = target.eulerAngles.y; 93 | float currentAngle = transform.eulerAngles.y; 94 | 95 | currentAngle = Mathf.SmoothDampAngle (currentAngle, targetAngle,ref velocity.x, snapLag); 96 | currentDistance = Mathf.SmoothDamp (currentDistance, distance,ref velocity.z, snapLag); 97 | 98 | Vector3 newPosition = targetCenter; 99 | newPosition += Quaternion.Euler (0, currentAngle, 0) * Vector3.back * currentDistance; 100 | 101 | newPosition.y = Mathf.SmoothDamp (position.y, targetCenter.y + height,ref velocity.y, smoothLag, maxSpeed); 102 | 103 | newPosition = AdjustLineOfSight (newPosition, targetCenter); 104 | 105 | transform.position = newPosition; 106 | 107 | // We are close to the target, so we can stop snapping now! 108 | if (AngleDistance (currentAngle, targetAngle) < 3.0f) { 109 | isSnapping = false; 110 | velocity = Vector3.zero; 111 | } 112 | } 113 | 114 | Vector3 AdjustLineOfSight (Vector3 newPosition, Vector3 target) 115 | { 116 | RaycastHit hit; 117 | if (Physics.Linecast (target, newPosition,out hit, lineOfSightMask.value)) { 118 | velocity = Vector3.zero; 119 | return hit.point; 120 | } 121 | return newPosition; 122 | } 123 | 124 | void ApplyPositionDamping (Vector3 targetCenter) 125 | { 126 | // We try to maintain a constant distance on the x-z plane with a spring. 127 | // Y position is handled with a seperate spring 128 | Vector3 position = transform.position; 129 | Vector3 offset = position - targetCenter; 130 | offset.y = 0; 131 | Vector3 newTargetPos = offset.normalized * distance + targetCenter; 132 | 133 | Vector3 newPosition; 134 | newPosition.x = Mathf.SmoothDamp (position.x, newTargetPos.x,ref velocity.x, smoothLag, maxSpeed); 135 | newPosition.z = Mathf.SmoothDamp (position.z, newTargetPos.z,ref velocity.z, smoothLag, maxSpeed); 136 | newPosition.y = Mathf.SmoothDamp (position.y, targetCenter.y,ref velocity.y, smoothLag, maxSpeed); 137 | 138 | newPosition = AdjustLineOfSight (newPosition, targetCenter); 139 | 140 | transform.position = newPosition; 141 | } 142 | 143 | void SetUpRotation (Vector3 centerPos, Vector3 headPos) 144 | { 145 | // Now it's getting hairy. The devil is in the details here, the big issue is jumping of course. 146 | // * When jumping up and down don't center the guy in screen space. This is important to give a feel for how high you jump. 147 | // When keeping him centered, it is hard to see the jump. 148 | // * At the same time we dont want him to ever go out of screen and we want all rotations to be totally smooth 149 | // 150 | // So here is what we will do: 151 | // 152 | // 1. We first find the rotation around the y axis. Thus he is always centered on the y-axis 153 | // 2. When grounded we make him be cented 154 | // 3. When jumping we keep the camera rotation but rotate the camera to get him back into view if his head is above some threshold 155 | // 4. When landing we must smoothly interpolate towards centering him on screen 156 | Vector3 cameraPos = transform.position; 157 | Vector3 offsetToCenter = centerPos - cameraPos; 158 | 159 | // Generate base rotation only around y-axis 160 | Quaternion yRotation = Quaternion.LookRotation (new Vector3 (offsetToCenter.x, 0, offsetToCenter.z)); 161 | 162 | Vector3 relativeOffset = Vector3.forward * distance + Vector3.down * height; 163 | transform.rotation = yRotation * Quaternion.LookRotation (relativeOffset); 164 | 165 | // Calculate the projected center position and top position in world space 166 | Ray centerRay = camera.ViewportPointToRay (new Vector3 (0.5f, 0.5f, 1)); 167 | Ray topRay = camera.ViewportPointToRay (new Vector3 (0.5f, clampHeadPositionScreenSpace, 1)); 168 | 169 | Vector3 centerRayPos = centerRay.GetPoint (distance); 170 | Vector3 topRayPos = topRay.GetPoint (distance); 171 | 172 | float centerToTopAngle = Vector3.Angle (centerRay.direction, topRay.direction); 173 | 174 | float heightToAngle = centerToTopAngle / (centerRayPos.y - topRayPos.y); 175 | 176 | var extraLookAngle = heightToAngle * (centerRayPos.y - centerPos.y); 177 | if (extraLookAngle < centerToTopAngle) { 178 | extraLookAngle = 0; 179 | } else { 180 | extraLookAngle = extraLookAngle - centerToTopAngle; 181 | transform.rotation *= Quaternion.Euler (-extraLookAngle, 0, 0); 182 | } 183 | } 184 | 185 | float AngleDistance (float a, float b) 186 | { 187 | a = Mathf.Repeat (a, 360); 188 | b = Mathf.Repeat (b, 360); 189 | 190 | return Mathf.Abs (b - a); 191 | } 192 | 193 | Vector3 GetCenterOffset () 194 | { 195 | return centerOffset; 196 | } 197 | 198 | void SetTarget (Transform t) 199 | { 200 | target = t; 201 | DidChangeTarget (); 202 | } 203 | 204 | 205 | } 206 | -------------------------------------------------------------------------------- /Assets/Scripts/CharacterControls-(No Networking code)/SpringFollowCamera.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: c5ce1d5e0f40a4ea99b38d428e8a5c4b 3 | -------------------------------------------------------------------------------- /Assets/Scripts/CharacterControls-(No Networking code)/ThirdPersonController.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | // Require a character controller to be attached to the same game object 5 | [RequireComponent(typeof(CharacterController))] 6 | [AddComponentMenu("Third Person Player/Third Person Controller")] 7 | public class ThirdPersonController : MonoBehaviour 8 | { 9 | 10 | // The speed when walking 11 | public float walkSpeed = 3.0f; 12 | // after trotAfterSeconds of walking we trot with trotSpeed 13 | public float trotSpeed = 4.0f; 14 | // when pressing "Fire3" button (cmd) we start running 15 | public float runSpeed = 6.0f; 16 | 17 | public float inAirControlAcceleration = 3.0f; 18 | 19 | // How high do we jump when pressing jump and letting go immediately 20 | public float jumpHeight = 0.5f; 21 | // We add extraJumpHeight meters on top when holding the button down longer while jumping 22 | public float extraJumpHeight = 2.5f; 23 | 24 | // The gravity for the character 25 | public float gravity = 20.0f; 26 | // The gravity in cape fly mode 27 | public float capeFlyGravity = 2.0f; 28 | public float speedSmoothing = 10.0f; 29 | public float rotateSpeed = 500.0f; 30 | public float trotAfterSeconds = 3.0f; 31 | 32 | public bool canJump = true; 33 | public bool canCapeFly = true; 34 | public bool canWallJump = false; 35 | 36 | private float jumpRepeatTime = 0.05f; 37 | private float wallJumpTimeout = 0.15f; 38 | private float jumpTimeout = 0.15f; 39 | private float groundedTimeout = 0.25f; 40 | 41 | // The camera doesnt start following the target immediately but waits for a split second to avoid too much waving around. 42 | private float lockCameraTimer = 0.0f; 43 | 44 | // The current move direction in x-z 45 | private Vector3 moveDirection = Vector3.zero; 46 | // The current vertical speed 47 | private float verticalSpeed = 0.0f; 48 | // The current x-z move speed 49 | private float moveSpeed = 0.0f; 50 | 51 | // The last collision flags returned from controller.Move 52 | private CollisionFlags collisionFlags; 53 | 54 | // Are we jumping? (Initiated with jump button and not grounded yet) 55 | private bool jumping = false; 56 | private bool jumpingReachedApex = false; 57 | 58 | // Are we moving backwards (This locks the camera to not do a 180 degree spin) 59 | private bool movingBack = false; 60 | // Is the user pressing any keys? 61 | public bool isMoving = false; 62 | // When did the user start walking (Used for going into trot after a while) 63 | private float walkTimeStart = 0.0f; 64 | // Last time the jump button was clicked down 65 | private float lastJumpButtonTime = -10.0f; 66 | // Last time we performed a jump 67 | private float lastJumpTime = -1.0f; 68 | // Average normal of the last touched geometry 69 | private Vector3 wallJumpContactNormal = Vector3.zero; 70 | // private float wallJumpContactNormalHeight = 0f; 71 | 72 | // the height we jumped from (Used to determine for how long to apply extra jump power after jumping.) 73 | private float lastJumpStartHeight = 0.0f; 74 | // When did we touch the wall the first time during this jump (Used for wall jumping) 75 | private float touchWallJumpTime = -1.0f; 76 | 77 | private Vector3 inAirVelocity = Vector3.zero; 78 | 79 | private float lastGroundedTime = 0.0f; 80 | 81 | // private float lean = 0.0f; 82 | 83 | // The vertical/horizontal input axes and jump button from user input, synchronized over network 84 | public float verticalInput; 85 | public float horizontalInput; 86 | public bool jumpButton; 87 | 88 | public bool getUserInput = true; 89 | 90 | private ThirdPersonSimpleAnimation thirdPersonSimpleAnimation; 91 | void Awake () 92 | { 93 | //moveDirection = transform.TransformDirection(Vector3.forward); 94 | thirdPersonSimpleAnimation = GetComponent (); 95 | } 96 | 97 | void UpdateSmoothedMovementDirection () 98 | { 99 | Transform cameraTransform = Camera.main.transform; 100 | bool grounded = IsGrounded (); 101 | 102 | // Forward vector relative to the camera along the x-z plane 103 | Vector3 forward = cameraTransform.TransformDirection (Vector3.forward); 104 | forward.y = 0; 105 | forward = forward.normalized; 106 | 107 | // Right vector relative to the camera 108 | // Always orthogonal to the forward vector 109 | Vector3 right = new Vector3 (forward.z, 0, -forward.x); 110 | 111 | if (getUserInput) { 112 | verticalInput = Input.GetAxisRaw ("Vertical"); 113 | horizontalInput = Input.GetAxisRaw ("Horizontal"); 114 | } 115 | 116 | // Are we moving backwards or looking backwards 117 | if (verticalInput < -0.2f) 118 | movingBack = true; 119 | else 120 | movingBack = false; 121 | 122 | bool wasMoving = isMoving; 123 | isMoving = Mathf.Abs (horizontalInput) > 0.1f || Mathf.Abs (verticalInput) > 0.1f; 124 | 125 | // Target direction relative to the camera 126 | Vector3 targetDirection = horizontalInput * right + verticalInput * forward; 127 | 128 | // Grounded controls 129 | if (grounded) { 130 | // Lock camera for short period when transitioning moving & standing still 131 | lockCameraTimer += Time.deltaTime; 132 | if (isMoving != wasMoving) 133 | lockCameraTimer = 0.0f; 134 | 135 | // We store speed and direction seperately, 136 | // so that when the character stands still we still have a valid forward direction 137 | // moveDirection is always normalized, and we only update it if there is user input. 138 | if (targetDirection != Vector3.zero) { 139 | // If we are really slow, just snap to the target direction 140 | if (moveSpeed < walkSpeed * 0.9f && grounded) { 141 | moveDirection = targetDirection.normalized; 142 | // Otherwise smoothly turn towards it 143 | } else { 144 | moveDirection = Vector3.RotateTowards (moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000); 145 | 146 | moveDirection = moveDirection.normalized; 147 | } 148 | } 149 | 150 | // Smooth the speed based on the current target direction 151 | float curSmooth = speedSmoothing * Time.deltaTime; 152 | 153 | // Choose target speed 154 | //* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways 155 | float targetSpeed = Mathf.Min (targetDirection.magnitude, 1.0f); 156 | 157 | // Pick speed modifier 158 | if (Input.GetButton ("Fire3")) { 159 | targetSpeed *= runSpeed; 160 | } else if (Time.time - trotAfterSeconds > walkTimeStart) { 161 | targetSpeed *= trotSpeed; 162 | } else { 163 | targetSpeed *= walkSpeed; 164 | } 165 | 166 | moveSpeed = Mathf.Lerp (moveSpeed, targetSpeed, curSmooth); 167 | 168 | // Reset walk time start when we slow down 169 | if (moveSpeed < walkSpeed * 0.3f) 170 | walkTimeStart = Time.time; 171 | // In air controls 172 | } else { 173 | // Lock camera while in air 174 | if (jumping) 175 | lockCameraTimer = 0.0f; 176 | 177 | if (isMoving) 178 | inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration; 179 | } 180 | } 181 | 182 | void ApplyWallJump () 183 | { 184 | // We must actually jump against a wall for this to work 185 | if (!jumping) 186 | return; 187 | 188 | // Store when we first touched a wall during this jump 189 | if (collisionFlags == CollisionFlags.CollidedSides && touchWallJumpTime < 0) { 190 | touchWallJumpTime = Time.time; 191 | } 192 | 193 | // The user can trigger a wall jump by hitting the button shortly before or shortly after hitting the wall the first time. 194 | bool mayJump = lastJumpButtonTime > touchWallJumpTime - wallJumpTimeout && lastJumpButtonTime < touchWallJumpTime + wallJumpTimeout; 195 | if (!mayJump) 196 | return; 197 | 198 | // Prevent jumping too fast after each other 199 | if (lastJumpTime + jumpRepeatTime > Time.time) 200 | return; 201 | 202 | wallJumpContactNormal.y = 0; 203 | if (wallJumpContactNormal != Vector3.zero) { 204 | moveDirection = wallJumpContactNormal.normalized; 205 | // Wall jump gives us at least trotspeed 206 | moveSpeed = Mathf.Clamp (moveSpeed * 1.5f, trotSpeed, runSpeed); 207 | } else { 208 | moveSpeed = 0; 209 | } 210 | 211 | verticalSpeed = CalculateJumpVerticalSpeed (jumpHeight); 212 | DidJump (); 213 | thirdPersonSimpleAnimation.SendMessage ("DidWallJump", null, SendMessageOptions.DontRequireReceiver); 214 | } 215 | 216 | void ApplyJumping () 217 | { 218 | // Prevent jumping too fast after each other 219 | if (lastJumpTime + jumpRepeatTime > Time.time) 220 | return; 221 | 222 | if (IsGrounded ()) { 223 | // Jump 224 | // - Only when pressing the button down 225 | // - With a timeout so you can press the button slightly before landing 226 | if (canJump && Time.time < lastJumpButtonTime + jumpTimeout) { 227 | verticalSpeed = CalculateJumpVerticalSpeed (jumpHeight); 228 | DidJump (); 229 | } 230 | } 231 | } 232 | 233 | 234 | void ApplyGravity () 235 | { 236 | // Apply gravity 237 | if (getUserInput) 238 | jumpButton = Input.GetButton ("Jump"); 239 | 240 | // * When falling down we use capeFlyGravity (only when holding down jump) 241 | bool capeFly = canCapeFly && verticalSpeed <= 0.0f && jumpButton && jumping; 242 | 243 | // When we reach the apex of the jump we send out a message 244 | if (jumping && !jumpingReachedApex && verticalSpeed <= 0.0f) { 245 | jumpingReachedApex = true; 246 | SendMessage ("DidJumpReachApex", SendMessageOptions.DontRequireReceiver); 247 | } 248 | 249 | // * When jumping up we don't apply gravity for some time when the user is holding the jump button 250 | // This gives more control over jump height by pressing the button longer 251 | bool extraPowerJump = IsJumping () && verticalSpeed > 0.0f && jumpButton && transform.position.y < lastJumpStartHeight + extraJumpHeight; 252 | 253 | if (capeFly) 254 | verticalSpeed -= capeFlyGravity * Time.deltaTime; else if (extraPowerJump) 255 | return; else if (IsGrounded ()) 256 | verticalSpeed = -gravity * 0.2f; 257 | else 258 | verticalSpeed -= gravity * Time.deltaTime; 259 | } 260 | 261 | float CalculateJumpVerticalSpeed (float targetJumpHeight) 262 | { 263 | // From the jump height and gravity we deduce the upwards speed 264 | // for the character to reach at the apex. 265 | return Mathf.Sqrt (2 * targetJumpHeight * gravity); 266 | } 267 | 268 | void DidJump () 269 | { 270 | jumping = true; 271 | jumpingReachedApex = false; 272 | lastJumpTime = Time.time; 273 | lastJumpStartHeight = transform.position.y; 274 | touchWallJumpTime = -1; 275 | lastJumpButtonTime = -10; 276 | } 277 | 278 | void Update () 279 | { 280 | 281 | if (getUserInput) { 282 | if (Input.GetButtonDown ("Jump")) 283 | lastJumpButtonTime = Time.time; 284 | } else { 285 | if (jumpButton) 286 | lastJumpButtonTime = Time.time; 287 | } 288 | 289 | 290 | UpdateSmoothedMovementDirection (); 291 | 292 | // Apply gravity 293 | // - extra power jump modifies gravity 294 | // - capeFly mode modifies gravity 295 | ApplyGravity (); 296 | 297 | // Perform a wall jump logic 298 | // - Make sure we are jumping against wall etc. 299 | // - Then apply jump in the right direction) 300 | if (canWallJump) 301 | ApplyWallJump (); 302 | 303 | // Apply jumping logic 304 | ApplyJumping (); 305 | 306 | // Calculate actual motion 307 | Vector3 movement = moveDirection * moveSpeed + new Vector3 (0, verticalSpeed, 0) + inAirVelocity; 308 | movement *= Time.deltaTime; 309 | 310 | // Move the controller 311 | CharacterController controller = GetComponent (); 312 | wallJumpContactNormal = Vector3.zero; 313 | collisionFlags = controller.Move (movement); 314 | 315 | // Set rotation to the move direction 316 | if (IsGrounded () && moveDirection != Vector3.zero) { 317 | transform.rotation = Quaternion.LookRotation (moveDirection); 318 | } else { 319 | Vector3 xzMove = movement; 320 | xzMove.y = 0; 321 | if (xzMove.magnitude > 0.001f) { 322 | transform.rotation = Quaternion.LookRotation (xzMove); 323 | } 324 | } 325 | 326 | // We are in jump mode but just became grounded 327 | if (IsGrounded ()) { 328 | lastGroundedTime = Time.time; 329 | inAirVelocity = Vector3.zero; 330 | if (jumping) { 331 | jumping = false; 332 | thirdPersonSimpleAnimation.SendMessage ("DidLand", SendMessageOptions.DontRequireReceiver); 333 | } 334 | } 335 | } 336 | 337 | void OnControllerColliderHit (ControllerColliderHit hit) 338 | { 339 | if (hit.moveDirection.y > 0.01f) 340 | return; 341 | wallJumpContactNormal = hit.normal; 342 | } 343 | 344 | public float GetSpeed () 345 | { 346 | return moveSpeed; 347 | } 348 | 349 | public bool IsJumping () 350 | { 351 | return jumping; 352 | } 353 | 354 | public bool IsGrounded () 355 | { 356 | return (collisionFlags & CollisionFlags.CollidedBelow) != 0; 357 | } 358 | 359 | public void SuperJump (float height) 360 | { 361 | verticalSpeed = CalculateJumpVerticalSpeed (height); 362 | collisionFlags = CollisionFlags.None; 363 | DidJump (); 364 | } 365 | 366 | public void SuperJump (float height, Vector3 jumpVelocity) 367 | { 368 | verticalSpeed = CalculateJumpVerticalSpeed (height); 369 | inAirVelocity = jumpVelocity; 370 | 371 | collisionFlags = CollisionFlags.None; 372 | DidJump (); 373 | } 374 | 375 | 376 | public Vector3 GetDirection () 377 | { 378 | return moveDirection; 379 | } 380 | 381 | public bool IsMovingBackwards () 382 | { 383 | return movingBack; 384 | } 385 | 386 | public float GetLockCameraTimer () 387 | { 388 | return lockCameraTimer; 389 | } 390 | 391 | public float GetLean () 392 | { 393 | return 0.0f; 394 | } 395 | 396 | public bool HasJumpReachedApex () 397 | { 398 | return jumpingReachedApex; 399 | } 400 | 401 | public bool IsGroundedWithTimeout () 402 | { 403 | return lastGroundedTime + groundedTimeout > Time.time; 404 | } 405 | 406 | public bool IsCapeFlying () 407 | { 408 | // * When falling down we use capeFlyGravity (only when holding down jump) 409 | if (getUserInput) 410 | jumpButton = Input.GetButton ("Jump"); 411 | return canCapeFly && verticalSpeed <= 0.0f && jumpButton && jumping; 412 | } 413 | 414 | void Reset () 415 | { 416 | gameObject.tag = "Player"; 417 | } 418 | 419 | 420 | } 421 | -------------------------------------------------------------------------------- /Assets/Scripts/CharacterControls-(No Networking code)/ThirdPersonController.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 21aa87d34e9fb444f9e604bf0ddf1107 3 | -------------------------------------------------------------------------------- /Assets/Scripts/CharacterControls-(No Networking code)/ThirdPersonPushBodies.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | [RequireComponent(typeof(ThirdPersonController))] 5 | public class ThirdPersonPushBodies : MonoBehaviour { 6 | 7 | public float pushPower = 0.5f; 8 | public LayerMask pushLayers = -1; 9 | private ThirdPersonController controller ; 10 | 11 | 12 | void Start () 13 | { 14 | controller = GetComponent (); 15 | } 16 | 17 | void OnControllerColliderHit (ControllerColliderHit hit) 18 | { 19 | Rigidbody body = hit.collider.attachedRigidbody; 20 | // no rigidbody 21 | if (body == null || body.isKinematic) 22 | return; 23 | // Ignore pushing those rigidbodies 24 | int bodyLayerMask = 1 << body.gameObject.layer; 25 | if ((bodyLayerMask & pushLayers.value) == 0) 26 | return; 27 | 28 | // We dont want to push objects below us 29 | if (hit.moveDirection.y < -0.3) 30 | return; 31 | 32 | // Calculate push direction from move direction, we only push objects to the sides 33 | // never up and down 34 | Vector3 pushDir =new Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z); 35 | 36 | // push with move speed but never more than walkspeed 37 | body.velocity = pushDir * pushPower * Mathf.Min(controller.GetSpeed(), controller.walkSpeed); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Assets/Scripts/CharacterControls-(No Networking code)/ThirdPersonPushBodies.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 29f6a3f940e844b92bcdacbb3ff0b243 3 | -------------------------------------------------------------------------------- /Assets/Scripts/CharacterControls-(No Networking code)/ThirdPersonSimpleAnimation.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | [AddComponentMenu("Third Person Player/Third Person Player Animation")] 5 | public class ThirdPersonSimpleAnimation : MonoBehaviour 6 | { 7 | 8 | public float runSpeedScale = 1.0f; 9 | public float walkSpeedScale = 1.0f; 10 | public Transform torso; 11 | private NetworkSyncAnimation networkSyncAnimation; 12 | void Awake () 13 | { 14 | // By default loop all animations 15 | animation.wrapMode = WrapMode.Loop; 16 | 17 | // We are in full control here - don't let any other animations play when we start 18 | animation.Stop (); 19 | animation.Play ("idle"); 20 | } 21 | 22 | void Update () 23 | { 24 | ThirdPersonController marioController = GetComponent (); 25 | float currentSpeed = marioController.GetSpeed (); 26 | networkSyncAnimation = GetComponent (); 27 | 28 | // Fade in run 29 | if (currentSpeed > marioController.walkSpeed) { 30 | animation.CrossFade ("run"); 31 | // We fade out jumpland quick otherwise we get sliding feet 32 | animation.Blend ("jumpland", 0); 33 | networkSyncAnimation.SendMessage ("SyncAnimation", "run"); 34 | // Fade in walk 35 | } else if (currentSpeed > 0.1f) { 36 | animation.CrossFade ("walk"); 37 | // We fade out jumpland realy quick otherwise we get sliding feet 38 | animation.Blend ("jumpland", 0); 39 | networkSyncAnimation.SendMessage ("SyncAnimation", "walk"); 40 | // Fade out walk and run 41 | } else { 42 | animation.CrossFade ("idle"); 43 | networkSyncAnimation.SendMessage ("SyncAnimation", "idle"); 44 | } 45 | 46 | animation["run"].normalizedSpeed = runSpeedScale; 47 | animation["walk"].normalizedSpeed = walkSpeedScale; 48 | 49 | if (marioController.IsJumping ()) { 50 | if (marioController.IsCapeFlying ()) { 51 | animation.CrossFade ("jetpackjump", 0.2f); 52 | networkSyncAnimation.SendMessage ("SyncAnimation", "jetpackjump"); 53 | } else if (marioController.HasJumpReachedApex ()) { 54 | animation.CrossFade ("jumpfall", 0.2f); 55 | networkSyncAnimation.SendMessage ("SyncAnimation", "jumpfall"); 56 | } else { 57 | animation.CrossFade ("jump", 0.2f); 58 | networkSyncAnimation.SendMessage ("SyncAnimation", "jump"); 59 | } 60 | // We fell down somewhere 61 | } else if (!marioController.IsGroundedWithTimeout ()) { 62 | animation.CrossFade ("ledgefall", 0.2f); 63 | networkSyncAnimation.SendMessage ("SyncAnimation", "ledgefall"); 64 | // We are not falling down anymore 65 | } else { 66 | animation.Blend ("ledgefall", 0.0f, 0.2f); 67 | } 68 | } 69 | 70 | public void DidLand () 71 | { 72 | animation.Play ("jumpland"); 73 | networkSyncAnimation.SendMessage ("SyncAnimation", "jumpland"); 74 | } 75 | 76 | public void DidPunch () 77 | { 78 | animation.CrossFadeQueued ("punch", 0.3f, QueueMode.PlayNow); 79 | } 80 | 81 | public void DidButtStomp () 82 | { 83 | animation.CrossFade ("buttstomp", 0.1f); 84 | networkSyncAnimation.SendMessage ("SyncAnimation", "buttstomp"); 85 | animation.CrossFadeQueued ("jumpland", 0.2f); 86 | } 87 | 88 | public void ApplyDamage () 89 | { 90 | animation.CrossFade ("gothit", 0.1f); 91 | networkSyncAnimation.SendMessage ("SyncAnimation", "gothit"); 92 | } 93 | 94 | 95 | public void DidWallJump () 96 | { 97 | // Wall jump animation is played without fade. 98 | // We are turning the character controller 180 degrees around when doing a wall jump so the animation accounts for that. 99 | // But we really have to make sure that the animation is in full control so 100 | // that we don't do weird blends between 180 degree apart rotations 101 | animation.Play ("walljump"); 102 | networkSyncAnimation.SendMessage ("SyncAnimation", "walljump"); 103 | } 104 | 105 | 106 | } 107 | -------------------------------------------------------------------------------- /Assets/Scripts/CharacterControls-(No Networking code)/ThirdPersonSimpleAnimation.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 556f5e51a8e794d5a9fb5126ed58b3ec 3 | -------------------------------------------------------------------------------- /Assets/Scripts/Networking Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: e14b6d22cd1103501100383a44295342 3 | -------------------------------------------------------------------------------- /Assets/Scripts/Networking Scripts/ConnectGui.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class ConnectGui : MonoBehaviour 5 | { 6 | 7 | public string remoteIP = "127.0.0.1"; 8 | public int remotePort = 25001; 9 | public int listenPort = 25000; 10 | public int remoteGUID = 0; 11 | public bool useNat = false; 12 | // private string connectionInfo = ""; 13 | 14 | void Awake () 15 | { 16 | if (FindObjectOfType (typeof(ConnectGuiMasterServer))) 17 | this.enabled = false; 18 | } 19 | 20 | void OnGUI () 21 | { 22 | GUILayout.Space (10); 23 | GUILayout.BeginHorizontal (); 24 | GUILayout.Space (10); 25 | if (Network.peerType == NetworkPeerType.Disconnected) { 26 | useNat = GUILayout.Toggle (useNat, "Use NAT punchthrough"); 27 | GUILayout.EndHorizontal (); 28 | GUILayout.BeginHorizontal (); 29 | GUILayout.Space (10); 30 | 31 | GUILayout.BeginVertical (); 32 | if (GUILayout.Button ("Connect")) { 33 | if (useNat) { 34 | if (remoteGUID != 0) 35 | Debug.LogWarning ("Invalid GUID given, must be a valid one as reported by Network.player.guid or returned in a HostData struture from the master server"); 36 | else 37 | Network.Connect (remoteGUID.ToString ()); 38 | } else { 39 | Network.Connect (remoteIP, remotePort); 40 | } 41 | } 42 | if (GUILayout.Button ("Start Server")) { 43 | Network.InitializeServer (32, listenPort, useNat); 44 | // Notify our objects that the level and the network is ready 45 | foreach (GameObject go in FindObjectsOfType (typeof(GameObject))) 46 | go.SendMessage ("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver); 47 | } 48 | GUILayout.EndVertical (); 49 | if (useNat) { 50 | remoteGUID = int.Parse (GUILayout.TextField (remoteGUID.ToString (), GUILayout.MinWidth (145))); 51 | } else { 52 | remoteIP = GUILayout.TextField (remoteIP, GUILayout.MinWidth (100)); 53 | remotePort = int.Parse (GUILayout.TextField (remotePort.ToString ())); 54 | } 55 | } else { 56 | if (useNat) 57 | GUILayout.Label ("GUID: " + Network.player.guid + " - "); 58 | GUILayout.Label ("Local IP/port: " + Network.player.ipAddress + "/" + Network.player.port); 59 | GUILayout.Label (" - External IP/port: " + Network.player.externalIP + "/" + Network.player.externalPort); 60 | GUILayout.EndHorizontal (); 61 | GUILayout.BeginHorizontal (); 62 | if (GUILayout.Button ("Disconnect")) 63 | Network.Disconnect (200); 64 | } 65 | GUILayout.FlexibleSpace (); 66 | GUILayout.EndHorizontal (); 67 | } 68 | 69 | void OnServerInitialized () 70 | { 71 | if (useNat) 72 | Debug.Log ("==> GUID is " + Network.player.guid + ". Use this on clients to connect with NAT punchthrough."); 73 | Debug.Log ("==> Local IP/port is " + Network.player.ipAddress + "/" + Network.player.port + ". Use this on clients to connect directly."); 74 | } 75 | 76 | void OnConnectedToServer () 77 | { 78 | // Notify our objects that the level and the network is ready 79 | foreach (GameObject go in FindObjectsOfType (typeof(GameObject))) 80 | go.SendMessage ("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver); 81 | } 82 | 83 | void OnDisconnectedFromServer () 84 | { 85 | if (this.enabled != false) 86 | Application.LoadLevel (Application.loadedLevel); 87 | else { 88 | NetworkLevelLoad n = (NetworkLevelLoad)FindObjectOfType (typeof(NetworkLevelLoad)); 89 | n.OnDisconnectedFromServer (); 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /Assets/Scripts/Networking Scripts/ConnectGui.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 2f99cf7149d814f0a8978daa99425b62 3 | -------------------------------------------------------------------------------- /Assets/Scripts/Networking Scripts/ConnectGuiMasterServer.cs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Assets/Scripts/Networking Scripts/ConnectGuiMasterServer.cs -------------------------------------------------------------------------------- /Assets/Scripts/Networking Scripts/ConnectGuiMasterServer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: e1e9b17dc51c64d838230c22dd6ec8cd 3 | -------------------------------------------------------------------------------- /Assets/Scripts/Networking Scripts/CreateConnectionGUI.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class CreateConnectionGUI : MonoBehaviour 5 | { 6 | 7 | public GameObject connectionGUI; 8 | void Awake () 9 | { 10 | ConnectGuiMasterServer cgm = FindObjectOfType (typeof(ConnectGuiMasterServer)) as ConnectGuiMasterServer; 11 | if (cgm == null) 12 | Instantiate (connectionGUI, Vector3.zero, Quaternion.identity); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Assets/Scripts/Networking Scripts/CreateConnectionGUI.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 40584b9c61aef4bed87f280fb52b8ea1 3 | -------------------------------------------------------------------------------- /Assets/Scripts/Networking Scripts/NetworkInterpolatedTransform.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class NetworkInterpolatedTransform : MonoBehaviour 5 | { 6 | 7 | public double interpolationBackTime = 0.1; 8 | 9 | internal struct State 10 | { 11 | internal double timestamp; 12 | internal Vector3 pos; 13 | internal Quaternion rot; 14 | } 15 | 16 | // We store twenty states with "playback" information 17 | State[] m_BufferedState = new State[20]; 18 | // Keep track of what slots are used 19 | int m_TimestampCount = 0; 20 | 21 | void OnSerializeNetworkView (BitStream stream, NetworkMessageInfo info) 22 | { 23 | // Always send transform (depending on reliability of the network view) 24 | if (stream.isWriting) { 25 | Vector3 pos = transform.localPosition; 26 | Quaternion rot = transform.localRotation; 27 | stream.Serialize (ref pos); 28 | stream.Serialize (ref rot); 29 | // When receiving, buffer the information 30 | } else { 31 | // Receive latest state information 32 | Vector3 pos = Vector3.zero; 33 | Quaternion rot = Quaternion.identity; 34 | stream.Serialize (ref pos); 35 | stream.Serialize (ref rot); 36 | // Shift buffer contents, oldest data erased, 18 becomes 19, ... , 0 becomes 1 37 | for (int i = m_BufferedState.Length - 1; i >= 1; i--) { 38 | m_BufferedState[i] = m_BufferedState[i - 1]; 39 | } 40 | 41 | // Save currect received state as 0 in the buffer, safe to overwrite after shifting 42 | State state; 43 | state.timestamp = info.timestamp; 44 | state.pos = pos; 45 | state.rot = rot; 46 | m_BufferedState[0] = state; 47 | 48 | // Increment state count but never exceed buffer size 49 | m_TimestampCount = Mathf.Min (m_TimestampCount + 1, m_BufferedState.Length); 50 | 51 | // Check integrity, lowest numbered state in the buffer is newest and so on 52 | for (int i = 0; i < m_TimestampCount - 1; i++) { 53 | if (m_BufferedState[i].timestamp < m_BufferedState[i + 1].timestamp) 54 | Debug.Log ("State inconsistent"); 55 | } 56 | 57 | //Debug.Log("stamp: " + info.timestamp + "my time: " + Network.time + "delta: " + (Network.time - info.timestamp)); 58 | } 59 | } 60 | 61 | // This only runs where the component is enabled, which is only on remote peers (server/clients) 62 | void Update () 63 | { 64 | double currentTime = Network.time; 65 | double interpolationTime = currentTime - interpolationBackTime; 66 | // We have a window of interpolationBackTime where we basically play 67 | // By having interpolationBackTime the average ping, you will usually use interpolation. 68 | // And only if no more data arrives we will use extrapolation 69 | 70 | // Use interpolation 71 | // Check if latest state exceeds interpolation time, if this is the case then 72 | // it is too old and extrapolation should be used 73 | if (m_BufferedState[0].timestamp > interpolationTime) { 74 | for (int i = 0; i < m_TimestampCount; i++) { 75 | // Find the state which matches the interpolation time (time+0.1) or use last state 76 | if (m_BufferedState[i].timestamp <= interpolationTime || i == m_TimestampCount - 1) { 77 | // The state one slot newer (<100ms) than the best playback state 78 | State rhs = m_BufferedState[Mathf.Max (i - 1, 0)]; 79 | // The best playback state (closest to 100 ms old (default time)) 80 | State lhs = m_BufferedState[i]; 81 | 82 | // Use the time between the two slots to determine if interpolation is necessary 83 | double length = rhs.timestamp - lhs.timestamp; 84 | float t = 0.0f; 85 | // As the time difference gets closer to 100 ms t gets closer to 1 in 86 | // which case rhs is only used 87 | if (length > 0.0001) 88 | t = (float)((interpolationTime - lhs.timestamp) / length); 89 | 90 | // if t=0 => lhs is used directly 91 | transform.localPosition = Vector3.Lerp (lhs.pos, rhs.pos, t); 92 | transform.localRotation = Quaternion.Slerp (lhs.rot, rhs.rot, t); 93 | return; 94 | } 95 | } 96 | // Use extrapolation. Here we do something really simple and just repeat the last 97 | // received state. You can do clever stuff with predicting what should happen. 98 | } else { 99 | State latest = m_BufferedState[0]; 100 | transform.localPosition = latest.pos; 101 | transform.localRotation = latest.rot; 102 | } 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /Assets/Scripts/Networking Scripts/NetworkInterpolatedTransform.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 82eea118bd1113af1100e0fb44295342 3 | -------------------------------------------------------------------------------- /Assets/Scripts/Networking Scripts/NetworkLevelLoad.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | [RequireComponent(typeof(NetworkView))] 5 | public class NetworkLevelLoad : MonoBehaviour 6 | { 7 | 8 | public string[] supportedNetworkLevels = new string[] { "mylevel" }; 9 | public string disconnectedLevel = "loader"; 10 | 11 | // Keep track of the last level prefix (increment each time a new level loads) 12 | private int lastLevelPrefix = 0; 13 | 14 | void Awake () 15 | { 16 | // Network level loading is done in a seperate channel. 17 | DontDestroyOnLoad (this); 18 | networkView.@group = 1; 19 | } 20 | 21 | void OnGUI () 22 | { 23 | // When network is running (server or client) then display the levels 24 | // configured in the supportedNetworkLevels array and allow them to be loaded 25 | // at the push of a button 26 | if (Network.peerType != NetworkPeerType.Disconnected) { 27 | GUILayout.BeginArea (new Rect (Screen.width - 100, 80, 100, 30)); 28 | GUILayout.BeginHorizontal (); 29 | 30 | foreach (string level in supportedNetworkLevels) { 31 | if (GUILayout.Button (level)) { 32 | // Make sure no old RPC calls are buffered and then send load level command 33 | Network.RemoveRPCsInGroup (0); 34 | Network.RemoveRPCsInGroup (1); 35 | // Load level with incremented level prefix (for view IDs) 36 | networkView.RPC ("LoadLevel", RPCMode.AllBuffered, level, lastLevelPrefix + 1); 37 | } 38 | } 39 | GUILayout.FlexibleSpace (); 40 | GUILayout.EndHorizontal (); 41 | GUILayout.EndArea (); 42 | } 43 | } 44 | 45 | [RPC] 46 | IEnumerator LoadLevel (string level, int levelPrefix) 47 | { 48 | Debug.Log ("Loading level " + level + " with prefix " + levelPrefix); 49 | lastLevelPrefix = levelPrefix; 50 | 51 | // There is no reason to send any more data over the network on the default channel, 52 | // because we are about to load the level, thus all those objects will get deleted anyway 53 | Network.SetSendingEnabled (0, false); 54 | 55 | // We need to stop receiving because first the level must be loaded. 56 | // Once the level is loaded, RPC's and other state update attached to objects in the level are allowed to fire 57 | Network.isMessageQueueRunning = false; 58 | 59 | // All network views loaded from a level will get a prefix into their NetworkViewID. 60 | // This will prevent old updates from clients leaking into a newly created scene. 61 | Network.SetLevelPrefix (levelPrefix); 62 | Application.LoadLevel (level); 63 | //yield; 64 | yield return new WaitForSeconds (0); 65 | 66 | // Allow receiving data again 67 | Network.isMessageQueueRunning = true; 68 | // Now the level has been loaded and we can start sending out data 69 | Network.SetSendingEnabled (0, true); 70 | 71 | // Notify our objects that the level and the network is ready 72 | foreach (GameObject go in FindObjectsOfType (typeof(GameObject))) 73 | go.SendMessage ("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver); 74 | } 75 | 76 | public void OnDisconnectedFromServer () 77 | { 78 | Application.LoadLevel (disconnectedLevel); 79 | } 80 | 81 | 82 | } 83 | -------------------------------------------------------------------------------- /Assets/Scripts/Networking Scripts/NetworkLevelLoad.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 5e8f3daccad064e689688e8ccdd6e484 3 | -------------------------------------------------------------------------------- /Assets/Scripts/Networking Scripts/NetworkRigidbody.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class NetworkRigidbody : MonoBehaviour 5 | { 6 | 7 | public double m_InterpolationBackTime = 0.1; 8 | public double m_ExtrapolationLimit = 0.5; 9 | 10 | internal struct State 11 | { 12 | internal double timestamp; 13 | internal Vector3 pos; 14 | internal Vector3 velocity; 15 | internal Quaternion rot; 16 | internal Vector3 angularVelocity; 17 | } 18 | 19 | // We store twenty states with "playback" information 20 | State[] m_BufferedState = new State[20]; 21 | // Keep track of what slots are used 22 | int m_TimestampCount; 23 | 24 | void Start () 25 | { 26 | foreach (NetworkView n in GetComponents ()) { 27 | n.observed = this; 28 | } 29 | } 30 | 31 | void OnSerializeNetworkView (BitStream stream, NetworkMessageInfo info) 32 | { 33 | // Send data to server 34 | if (stream.isWriting) { 35 | Vector3 pos = rigidbody.position; 36 | Quaternion rot = rigidbody.rotation; 37 | Vector3 velocity = rigidbody.velocity; 38 | Vector3 angularVelocity = rigidbody.angularVelocity; 39 | 40 | stream.Serialize (ref pos); 41 | stream.Serialize (ref velocity); 42 | stream.Serialize (ref rot); 43 | stream.Serialize (ref angularVelocity); 44 | // Read data from remote client 45 | } else { 46 | Vector3 pos = Vector3.zero; 47 | Vector3 velocity = Vector3.zero; 48 | Quaternion rot = Quaternion.identity; 49 | Vector3 angularVelocity = Vector3.zero; 50 | stream.Serialize (ref pos); 51 | stream.Serialize (ref velocity); 52 | stream.Serialize (ref rot); 53 | stream.Serialize (ref angularVelocity); 54 | 55 | // Shift the buffer sideways, deleting state 20 56 | for (int i = m_BufferedState.Length - 1; i >= 1; i--) { 57 | m_BufferedState[i] = m_BufferedState[i - 1]; 58 | } 59 | 60 | // Record current state in slot 0 61 | State state; 62 | state.timestamp = info.timestamp; 63 | state.pos = pos; 64 | state.velocity = velocity; 65 | state.rot = rot; 66 | state.angularVelocity = angularVelocity; 67 | m_BufferedState[0] = state; 68 | 69 | // Update used slot count, however never exceed the buffer size 70 | // Slots aren't actually freed so this just makes sure the buffer is 71 | // filled up and that uninitalized slots aren't used. 72 | m_TimestampCount = Mathf.Min (m_TimestampCount + 1, m_BufferedState.Length); 73 | 74 | // Check if states are in order, if it is inconsistent you could reshuffel or 75 | // drop the out-of-order state. Nothing is done here 76 | for (int i = 0; i < m_TimestampCount - 1; i++) { 77 | if (m_BufferedState[i].timestamp < m_BufferedState[i + 1].timestamp) 78 | Debug.Log ("State inconsistent"); 79 | } 80 | } 81 | } 82 | 83 | // We have a window of interpolationBackTime where we basically play 84 | // By having interpolationBackTime the average ping, you will usually use interpolation. 85 | // And only if no more data arrives we will use extra polation 86 | void Update () 87 | { 88 | // This is the target playback time of the rigid body 89 | double interpolationTime = Network.time - m_InterpolationBackTime; 90 | 91 | // Use interpolation if the target playback time is present in the buffer 92 | if (m_BufferedState[0].timestamp > interpolationTime) { 93 | // Go through buffer and find correct state to play back 94 | for (int i = 0; i < m_TimestampCount; i++) { 95 | if (m_BufferedState[i].timestamp <= interpolationTime || i == m_TimestampCount - 1) { 96 | // The state one slot newer (<100ms) than the best playback state 97 | State rhs = m_BufferedState[Mathf.Max (i - 1, 0)]; 98 | // The best playback state (closest to 100 ms old (default time)) 99 | State lhs = m_BufferedState[i]; 100 | 101 | // Use the time between the two slots to determine if interpolation is necessary 102 | double length = rhs.timestamp - lhs.timestamp; 103 | float t = 0.0f; 104 | // As the time difference gets closer to 100 ms t gets closer to 1 in 105 | // which case rhs is only used 106 | // Example: 107 | // Time is 10.000, so sampleTime is 9.900 108 | // lhs.time is 9.910 rhs.time is 9.980 length is 0.070 109 | // t is 9.900 - 9.910 / 0.070 = 0.14. So it uses 14% of rhs, 86% of lhs 110 | if (length > 0.0001f) 111 | t = (float)((interpolationTime - lhs.timestamp) / length); 112 | 113 | // if t=0 => lhs is used directly 114 | transform.localPosition = Vector3.Lerp (lhs.pos, rhs.pos, t); 115 | transform.localRotation = Quaternion.Slerp (lhs.rot, rhs.rot, t); 116 | return; 117 | } 118 | } 119 | // Use extrapolation 120 | } else { 121 | State latest = m_BufferedState[0]; 122 | 123 | float extrapolationLength = (float)(interpolationTime - latest.timestamp); 124 | // Don't extrapolation for more than 500 ms, you would need to do that carefully 125 | if (extrapolationLength < m_ExtrapolationLimit) { 126 | float axisLength = extrapolationLength * latest.angularVelocity.magnitude * Mathf.Rad2Deg; 127 | Quaternion angularRotation = Quaternion.AngleAxis (axisLength, latest.angularVelocity); 128 | 129 | rigidbody.position = latest.pos + latest.velocity * extrapolationLength; 130 | rigidbody.rotation = angularRotation * latest.rot; 131 | rigidbody.velocity = latest.velocity; 132 | rigidbody.angularVelocity = latest.angularVelocity; 133 | } 134 | } 135 | } 136 | 137 | } 138 | -------------------------------------------------------------------------------- /Assets/Scripts/Networking Scripts/NetworkRigidbody.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: eb1a283acd11b900110003a944295342 3 | -------------------------------------------------------------------------------- /Assets/Scripts/Networking Scripts/NetworkSyncAnimation.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | 5 | public class NetworkSyncAnimation : MonoBehaviour 6 | { 7 | 8 | public enum AniStates 9 | { 10 | walk = 0, 11 | run, 12 | kick, 13 | punch, 14 | jump, 15 | jumpfall, 16 | idle, 17 | gotbit, 18 | gothit, 19 | walljump, 20 | deathfall, 21 | jetpackjump, 22 | ledgefall, 23 | buttstomp, 24 | jumpland 25 | } 26 | 27 | public AniStates currentAnimation = AniStates.idle; 28 | public AniStates lastAnimation = AniStates.idle; 29 | 30 | public void SyncAnimation (String animationValue) 31 | { 32 | 33 | 34 | currentAnimation = (AniStates)Enum.Parse (typeof(AniStates), animationValue); 35 | if (gameObject.name.Contains ("Remote")) { 36 | print ("SyncAnimation : " + animationValue); 37 | print ("currentAnimation : " + currentAnimation); 38 | } 39 | } 40 | 41 | // Update is called once per frame 42 | void Update () 43 | { 44 | 45 | if (lastAnimation != currentAnimation) { 46 | lastAnimation = currentAnimation; 47 | animation.CrossFade (Enum.GetName (typeof(AniStates), currentAnimation)); 48 | animation["run"].normalizedSpeed = 1.0f; 49 | animation["walk"].normalizedSpeed = 1.0f; 50 | } 51 | } 52 | 53 | void OnSerializeNetworkView (BitStream stream, NetworkMessageInfo info) 54 | { 55 | if (stream.isWriting) { 56 | char ani = (char)currentAnimation; 57 | stream.Serialize (ref ani); 58 | } else { 59 | char ani = (char)0; 60 | stream.Serialize (ref ani); 61 | 62 | currentAnimation = (AniStates)ani; 63 | } 64 | 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /Assets/Scripts/Networking Scripts/NetworkSyncAnimation.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 47fdb07083cbd4fcaaf266ea956cd853 3 | -------------------------------------------------------------------------------- /Assets/Scripts/Networking Scripts/SpawnPrefab.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class SpawnPrefab : MonoBehaviour 5 | { 6 | 7 | public Transform playerPrefab; 8 | 9 | void OnNetworkLoadedLevel () 10 | { 11 | Network.Instantiate (playerPrefab, transform.position, transform.rotation, 0); 12 | } 13 | 14 | void OnPlayerDisconnected (NetworkPlayer player) 15 | { 16 | Debug.Log ("Server destroying player"); 17 | Network.RemoveRPCs (player, 0); 18 | Network.DestroyPlayerObjects (player); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Assets/Scripts/Networking Scripts/SpawnPrefab.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 55f978c4f87a445c28cfe4943590a147 3 | -------------------------------------------------------------------------------- /Assets/Scripts/Networking Scripts/ThirdPersonNetworkInit.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class ThirdPersonNetworkInit : MonoBehaviour 5 | { 6 | void OnNetworkInstantiate (NetworkMessageInfo msg) 7 | { 8 | // This is our own player 9 | if (networkView.isMine) { 10 | Camera.main.SendMessage ("SetTarget", transform); 11 | GetComponent ().enabled = false; 12 | // This is just some remote controlled player 13 | } else { 14 | name += "Remote"; 15 | GetComponent ().enabled = false; 16 | GetComponent ().enabled = false; 17 | GetComponent ().enabled = true; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Assets/Scripts/Networking Scripts/ThirdPersonNetworkInit.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: 6ce3428ea86f044389189e2e9068e361 3 | -------------------------------------------------------------------------------- /Assets/Scripts/ThirdPersonAction.cs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Assets/Scripts/ThirdPersonAction.cs -------------------------------------------------------------------------------- /Assets/Scripts/ThirdPersonAction.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 1 2 | guid: c573acdb0abc94750a819a43c34fd12f 3 | -------------------------------------------------------------------------------- /Library/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Library/AudioManager.asset -------------------------------------------------------------------------------- /Library/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Library/DynamicsManager.asset -------------------------------------------------------------------------------- /Library/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Library/EditorBuildSettings.asset -------------------------------------------------------------------------------- /Library/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Library/InputManager.asset -------------------------------------------------------------------------------- /Library/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Library/NetworkManager.asset -------------------------------------------------------------------------------- /Library/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Library/ProjectSettings.asset -------------------------------------------------------------------------------- /Library/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Library/QualitySettings.asset -------------------------------------------------------------------------------- /Library/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Library/TagManager.asset -------------------------------------------------------------------------------- /Library/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anchan828/unity-network-sample/f5d4b5b2b00cea397476b30c4a173e7cdc58af2b/Library/TimeManager.asset -------------------------------------------------------------------------------- /README.textile: -------------------------------------------------------------------------------- 1 | h1. 概要 2 | 3 | Unityの "ネットワークサンプル":http://unity3d.com/support/resources/example-projects/networking-example.html を簡略化したもです。 4 | ThirdPersonControllerの同期、キューブの生成と同期を行なっています。 5 | 6 | !https://github.com/anchan828/file-place/raw/master/unity-network-main.png! 7 | 8 | h1. マスターサーバーを走らせる 9 | 10 | ネットワークのサンプルを動かすにはマスターサーバーを立てる必要があります。 11 | まずは "Unity Networking Servers":http://unity3d.com/master-server/index.html からMaster Serverをクリックしプロジェクトをダウンロードします。 12 | 13 | !https://github.com/anchan828/file-place/raw/master/masterserverfolder.png! 14 | 15 | MasterServer-2.0.1f1の中身 16 | 17 | h5. MacOSXの場合 18 | 19 | # xcodeフォルダの中のプロジェクトを起動。 20 | # 「Compiler for C/C++/Objective-C」で「LLVM GCC 4.2」を選択。 21 | # Runでマスターサーバーを走らせる。 22 | 23 | h5. Windowsの場合 24 | 25 | # VisualStudioフォルダの中のプロジェクトを起動。 26 | # ビルドをしプロジェクトフォルダ内にできる「Debug」フォルダの「MasterServer.exe」をダブルクリックしてマスターサーバーを走らせる。 27 | 28 | 29 | h1. マスターサーバーへの接続 30 | 31 | 「Scene/loader」をクリックしてUnityプロジェクトを開き「Prefab/ConnectionGUI」に付加されている「ConnectGuiMasterServer」の「Ip Address」をプライベートIPアドレスに変更する。 32 | 33 | !https://github.com/anchan828/file-place/raw/master/connectgui.png! 34 | 35 | # ここでプラットフォーム「スタンドアローン」を選択してビルドする。そしてアプリを起動。 36 | # 片方のゲームビューの「サーバーコンソール」から「サーバー起動」を選択しサーバーを走らせ、「Third Person」をクリックする。 37 | # 残りのもう片方は一度「サーバーコンソール」の「リフレッシュ」を押す。 38 | すると下のようなウィンドウが現れる。 39 | 40 | !https://github.com/anchan828/file-place/raw/master/gameviewconnect.png! 41 | 42 | 「接続」ボタンを押すと「Third Person」のシーンへ移動する。 43 | 44 | これで同期は取れてますので「Lerpz」を動かしまくりましょう 45 | 46 | h1. キューブの生成 47 | 48 | 本家のサンプルでは少しごちゃごちゃしていてわかりにくかったので、 49 | こちらでRigidbodyの付いたキューブを生成するコードを書いてみました。(zでキューブ生成) 50 | 51 | 多少はわかりやすいはず... 52 | 53 | キューブを生成するアクションを起こすコードは「Scripts/ThirdPersonAction」に記述しています。 54 | 55 | Network.Instantiateで生成するというもの。 56 | 57 | 「Prefab/Cube」が生成されるオブジェクトです。「NetworkView」をつけることでIDが割り当てられ、管理をすることができます。 58 | 59 | 「NetworkRigidbody」をつけることによって物理演算処理の同期をとることができます。 60 | 61 | 生成するオブジェクトがRigidbodyでない場合は「NetworkInterpolatedTransform」を付加させましょう(たぶん) 62 | 63 | --------------------------------------------------------------------------------