Advertisement
Guest User

ObjectMovementUpdate

a guest
Apr 27th, 2014
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. // This script is attached to the pickup object. It ensures that the pickup object's
  5. // position, rotation and scale are kept up to date across the network.
  6.  
  7. public class PickupMovementUpdate : MonoBehaviour {
  8.    
  9.     // Declare Variables
  10.     private Vector3 lastPosition;
  11.     private Quaternion lastRotation;
  12.     private Transform myTransform;
  13.    
  14.     public bool amIBeingHeld = false;
  15.     // Declare Variables end
  16.  
  17.     // Use this for initialization
  18.     void Start ()
  19.     {
  20.         myTransform = transform;
  21.        
  22.         // Ensure that everyone sees the player at the correct location the moment they spawn
  23.         //networkView.RPC ("updateMovement", RPCMode.OthersBuffered, myTransform.position, myTransform.rotation, myTransform.localScale);
  24.     }
  25.    
  26.     // Update is called once per frame
  27.     void Update ()
  28.     {
  29.         if (Network.peerType != NetworkPeerType.Disconnected && Network.isClient)
  30.         {
  31.             // If the object has moved at all, fire off an NPC to update the objects position and rotation across the network
  32.             //Debug.Log("the cube's position is: " + myTransform.position);
  33.            
  34.             if (Vector3.Distance (myTransform.position, lastPosition) >= 0.1)
  35.             {
  36.                 // Capture the pickup object's position before the RPC is fired off
  37.                 // Use this to determine if the object has moved in the if statement above
  38.                 lastPosition = myTransform.position;
  39.                 if (amIBeingHeld == false)
  40.                 {
  41.                     networkView.RPC ("updateMovement", RPCMode.AllBuffered, myTransform.position, myTransform.rotation, myTransform.localScale);
  42.                 }
  43.                
  44.                 if (amIBeingHeld == true)
  45.                 {
  46.                    
  47.                 }
  48.                
  49.             }
  50.            
  51.             if (Quaternion.Angle (myTransform.rotation, lastRotation) >= 1)
  52.             {
  53.                 // Capture the pickup object's rotation before the RPC is fired off
  54.                 // Use this to determine if the object has turned in the if statement above
  55.                 lastRotation = myTransform.rotation;
  56.                
  57.                 networkView.RPC ("updateMovement", RPCMode.AllBuffered, myTransform.position, myTransform.rotation, myTransform.localScale);
  58.             }
  59.         }
  60.     }
  61.    
  62.     // This ensures that the pickup object's position, rotation and scale are kept up to date across the network
  63.    
  64.     [RPC]
  65.     void updateMovement (Vector3 newPosition, Quaternion newRotation, Vector3 newScale)
  66.     {
  67.         transform.position = newPosition;
  68.         transform.rotation = newRotation;
  69.         transform.localScale = newScale;
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement