Search Unity

UNet Control the "same" object?

Discussion in 'UNet' started by Leosubaru1, Nov 29, 2016.

  1. Leosubaru1

    Leosubaru1

    Joined:
    Feb 10, 2013
    Posts:
    29
    Hello & Good evening!

    now I have rethought it.. and my result looks like you can see on the following screenshot:
    Hmm.jpg

    One player is able to control the car and the other can control the tower and shoot. But I think the
    implementation/logic behind sucks :p

    My Idea:
    I have dragged a car model in the scene (just with a rigidbody, box collider, wheel collider etc.).
    And I spawn a player (empty Gameobject with the "CAR1" Script on it.
    Is there a better solution or an advice?

    Thank you :)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.Networking;
    5.  
    6. [System.Serializable]
    7. public class AxleInfo11
    8. {
    9.     public WheelCollider leftWheel;
    10.     public WheelCollider rightWheel;
    11.     public bool motor;
    12.     public bool steering;
    13. }
    14.  
    15. public class CAR1 : NetworkBehaviour
    16. {
    17.     public List<AxleInfo11> axleInfos;
    18.     public float maxMotorTorque;    public float maxSteeringAngle;
    19.  
    20.     private float LerpValue = 2;
    21.     private float MinPosDist = 0.5F;
    22.     public int playerMODE = 0;
    23.  
    24.     private CAR1 carScript;
    25.     GameObject carCRAB;
    26.     private Transform towerObject;
    27.     private Transform cannon;
    28.     private Transform gunTIP;
    29.  
    30.     private Vector3 lastTankpos;        private Quaternion lastTankRot;
    31.  
    32.     //Geschützturm
    33.     private float lsTowerRotationY;   private float LerpTurningTower = 2; private float MinMovementTower = 2F;
    34.     private float lsCannonRotZ;   private float LerpTurningCannon = 2; private float MinMovmentCannon = 2F;
    35.  
    36.     //Motor-Steering
    37.     private float lsTankSteering;         private float LerpTankSteering = 0.5F; private float MinAngleSteering = 2F;
    38.     private float lsTankMotor;            private float LerpTankMotor = 0.5F; private float MinAngleMotor = 2F;
    39.  
    40.     [SyncVar]    private Vector3 cTankPos;
    41.     [SyncVar]    private Quaternion cTankRot;
    42.     [SyncVar]    public float motorVar;
    43.     [SyncVar]    public float steeringVar;
    44.  
    45.     [SyncVar]    public float towerRotationY;
    46.     [SyncVar]    public float cannonRotationZ;
    47.  
    48.     private float rotationZ = 0f;
    49.     private float sensitivityZ = 2f;
    50.  
    51.     private AudioSource shootSound;
    52.     private ParticleSystem particleShootingSmoke;
    53.     void Start()
    54.     {
    55.         //FIND TANK
    56.         carCRAB = GameObject.Find("CarLokal");
    57.         towerObject = GameObject.Find("Drehturm").transform;
    58.         shootSound = towerObject.GetComponent<AudioSource>();
    59.         cannon = GameObject.Find("Drehkanone").transform;
    60.         gunTIP = GameObject.Find("WEAPONTIP").transform;
    61.         particleShootingSmoke = gunTIP.GetChild(0).transform.GetComponent<ParticleSystem>();
    62.         carScript = GetComponent<CAR1>();
    63.              
    64.         carScript.axleInfos[0].leftWheel = carCRAB.transform.GetChild(1).transform.GetComponent<WheelCollider>(); ;
    65.         carScript.axleInfos[0].rightWheel = carCRAB.transform.GetChild(2).transform.GetComponent<WheelCollider>(); ;
    66.         carScript.axleInfos[1].leftWheel = carCRAB.transform.GetChild(3).transform.GetComponent<WheelCollider>(); ;
    67.         carScript.axleInfos[1].rightWheel = carCRAB.transform.GetChild(4).transform.GetComponent<WheelCollider>(); ;
    68.      
    69.         //tank won't flip that easy
    70.         Rigidbody rb = carCRAB.GetComponent<Rigidbody>();
    71.         rb.centerOfMass = new Vector3(0, -0.4F, 0);
    72.  
    73.  
    74.         playerMODE = GameObject.Find("NETWORKMANAGER").GetComponent<CurrentPos>().playerMODE;
    75.  
    76.         if (playerMODE == 1)
    77.         {
    78.             rb.isKinematic = true;
    79.         }
    80.   }
    81.     void Update()
    82.     {
    83.         if(playerMODE==1)
    84.         {
    85.             if (Input.GetButtonDown("Fire1"))
    86.             {
    87.                 StopCoroutine("Shoot");
    88.                 StartCoroutine("Shoot");
    89.             }
    90.         }
    91.     }
    92.  
    93.     // example unity
    94.     public void ApplyLocalPositionToVisuals(WheelCollider collider)
    95.     {
    96.         if (collider.transform.childCount == 0)
    97.         {
    98.             return;
    99.         }
    100.  
    101.         Transform visualWheel = collider.transform.GetChild(0);
    102.  
    103.         Vector3 position;
    104.         Quaternion rotation;
    105.         collider.GetWorldPose(out position, out rotation);
    106.  
    107.         visualWheel.transform.position = position;
    108.         visualWheel.transform.rotation = rotation;
    109.     }
    110.     private void ChangeAxisValues(float steering1, float motor1)
    111.     {
    112.         foreach (AxleInfo11 axleInfo in axleInfos)
    113.         {
    114.             if (axleInfo.steering)
    115.             {
    116.                 axleInfo.leftWheel.steerAngle = steering1;
    117.                 axleInfo.rightWheel.steerAngle = steering1;
    118.             }
    119.             if (axleInfo.motor)
    120.             {
    121.                 axleInfo.leftWheel.motorTorque = motor1;
    122.                 axleInfo.rightWheel.motorTorque = motor1;
    123.             }
    124.             ApplyLocalPositionToVisuals(axleInfo.leftWheel);
    125.             ApplyLocalPositionToVisuals(axleInfo.rightWheel);
    126.         }
    127.     }
    128.  
    129.  
    130.     public void FixedUpdate()
    131.     {
    132.         //MODE == DRIVER
    133.         if (playerMODE == 0)
    134.         {
    135.             float motor = maxMotorTorque * Input.GetAxis("Vertical");
    136.             float steering = maxSteeringAngle * Input.GetAxis("Horizontal");
    137.             ChangeAxisValues(steering, motor);
    138.  
    139.  
    140.             Vector3 currPos = carCRAB.transform.position;
    141.             ChangeCarPosition(currPos);
    142.  
    143.             Quaternion currRot = carCRAB.transform.rotation;
    144.             ChangeCarRotation(currRot);
    145.  
    146.             if (isOverThreshold(lastTankpos, currPos, MinPosDist))
    147.             {
    148.                 CmdSendPosition(currPos);
    149.                 lastTankpos = currPos;
    150.                 CmdSendRotation(currRot);
    151.                 lastTankRot = currRot;
    152.  
    153.                 CmdSendMotor(motor);
    154.                 lsTankMotor = motor;
    155.             }
    156.  
    157.             //TOWER
    158.             InterpolateTurretY();
    159.             InterpolateKanoneZ();
    160.      
    161.          
    162.             if (isOverThresholdFloat(lsTankSteering, steering, MinAngleSteering))
    163.             {
    164.                 CmdSendSteering(steering);
    165.                 lsTankSteering = steering;
    166.             }
    167.          
    168.         }
    169.         else         //MODE == SHOOTER
    170.         {
    171.             Interpolate();
    172.             InterpolateRot();
    173.                  
    174.             ChangeAxisValues(InterpolateSteering(), motorVar);
    175.  
    176.             //ROTATION WHOLE TOWER
    177.             towerObject.transform.Rotate(0.0f, Input.GetAxis("Horizontal") * 2, 0.0f);
    178.             if (isOverThresholdTurret(lsTowerRotationY, towerObject.transform.localRotation.y, MinMovementTower))
    179.             {
    180.                 CmdSendTowerY(towerObject.transform.localEulerAngles.y);
    181.                 lsTowerRotationY = towerObject.transform.localEulerAngles.y;
    182.             }
    183.  
    184.             //TOTATE CANNON
    185.             rotationZ += Input.GetAxis("Vertical") * sensitivityZ;
    186.             rotationZ = Mathf.Clamp(rotationZ, -20, 90);
    187.             cannon.transform.localEulerAngles = new Vector3(cannon.transform.localEulerAngles.x, cannon.transform.localEulerAngles.y, -rotationZ);
    188.             if (isOverThresholdDrehKanone(lsCannonRotZ, cannon.transform.localEulerAngles.z, MinMovmentCannon))
    189.             {
    190.                 CmdSendDrehKanoneZ(cannon.transform.localEulerAngles.z);
    191.                 lsCannonRotZ = cannon.transform.localEulerAngles.z;
    192.             }          
    193.         }  
    194.     }
    195.     //CAR POSITION
    196.     [Command]
    197.     private void CmdSendPosition(Vector3 pos)
    198.     {
    199.         cTankPos = pos;
    200.     }
    201.     [Client]
    202.     private void ChangeCarPosition(Vector3 pos)
    203.     {
    204.         carCRAB.transform.position = pos;
    205.     }
    206.  
    207.     [Command]
    208.     private void CmdSendMotor(float motor)
    209.     {
    210.         motorVar = motor;
    211.     }
    212.     [Command]
    213.     private void CmdSendSteering(float steering)
    214.     {
    215.         steeringVar = steering;
    216.     }
    217.  
    218.     //CAR ROTATION
    219.     [Command]
    220.     private void CmdSendRotation(Quaternion rot)
    221.     {
    222.         cTankRot = rot;
    223.     }
    224.     [Client]
    225.     private void ChangeCarRotation(Quaternion rot)
    226.     {
    227.         carCRAB.transform.rotation = rot;
    228.     }
    229.  
    230.  
    231.     //TOWER
    232.     [Command]
    233.     private void CmdSendTowerY(float rotY)
    234.     {
    235.         lsTowerRotationY = rotY;
    236.     }
    237.     [Command]
    238.     private void CmdSendDrehKanoneZ(float rotZ)
    239.     {
    240.         lsCannonRotZ = rotZ;
    241.     }
    242.  
    243.  
    244.  
    245.     //INTERPOLATION STUFF --> simplify later on
    246.     private void Interpolate()
    247.     {
    248.         Vector3 pos = Vector3.Lerp(carCRAB.transform.position, cTankPos, Time.deltaTime * LerpValue);
    249.         carCRAB.transform.position = pos;
    250.     }
    251.  
    252.     private void InterpolateRot()
    253.     {
    254.         Quaternion rot = Quaternion.Lerp(carCRAB.transform.rotation, cTankRot, Time.deltaTime * LerpValue);
    255.         carCRAB.transform.rotation = rot;
    256.     }
    257.  
    258.     private void InterpolateTurretY()
    259.     {
    260.         towerObject.transform.localEulerAngles = new Vector3(towerObject.transform.localEulerAngles.x, Mathf.Lerp(towerObject.transform.localEulerAngles.y, lsTowerRotationY, LerpTurningTower), towerObject.transform.localEulerAngles.z);
    261.     }
    262.  
    263.     private void InterpolateKanoneZ()
    264.     {
    265.         cannon.transform.localEulerAngles = new Vector3(cannon.transform.localEulerAngles.x, cannon.transform.localEulerAngles.y, Mathf.Lerp(cannon.transform.localEulerAngles.y, lsCannonRotZ, LerpTurningCannon));
    266.     }
    267.  
    268.     private float InterpolateSteering()
    269.     {
    270.         return Mathf.Lerp(lsTankSteering, steeringVar, LerpTankSteering);
    271.     }
    272.     private float InterpolateMotor()
    273.     {
    274.         return Mathf.Lerp(lsTankMotor, motorVar, LerpTankMotor);
    275.     }
    276.  
    277.     private bool isOverThreshold(Vector3 old, Vector3 pos, float threshold)
    278.     {
    279.         return Vector3.Distance(old, pos) > threshold;
    280.     }
    281.  
    282.     private bool isOverThresholdTurret(float old, float pos, float angleThreshold)
    283.     {
    284.         return (old - pos) > angleThreshold || (old - pos) < angleThreshold;
    285.     }
    286.  
    287.     private bool isOverThresholdDrehKanone(float old, float pos, float angleThreshold)
    288.     {
    289.         return (old - pos) > angleThreshold || (old - pos) < angleThreshold;
    290.     }
    291.  
    292.  
    293.     private bool isOverThresholdFloat(float old, float pos, float angleThreshold)
    294.     {
    295.         return (old - pos) > angleThreshold || (old - pos) < angleThreshold;
    296.     }
    297.  
    298.  
    299.  
    300.     IEnumerator Shoot()
    301.     {
    302.         while (Input.GetButton("Fire1"))
    303.         {
    304.             CmdShoot();
    305.  
    306.             shootSound.Stop();
    307.             shootSound.Play();
    308.             particleShootingSmoke.Play();
    309.  
    310.             Vector3 fwd = gunTIP.transform.TransformDirection(Vector3.forward);
    311.             Debug.DrawRay(gunTIP.transform.position, fwd * 50, Color.green);
    312.             RaycastHit hit;
    313.  
    314.             if (Physics.Raycast(gunTIP.transform.position, fwd, out hit, 100))
    315.             {
    316.                 Debug.DrawLine(gunTIP.transform.position, hit.point, Color.red);
    317.  
    318.                 if (hit.transform.tag == "ENEMY")
    319.                 {
    320.                     Debug.Log("TEST");
    321.                 }
    322.             }
    323.             particleShootingSmoke.Stop();
    324.             yield return new WaitForSeconds(0.1F);
    325.         }
    326.     }
    327.     [Command]
    328.     public void CmdShoot()
    329.     {
    330.         shootSound.Stop();
    331.         shootSound.Play();
    332.         particleShootingSmoke.Play();
    333.  
    334.         Vector3 fwd = gunTIP.transform.TransformDirection(Vector3.forward);
    335.         Debug.DrawRay(gunTIP.transform.position, fwd * 50, Color.green);
    336.         RaycastHit hit;
    337.  
    338.         if (Physics.Raycast(gunTIP.transform.position, fwd, out hit, 100))
    339.         {
    340.             Debug.DrawLine(gunTIP.transform.position, hit.point, Color.red);
    341.  
    342.             if (hit.transform.tag == "ENEMY")
    343.             {
    344.                 Debug.Log("TEST");
    345.             }
    346.         }
    347.         particleShootingSmoke.Stop();
    348.     }
    349. }


    firstly I've found similar threads but unfortunately I can't really solve my problem :(
    I've tried several things but I'm.... stuck.

    Idea:
    I want the user to join a room and then walk to different vehicles and control them. Further more I want that player1 controls the car movement and player2 the rotation of the turret (or is able to control the radio etc..).
    Simplification:
    Empty Gameobject Player spawns:
    currentmode =0 --> Player can control the car
    currentmode =1 --> Player can control the turret

    Problem.
    I'm not sure about the logic behind the scenario.
    1. My first thought was to create 1 car in my scene add a network identity, check Local Player Authority, add a network transform to that car. And when the game starts each player can control this 1 car.

    2. Or do I instantiate for all player in that car 1 car object and disable the visibility and then sync.
    Player 1 --> syncs turret rotation/ receives car position
    Player 2 --> syncs car movement/ receives turret rotation


    Hmm appreciate some hints/help :)

    Short:
    I want two ore more player to control different car gadgets.


    And for those who aren't confused yet...: ;)
    ____________________
    My plan:
    1. Empty Gameobject (Network Identity -> local player authority checked) assigned to the Network Manager Script

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class PlayerController : NetworkBehaviour {
    6.     GameObject car;
    7.     int currentmode = 0;
    8.     public GUISkin guistyle;
    9.     private CarController carcontroller;
    10.     private CarShooter carShooter;
    11.     void Start()
    12.     {
    13.         car = GameObject.Find("CarLokal");
    14.         carcontroller = car.GetComponent<CarController>();
    15.         carShooter = car.GetComponent<CarShooter>();
    16.     }
    17.  
    18.     void OnGUI()
    19.     {
    20.         if(!isLocalPlayer || isServer)
    21.         {
    22.             return;
    23.         }
    24.         GUI.skin = guistyle;
    25.         GUI.color = Color.red;
    26.         GUILayout.BeginArea(new Rect(Screen.width / 2 - 200, Screen.height - 100, 400, 400));
    27.         if (GUILayout.Button("Mode1"))
    28.         {
    29.             currentmode = 0;
    30.         }
    31.         if (GUILayout.Button("Mode2"))
    32.         {
    33.             currentmode = 1;
    34.         }
    35.         GUILayout.Label("MODE: " + currentmode);
    36.         GUILayout.EndArea();
    37.     }
    38.  
    39.     void Update () {
    40.         if (!isLocalPlayer)
    41.         {
    42.             return;
    43.         }
    44.  
    45.         if(currentmode==0)
    46.         {
    47.             if(carcontroller !=null)
    48.             {
    49.                     carcontroller.GiveVal(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"));
    50.                 //  carcontroller.RpcGiveVal(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"));
    51.                 //  carcontroller.CmdGiveVal(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"));
    52.             }
    53.         }
    54.         else
    55.         {
    56.             if(carShooter !=null)
    57.             {
    58.                 carShooter.GiveVal(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"));
    59.              // carShooter.RpcGiveVal(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"));
    60.              //  carShooter.CmdGiveVal(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"));
    61.             }
    62.         }
    63.     }
    64. }
    65.  
    ___________________________

    Car placed in the scene (Network Identity-> Local Player Authority checked + Car Shooter (Script) + Network Transform?!)

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine.Networking;
    6.  
    7. [System.Serializable]
    8. public class AxleInfo2
    9. {
    10.     public WheelCollider leftWheel;
    11.     public WheelCollider rightWheel;
    12.     public bool motor;
    13.     public bool steering;
    14. }
    15.  
    16. public class CarController : NetworkBehaviour {
    17.  
    18.     public List<AxleInfo2> axleInfos;
    19.     public float maxMotorTorque;
    20.     public float maxSteeringAngle;
    21.  
    22.     private PlayerCamera playerCamera;
    23.  
    24.     void Start()
    25.     {
    26.         Rigidbody rb = GetComponent<Rigidbody>();
    27.         rb.centerOfMass = new Vector3(0, -0.4F, 0);
    28.     }
    29.  
    30.  
    31.     // finds the corresponding visual wheel
    32.     // correctly applies the transform
    33.     public void ApplyLocalPositionToVisuals(WheelCollider collider)
    34.     {
    35.         if (collider.transform.childCount == 0)
    36.         {
    37.             return;
    38.         }
    39.  
    40.         Transform visualWheel = collider.transform.GetChild(0);
    41.  
    42.         Vector3 position;
    43.         Quaternion rotation;
    44.         collider.GetWorldPose(out position, out rotation);
    45.  
    46.         visualWheel.transform.position = position;
    47.         visualWheel.transform.rotation = rotation;
    48.     }
    49.  
    50.  
    51.     [SyncVar]
    52.     public float currentVert=0;
    53.  
    54.     [SyncVar]
    55.     public float currentHori=0;
    56.  
    57.  
    58.     //Now let's start the confusion party :)
    59.     [Command]
    60.     public void CmdGiveVal(float currentVert, float currentHori)
    61.     {
    62.         this.currentVert = currentVert;
    63.         this.currentHori = currentHori;
    64.     }
    65.  
    66.     public void GiveVal(float currentVert, float currentHori)
    67.     {
    68.         this.currentVert = currentVert;
    69.         this.currentHori = currentHori;
    70.     }
    71.  
    72.       [ClientRpc]
    73.       public void RpcGiveVal(float currentVert, float currentHori)
    74.       {
    75.           this.currentVert = currentVert;
    76.           this.currentHori = currentHori;
    77.       }
    78.  
    79.     public void FixedUpdate()
    80.     {
    81.  
    82.         //   if (!isServer)
    83.         //    {
    84.         //        RpcSpreadVal(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"));
    85.         //   }
    86.  
    87.         //  if (isLocalPlayer)
    88.         //   {
    89.         //       RpcGiveVal(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"));
    90.         //     }
    91.  
    92.  
    93.   //      if(!isLocalPlayer)
    94.    //     {
    95.     //        return;
    96.   //      }
    97.    //     GiveVal(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"));
    98.  
    99.  
    100.         float motor = maxMotorTorque * currentVert;
    101.         float steering = maxSteeringAngle * currentHori;
    102.  
    103.         foreach (AxleInfo2 axleInfo in axleInfos)
    104.         {
    105.             if (axleInfo.steering)
    106.             {
    107.                 axleInfo.leftWheel.steerAngle = steering;
    108.                 axleInfo.rightWheel.steerAngle = steering;
    109.             }
    110.             if (axleInfo.motor)
    111.             {
    112.                 axleInfo.leftWheel.motorTorque = motor;
    113.                 axleInfo.rightWheel.motorTorque = motor;
    114.             }
    115.             ApplyLocalPositionToVisuals(axleInfo.leftWheel);
    116.             ApplyLocalPositionToVisuals(axleInfo.rightWheel);
    117.         }
    118.     }
    119.  
    120. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4. public class CarShooter : NetworkBehaviour {
    5.     public Transform tower;
    6.     public Transform towerBarrel;
    7.     [SyncVar]
    8.     public float currentVert = 0;
    9.  
    10.     [SyncVar]
    11.     public float currentHori = 0;
    12.  
    13.     [Command]
    14.     public void CmdGiveVal(float currentVert, float currentHori)
    15.     {
    16.         this.currentVert = currentVert;
    17.         this.currentHori = currentHori;
    18.     }
    19.     // Use this for initialization
    20.     void Start () {
    21.  
    22.     }
    23.  
    24.     float speed = 4;
    25.     [ClientRpc]
    26.     public void RpcGiveVal(float currentVert, float currentHori)
    27.     {
    28.         this.currentVert = currentVert;
    29.         this.currentHori = currentHori;
    30.     }
    31.     public void GiveVal(float currentVert, float currentHori)
    32.     {
    33.         this.currentVert = currentVert;
    34.         this.currentHori = currentHori;
    35.     }
    36.  
    37.  
    38.     private float rotationZ = 0f;
    39.     private float sensitivityZ = 2f;
    40.  
    41.  
    42.     // Update is called once per frame
    43.     void Update () {
    44.  
    45.      //   GiveVal(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"));
    46.  
    47.      //   if(isLocalPlayer)
    48.     //    {
    49.      //       RpcGiveVal(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"));
    50.      //   }
    51.  
    52.         //  if (!isServer)
    53.         //  {
    54.         //       RpcSpreadVal(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"));
    55.         //   }
    56.  
    57.         tower.transform.Rotate(0.0f, currentHori * speed, 0.0f);
    58.  
    59.         //  towerBarrel.transform.Rotate(0.0f, -Input.GetAxis("V") * speed, 0.0f);
    60.  
    61.  
    62.         //  towerBarrel.transform.Rotate(0, Input.GetAxis("Vertical") * speed * Time.deltaTime, 0);
    63.         //The function is Mathf.Clamp(value, min, max)
    64.         rotationZ += currentVert * sensitivityZ;
    65.         rotationZ = Mathf.Clamp(rotationZ, -20, 90);
    66.  
    67.         towerBarrel.transform.localEulerAngles = new Vector3(towerBarrel.transform.localEulerAngles.x, towerBarrel.transform.localEulerAngles.y, -rotationZ);
    68.  
    69.         /* if (Input.GetKey(KeyCode.A))
    70.          {
    71.              transform.Rotate(Vector3.up * speed * Time.deltaTime);
    72.          }
    73.  
    74.          if (Input.GetKey(KeyCode.D))
    75.                  transform.Rotate(-Vector3.up * speed * Time.deltaTime);
    76.  
    77.          */
    78.     }
    79. }
    80.  


    mop.jpg



    Regards Leo
     
    Last edited: Dec 2, 2016
  2. Leosubaru1

    Leosubaru1

    Joined:
    Feb 10, 2013
    Posts:
    29
    Hmm ok now I have an idea :)
    Here's a small script for changing the color of one cube (every player in the scene can change it)
    (This is the same idea/just for movement right?) - Or is this script terrible (insecure?)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class PlayerColor : NetworkBehaviour {
    6.     GameObject cubeObj;
    7.     private MeshRenderer meshRend;
    8.  
    9.     void Start () {
    10.         cubeObj = GameObject.Find("Cube");
    11.         meshRend = cubeObj.GetComponent<MeshRenderer>();
    12.     }
    13.    
    14.     void Update () {
    15.         if (isLocalPlayer)
    16.         {
    17.             if(Input.GetButtonDown("Fire1"))
    18.             {
    19.                 Color32 colornew = GenColor();
    20.                 ChangeMaterialColor(colornew);
    21.                 CmdSendColor(colornew);
    22.             }
    23.         }
    24.     }
    25.  
    26.     [Command]
    27.     private void CmdSendColor(Color32 materialColor)
    28.     {
    29.         meshRend.material.color = materialColor;
    30.         RpcSyncMaterialcolor(materialColor);
    31.     }
    32.  
    33.     Color32 GenColor()
    34.     {
    35.         return new Color32((byte)Random.Range(0, 255), (byte)Random.Range(0, 255), (byte)Random.Range(0, 255), 255); ;
    36.     }
    37.  
    38.     [ClientRpc]
    39.     private void RpcSyncMaterialcolor(Color32 materialColor)
    40.     {
    41.         ChangeMaterialColor(materialColor);
    42.     }
    43.  
    44.     [Client]
    45.     private void ChangeMaterialColor(Color32 materialColor)
    46.     {
    47.         meshRend.material.color = materialColor;
    48.     }
    49. }
    50.  
     
  3. Leosubaru1

    Leosubaru1

    Joined:
    Feb 10, 2013
    Posts:
    29
    Sry for the push :) I've edited the main thread and tried to make it more specific and clear.