- Home /
Question by
darkcookie · Apr 14, 2012 at 04:02 AM ·
multiplayerupdate
update position of cube
my problem is that I have a cube with this script
var mouseOverColor = Color.blue;
private var originalColor : Color;
function Start () {
originalColor = renderer.sharedMaterial.color; }
function OnMouseEnter () {
renderer.material.color = mouseOverColor;}
function OnMouseExit () { renderer.material.color = originalColor; }
function OnMouseDown () {
var screenSpace = Camera.main.WorldToScreenPoint(transform.position);
var offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
while (Input.GetMouseButton(0)) {
var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset; transform.position = curPosition; yield;
} }
nothing is wrong with it but the problem is that when running multyplayer i want to update the position of the cube per frame ... i tried to use the same update script that I use for the player but it doesn't update per frame. can some one pls help me with updating the cubes position per frame?
player update script:
using UnityEngine;
using System.Collections;
/// <summary>
/// This script is attached to the player and it
/// ensures that every players position, rotation, and scale,
/// are kept up to date across the network.
/// </summary>
public class MovementUpdate : MonoBehaviour {
//Variables Start___________________________________
private Vector3 lastPosition;
private Quaternion lastRotation;
private Transform myTransform;
//Variables End_____________________________________
// Use this for initialization
void Start ()
{
if(networkView.isMine == true)
{
myTransform = transform;
//Ensure taht everyone sees the player at the correct location
//the moment they spawn.
networkView.RPC("updateMovement", RPCMode.OthersBuffered,
myTransform.position, myTransform.rotation);
}
else
{
enabled = false;
}
}
// Update is called once per frame
void Update ()
{
//If the player has moved at all then fire off an RPC to update the players
//position and rotation across the network.
if(Vector3.Distance(myTransform.position, lastPosition) >= 0.1)
{
//Capture the player's position before the RPC is fired off and use this
//to determine if the player has moved in the if statement above.
lastPosition = myTransform.position;
networkView.RPC("updateMovement", RPCMode.OthersBuffered,
myTransform.position, myTransform.rotation);
}
if(Quaternion.Angle(myTransform.rotation, lastRotation) >= 1)
{
//Capture the player's rotation before the RPC is fired off and use this
//to determine if the player has turned in the if statement above.
lastRotation = myTransform.rotation;
networkView.RPC("updateMovement", RPCMode.OthersBuffered,
myTransform.position, myTransform.rotation);
}
}
[RPC]
void updateMovement (Vector3 newPosition, Quaternion newRotation)
{
transform.position = newPosition;
transform.rotation = newRotation;
}
}
Comment