Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by johny · Sep 21, 2012 at 02:03 PM · c#positionnetworkrtsco-op

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges