- Home /
Syncing networked units for an online rts trouble.
So I have been working on this co-op online rts using unitys networking system and am having trouble with moving shared units around. The units seem to freak out or stand in different places for each client (and yes I am using a network view).
So simply put here is the code I am using.
For choosing and setting the destination of units:
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour
{
public Transform mover; //the object being moved
public Transform mover2;
// Use this for initialization
// Update is called once per frame
public void Update()
{
if (Input.GetButtonDown("Fire2"))//clears any selected units
{
mover = null;
mover2 = null;
}
//when left mouse button is pressed
if (Input.GetButtonDown("Fire1"))
{
Ray ray = (Camera.main.ScreenPointToRay(Input.mousePosition)); //create the ray
RaycastHit hit; //create the var that will hold the information of where the ray hit
if (Physics.Raycast(ray, out hit)){ //did we hit something?
if (hit.transform.tag == "Player"){
Debug.Log("Select");
mover = hit.transform ; //
mover.transform.GetComponent<Friendly_AI>().IsSelected = true;
}else if (hit.transform.tag == "ground"){ //did we hit the ground?
Debug.Log("Ground hit");
hit.point = hit.point + new Vector3(0,1f,0);
mover.transform.GetComponent<Friendly_AI>().destination = hit.point;
mover = null;
}
}
}
}
}
The units AI:
using UnityEngine;
using System.Collections;
public class Friendly_AI : MonoBehaviour
{
public bool IsSelected = false;
public Vector3 destination = Vector3.zero; //where we want to move
void Awake (){
destination = transform.position; //set the destination to the objects position when the script is run the first time
}
void Update()
{
transform.LookAt(destination);
NetworkViewID viewID = Network.AllocateViewID();
transform.position = Vector3.MoveTowards(transform.position, destination, Time.deltaTime * 3); //move toward destination
networkView.RPC("position", RPCMode.AllBuffered, viewID, destination);
}
[RPC]
void position(NetworkViewID viewID, Vector3 wereto) {
destination = wereto;
}
}
As you can see I tried using an RPC to make sure the positions are always the same. Which worked but is jumping and can take 3-4 tries to get it to move if the current person moving it isn't the same client as last time.
Any help would be appreciated thanks.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
AI Patrolling 1 Answer
Guided missile help c# 1 Answer
constructing buildings rts style. 1 Answer