Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Multiplayer Raycasting

Discussion in 'Multiplayer' started by ostrich160, Sep 24, 2014.

  1. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Hi guys, so I have an issue with my player script for multiplayer. Basically, the game works in a way where the gun will can be taken from other players. Now, for some reason, anyone can fire the gun whether they are holding it or not, which I believe has something to do with multiplayer raycasting. I've tried not using RPC, using every RPC function and using every RPC function with a non RPC call (so something like

    if (ThisIs == true){
    networkView.RPC ("Shot", RPCMode.AnyOfTheRPCCallsHere);
    Shot();
    )

    and its still having the same effect. Here is my Player script, the lines you want to look at a 38-40 and then just function Shot

    Code (JavaScript):
    1. #pragma strict
    2. var DeadPlayer : boolean = false;
    3. var GunPrefab : GameObject;
    4. var Holding : boolean = false;
    5. var AllowFire : boolean = true;
    6. var HoldPoint : Transform;
    7. static var HitByPunch : boolean = false;
    8. var GraphicsComp : GameObject;
    9. var Win : boolean = false;
    10.  
    11. function Start () {
    12. GunPrefab = GameObject.FindGameObjectWithTag("Gun");
    13. }
    14.  
    15. function Update () {
    16. if (WinState.Winner == true && DeadPlayer == false){
    17. Win = true;
    18. }
    19.  
    20. if (DeadPlayer == true){
    21. networkView.RPC ("IAmDead", RPCMode.All);
    22. }
    23.  
    24. if (GunPrefab.transform.IsChildOf(HoldPoint)){
    25. Holding = true;
    26. }
    27. else{
    28. Holding = false;
    29. }
    30.  
    31. if (DeadPlayer == true){
    32. Debug.Log("Dead");
    33. }
    34.  
    35. if (Input.GetButtonDown("Punch") && DeadPlayer == false){
    36. networkView.RPC ("Punching", RPCMode.All);
    37. }
    38. if (Input.GetButtonDown("Fire1") && Holding == true && AllowFire == true){
    39. //networkView.RPC ("Shot", RPCMode.Server);
    40. Shot();
    41. }
    42. }
    43.  
    44. @RPC
    45. function Punching(){
    46. var HitRay : RaycastHit;
    47. var PlayRange = transform.TransformDirection(Vector3(0,0,0.001));
    48. if (Physics.SphereCast((transform.position + Vector3.up * 0.75), 1, PlayRange, HitRay)){
    49. HitRay.transform.SendMessage ("HitPunch");
    50. }
    51. }
    52.  
    53. @RPC
    54. function Shot(){
    55. var rayhit : RaycastHit;
    56. var PlayRanger = transform.TransformDirection(Vector3(0,0,10));
    57. if (Physics.SphereCast((transform.position + Vector3.up * 0.75), 1, PlayRanger, rayhit)){
    58. AllowFire = false;
    59. rayhit.transform.SendMessage ("HitByGun");
    60. }
    61. yield WaitForSeconds (5);
    62. AllowFire = true;
    63. }
    64.  
    65. function HitByGun (){
    66. DeadPlayer = true;
    67. }
    68.  
    69. function OnTriggerStay (Gunn : Collider){
    70. if (Gunn.tag == "Gun"){
    71. yield WaitForSeconds (1);
    72. if (ShotgunGen.Held== false && DeadPlayer == false){
    73. networkView.RPC ("GunPickUP", RPCMode.All);
    74. //Holding = true;
    75. }
    76. //if (ShotgunGen.Held== true){
    77. //Debug.Log ("Im Trying to get a gun which is already held");
    78. //}
    79. }
    80. }
    81.  
    82. function OnTriggerExit (Gunn : Collider){
    83. if (Gunn.tag == "Gun"){
    84. //if (ShotgunGen.Held== true){
    85. //Holding = false;
    86. //}
    87. }
    88. }
    89.  
    90. function HitPunch(){
    91. if (Holding == true){
    92. HitByPunch = true;
    93. Holding = false;
    94. yield WaitForSeconds(2);
    95. HitByPunch = false;
    96. }
    97. }
    98.  
    99. @RPC
    100. function GunPickUP(){
    101. GunPrefab.transform.position = HoldPoint.transform.position;
    102. GunPrefab.transform.parent = HoldPoint.transform;
    103. GunPrefab.transform.localEulerAngles = new Vector3 (0,0,0);        //(-18.6, -0.7, -10.2)
    104. }
    105.  
    106. @RPC
    107. function IAmDead(){
    108. Destroy(GraphicsComp);
    109. }
    110.  
    111.  
    Any advice?
     
  2. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,117
    Check your holding logic, probably it will remain true in all because you should set it to true only in your own system, I mean the player who owns the character prefab should set holding to true and others should not do so.
     
  3. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Thats the odd part, I run the game with a client and with unity, hosting the server in play mode, so I can see the values in the inspector. The player without the guy doesnt even have holding set to true, yet they can still shoot anyway
     
  4. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
  5. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Something I should add thats quite peculiar, as this might help
    Before I added

    Code (JavaScript):
    1. if (GunPrefab.transform.IsChildOf(HoldPoint)){
    2. Holding = true;
    3. }
    4. else{
    5. Holding = false;
    6. }
    to the script, the RPC call for Shot worked fine with
    networkView.RPC ("Shot", RPCMode.Server);
    Shot();

    Yeh, both of them. But it worked. However with this update to when it is and isnt holding it, for some reason now it doesnt work
     
  6. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Fixed it! For those who want to know how, I used if(networkView.isMine) to enclose all of update