/// <summary> /// An example of how to teleport the player so that position will not be interpolated on non-owners. /// </summary> /// <remarks> /// Teleporting requires a command to be sent from the owner to the host. /// The SmoothSync teleport method is be called immediately on the owner before sending the command. /// </remarks> /// <param name="pos">The position to teleport to.</param> /// <param name="rot">The rotation to teleport to.</param> public void teleport(Vector3 pos, Quaternion rot) { if (!hasAuthority) { return; } int timestamp = NetworkTransport.GetNetworkTimestamp(); // Teleport owned object smoothSync.teleport(timestamp, pos, rot); // Tell host to tell non-owners to teleport CmdTeleport(timestamp, pos, rot); }
/// <summary> /// Simple movement. /// <remarks> /// Adds rigidbodyMovementForce on keypress if there is a Rigidbody. /// Shifts position based on transformMovementSpeed if there is no Rigidbody. /// Arrow keys control the parent rigidbody forces while WASD controls the child transform. /// Use + and - with left shift to scale the parent, use + and - with right shift to scale the child. /// Press '0' to stop instantly. /// </remarks> /// </summary> void Update() { if (!hasAuthority) { return; } // If you need to move a synced object and don't want the position to be interpolated on non-owners, // you can use the teleport() method. This is useful for things like respawning. if (Input.GetKeyDown(KeyCode.T)) { transform.position = transform.position + Vector3.right * 10; smoothSync.teleport(); } // If you need to send a State update, call forceStateSendNextFrame() and the next fixed update's State will be sent. // Useful for collisions and fast changes in direction so you can be as accurate as possible in between your send rate. if (Input.GetKeyDown(KeyCode.F)) { smoothSync.forceStateSendNextFixedUpdate(); } // If you need to change ownership of a SmoothSync object, clear the buffer on all instances. // You'll probably want to do NetworkIdentity.AssignClientAuthority(conn) and these Cmds and Rpcs // on a separate object that's always owned by the server so that there's no ownership changes of // the object while you are trying to send these Cmds and Rpcs. if (Input.GetKeyDown(KeyCode.C)) { CmdClearBuffer(); } #region Movement Controls float speed = transformMovementSpeed * Time.deltaTime; // Scale changes here if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.Equals)) { transform.localScale = transform.localScale + new Vector3(1, 1, 1) * speed * .2f; } if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.Minus)) { transform.localScale = transform.localScale - new Vector3(1, 1, 1) * speed * .2f; } if (childObjectToControl) { if (Input.GetKey(KeyCode.RightShift) && Input.GetKey(KeyCode.Equals)) { childObjectToControl.transform.localScale = childObjectToControl.transform.localScale + new Vector3(1, 1, 1) * speed * .2f; } if (Input.GetKey(KeyCode.RightShift) && Input.GetKey(KeyCode.Minus)) { childObjectToControl.transform.localScale = childObjectToControl.transform.localScale - new Vector3(1, 1, 1) * speed * .2f; } } // Child position changes here if (childObjectToControl) { if (Input.GetKey(KeyCode.S)) { childObjectToControl.transform.position = childObjectToControl.transform.position + new Vector3(0, -1.5f, -1) * speed; } if (Input.GetKey(KeyCode.W)) { childObjectToControl.transform.position = childObjectToControl.transform.position + new Vector3(0, 1.5f, 1) * speed; } if (Input.GetKey(KeyCode.A)) { childObjectToControl.transform.position = childObjectToControl.transform.position + new Vector3(-1, 0, 0) * speed; } if (Input.GetKey(KeyCode.D)) { childObjectToControl.transform.position = childObjectToControl.transform.position + new Vector3(1, 0, 0) * speed; } } if (rb) { // Lose all velocity by pressing '0' if (Input.GetKeyDown(KeyCode.Alpha0)) { rb.velocity = Vector3.zero; rb.angularVelocity = Vector3.zero; } // Add forces to the parent rigidbody if (Input.GetKeyDown(KeyCode.DownArrow)) { rb.AddForce(new Vector3(0, -1.5f, -1) * rigidbodyMovementForce); } if (Input.GetKeyDown(KeyCode.UpArrow)) { rb.AddForce(new Vector3(0, 1.5f, 1) * rigidbodyMovementForce); } if (Input.GetKeyDown(KeyCode.LeftArrow)) { rb.AddForce(new Vector3(-1, 0, 0) * rigidbodyMovementForce); } if (Input.GetKeyDown(KeyCode.RightArrow)) { rb.AddForce(new Vector3(1, 0, 0) * rigidbodyMovementForce); } } else if (rb2D) { // Lose all velocity by pressing '0' if (Input.GetKeyDown(KeyCode.Alpha0)) { rb2D.velocity = Vector3.zero; rb2D.angularVelocity = 0; } // Add forces to the parent rigidbody if (Input.GetKeyDown(KeyCode.DownArrow)) { rb2D.AddForce(new Vector3(0, -1.5f, -1) * rigidbodyMovementForce); } if (Input.GetKeyDown(KeyCode.UpArrow)) { rb2D.AddForce(new Vector3(0, 1.5f, 1) * rigidbodyMovementForce); } if (Input.GetKeyDown(KeyCode.LeftArrow)) { rb2D.AddForce(new Vector3(-1, 0, 0) * rigidbodyMovementForce); } if (Input.GetKeyDown(KeyCode.RightArrow)) { rb2D.AddForce(new Vector3(1, 0, 0) * rigidbodyMovementForce); } } else { // Move the parent transform if (Input.GetKey(KeyCode.DownArrow)) { transform.position = transform.position + new Vector3(0, -1.5f, -1) * speed; } if (Input.GetKey(KeyCode.UpArrow)) { transform.position = transform.position + new Vector3(0, 1.5f, 1) * speed; } if (Input.GetKey(KeyCode.LeftArrow)) { transform.position = transform.position + new Vector3(-1, 0, 0) * speed; } if (Input.GetKey(KeyCode.RightArrow)) { transform.position = transform.position + new Vector3(1, 0, 0) * speed; } } #endregion }