- Home /
Getting rotation to work on client
Hey guys,Ive created a turret script which basically allows the player to press the up and down arrow. The turret its self is controlled by an empty game object. Ive got that working locally but how do i get it to work on client side. My tutor told me it was similar to the way the position works. like this:
void Update ()
{
if(Input.GetButtonDown("Right"))
{
transform.position = new Vector3(transform.position.x + 1.0f, transform.position.y,transform.position.z);
}
else if(Input.GetButtonDown("Left"))
{
transform.position = new Vector3(transform.position.x -1.0f, transform.position.y,transform.position.z);
}
if(Vector3.Distance(transform.position,lastPosition) > 0.05);
{
networkView.RPC("Log", RPCMode.Server, "Client " + owner + " is moving");
lastPosition = transform.position;
networkView.RPC("SetPosition", RPCMode.Others, gameObject.name, transform.position);
}
}
[RPC]
public void SetPosition(string shipName, Vector3 newPosition)
{
// find the local version of the ship
GameObject ship = GameObject.Find(shipName);
networkView.RPC("Log", RPCMode.Server, "Updating position of " + ship.name + " to " + newPosition);
ship.transform.position = newPosition;
}
how much different is to find the rotation of my turretscript.?
this is my turret script:
using UnityEngine;
using System.Collections;
public class TurretControl : MonoBehaviour
{
public NetworkPlayer owner;
public float minForce, maxForce, forceIncrement;
public float force;
private int speed = 1;
private float maxRotationZ;
private float minRotationZ;
void Awake()
{
enabled = false;
}
void Start()
{
minForce = 100;
maxForce = 5000;
forceIncrement = maxForce * 0.05f;
force = (maxForce - minForce) / 2.0f;
maxRotationZ = 0.5f;
minRotationZ = -0.1f;
}
void Update ()
{
if(Input.GetKey(KeyCode.UpArrow))
{
if (transform.rotation.z <= maxRotationZ)
{
transform.Rotate(0, 0, Input.GetAxis("Vertical") * speed);
}
}
if(Input.GetKey(KeyCode.DownArrow))
{
if (transform.rotation.z >= minRotationZ)
{
transform.Rotate(0, 0, Input.GetAxis("Vertical") * speed);
}
}
if(Input.GetKey(KeyCode.A))
{
force += (forceIncrement * Time.deltaTime);
if (force > maxForce) force = maxForce;
}
else if(Input.GetKey(KeyCode.S))
{
force -= (forceIncrement * Time.deltaTime);
if (force < minForce) force = minForce;
}
}
public float getForce()
{
return force;
}
}
using UnityEngine;
using System.Collections;
public class ShipController : $$anonymous$$onoBehaviour
{
private Vector3 lastPosition;
private Quaternion lastRotation;
}
void Start ()
{
currentAmmo = 1;
lastPosition = transform.position;
lastRotation = transform.rotation;
}
// Update is called once per frame
void Update ()
{
if (game.running && Network.player == owner)
{
if(Input.GetButtonDown("Right"))
{
transform.position = new Vector3(transform.position.x + 1.0f, transform.position.y,transform.position.z);
}
else if(Input.GetButtonDown("Left"))
{
transform.position = new Vector3(transform.position.x -1.0f, transform.position.y,transform.position.z);
}
//Call SetPosition to notifty the client is moving
if(Vector3.Distance(transform.position,lastPosition) <= 1)
{
networkView.RPC("Log", RPC$$anonymous$$ode.Server, "Client " + owner + " is moving");
lastPosition = transform.position;
networkView.RPC("SetPosition", RPC$$anonymous$$ode.Others, gameObject.name, transform.position);
}
if(Quaternion.Angle(transform.rotation,lastRotation)>= -100)
{
lastRotation = transform.rotation;
networkView.RPC("SetRotation", RPC$$anonymous$$ode.Others,gameObject.name,transform.rotation);
}
[RPC]
public void SetPosition(string shipName, Vector3 newPosition)
{
// find the local version of the ship
GameObject ship = GameObject.Find(shipName);
networkView.RPC("Log", RPC$$anonymous$$ode.Server, "Updating position of " + ship.name + " to " + newPosition);
ship.transform.position = newPosition;
}
[RPC]
public void SetRotation(string TurretPivotControl, Quaternion newRotation)
{
//find the local version of the turretpivotControl
Transform turret = transform.Find("TurretPivotControl");
networkView.RPC("Log", RPC$$anonymous$$ode.Server, "Updating rotation of " + turret.name + " to " + newRotation);
transform.rotation= newRotation;
}
the position works fine.but the turret is not updating. not sure what im missing
Please do not bump unless it's been at least a week or two. It's considered spam and you will get flagged for it :)
Sorry, but im really stuck and have researched on how to fix the problem. Im getting closer. its just frustrating looking at code for hours.
Answer by Mint92 · Apr 27, 2014 at 12:08 PM
Okay, i found out that i needed to have a Network Component attached to the turret as well. which is updating both on server and client.