Search Unity

Synchronizing game object through OntriggerEnter

Discussion in 'Multiplayer' started by Resilo, May 26, 2017.

  1. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    Basically after many failed attempts im wondering how i can synchronize a game object in this case enemy to the server through
    OnTriggerEnter?

    or can you suggest another method for activating the attack through a collider enter?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6.  
    7. public class Gettindamaged : NetworkBehaviour
    8. {
    9.     public GameObject player;
    10.     public bool candamage;
    11.     public GameObject targeting;
    12.     public GameObject enemy;
    13.     public bool noshield;
    14.     public Animator anim;
    15.     public Animator playeranim;
    16.  
    17.  
    18.     void OnTriggerEnter(Collider other)
    19.  
    20.     {
    21.  
    22.  
    23.  
    24.  
    25.             if (candamage == true && other.gameObject.tag.Contains("Enemy") && noshield == false)
    26.             {
    27.             enemy = other.gameObject;
    28.             anim = other.gameObject.GetComponent<Animator>();
    29.             Debug.Log("damage");
    30.                 Debug.Log(other.gameObject);
    31.                 //defemce
    32.                 if (candamage == true && player.GetComponent<Attackdirection>().attacknumber == other.gameObject.GetComponent<AIattackdirection>().attacknumber && other.GetComponent<Stats>().sword.GetComponent<AIdoesdamage>().noshield == false)
    33.                 {
    34.  
    35.                     Debug.Log("defence");
    36.                     if (player.GetComponent<Attackdirection>().attackingleft == true && other.gameObject.GetComponent<AIattackdirection>().attackingleft == true)
    37.                     {
    38.                         anim.SetTrigger("blockedleft");
    39.                         playeranim.SetTrigger("parriedleft");
    40.  
    41.                         other.GetComponent<Stats>().shield.GetComponent<Stats>().takingdamage(1);
    42.  
    43.                     }
    44.                     if (player.GetComponent<Attackdirection>().attackingup == true && other.gameObject.GetComponent<AIattackdirection>().attackingup == true)
    45.                     {
    46.                         anim.SetTrigger("blockedup");
    47.                         playeranim.SetTrigger("parriedup");
    48.  
    49.                         other.GetComponent<Stats>().shield.GetComponent<Stats>().takingdamage(1);
    50.  
    51.  
    52.                     }
    53.                     if (player.GetComponent<Attackdirection>().attackingright == true && other.gameObject.GetComponent<AIattackdirection>().attackingright == true)
    54.                     {
    55.                         anim.SetTrigger("blockright");
    56.                         playeranim.SetTrigger("parriedright");
    57.  
    58.                         other.GetComponent<Stats>().shield.GetComponent<Stats>().takingdamage(1);
    59.  
    60.  
    61.                     }
    62.                     if (player.GetComponent<Attackdirection>().attackingdown == true && other.gameObject.GetComponent<AIattackdirection>().attackingdown == true)
    63.                     {
    64.                         anim.SetTrigger("blockedown");
    65.                         playeranim.SetTrigger("parrieddown");
    66.  
    67.                         other.GetComponent<Stats>().shield.GetComponent<Stats>().takingdamage(1);
    68.  
    69.  
    70.                     }
    71.  
    72.  
    73.                 }
    74.  
    75.  
    76.  
    77.                 //attack
    78.                 if (candamage == true && player.GetComponent<Attackdirection>().attacknumber != other.gameObject.GetComponent<AIattackdirection>().attacknumber || other.GetComponent<Stats>().sword.GetComponent<AIdoesdamage>().noshield == true && candamage == true)
    79.                 {
    80.                     Debug.Log("attack");
    81.                     if (player.GetComponent<Attackdirection>().attackingleft == true && other.gameObject.GetComponent<AIattackdirection>().attackingleft == false || other.GetComponent<Stats>().sword.GetComponent<AIdoesdamage>().noshield == true)
    82.                     {
    83.                     anim.SetTrigger("gethit");
    84.                     enemy.GetComponent<Stats>().takingdamage(player.GetComponent<Stats>().damage);
    85.  
    86.  
    87.                 }
    88.                     if (player.GetComponent<Attackdirection>().attackingup == true && other.gameObject.GetComponent<AIattackdirection>().attackingup == false || other.GetComponent<Stats>().sword.GetComponent<AIdoesdamage>().noshield == true)
    89.                     {
    90.                     anim.SetTrigger("gethit");
    91.                     enemy.GetComponent<Stats>().takingdamage(player.GetComponent<Stats>().damage);
    92.  
    93.                 }
    94.                     if (player.GetComponent<Attackdirection>().attackingright == true && other.gameObject.GetComponent<AIattackdirection>().attackingright == false || other.GetComponent<Stats>().sword.GetComponent<AIdoesdamage>().noshield == true)
    95.                     {
    96.                     anim.SetTrigger("gethit");
    97.                     enemy.GetComponent<Stats>().takingdamage(player.GetComponent<Stats>().damage);
    98.  
    99.                 }
    100.                     if (player.GetComponent<Attackdirection>().attackingdown == true && other.gameObject.GetComponent<AIattackdirection>().attackingdown == false || other.GetComponent<Stats>().sword.GetComponent<AIdoesdamage>().noshield == true)
    101.                     {
    102.                     anim.SetTrigger("gethit");
    103.                     enemy.GetComponent<Stats>().takingdamage(player.GetComponent<Stats>().damage);
    104.  
    105.                 }
    106.                 }
    107.  
    108.                 candamage = false;
    109.             }
    110.  
    111.  
    112.  
    113.  
    114.  
    115.  
    116.             //player
    117.             if (candamage == true && other.gameObject.tag.Contains("player") && other.gameObject != player.gameObject && other.GetComponent<Stats>().sword.GetComponent<Gettindamaged>().noshield == false)
    118.             {
    119.             enemy = other.gameObject;
    120.             anim = other.gameObject.GetComponent<Animator>();
    121.             Debug.Log("damag1e");
    122.  
    123.                 if (candamage == true && player.GetComponent<Attackdirection>().attacknumber == other.gameObject.GetComponent<Attackdirection>().attacknumber)
    124.                 {
    125.  
    126.                     if (player.GetComponent<Attackdirection>().attackingleft == true && other.gameObject.GetComponent<Attackdirection>().attackingleft == true)
    127.                     {
    128.                         anim.SetTrigger("blockedleft");
    129.                         playeranim.SetTrigger("parriedleft");
    130.                     candamage = false;
    131.                     other.GetComponent<Stats>().shield.GetComponent<Stats>().takingdamage(1);
    132.  
    133.                     }
    134.                     if (player.GetComponent<Attackdirection>().attackingup == true && other.gameObject.GetComponent<Attackdirection>().attackingup == true)
    135.                     {
    136.                         anim.SetTrigger("blockedup");
    137.                         playeranim.SetTrigger("parriedup");
    138.  
    139.                         other.GetComponent<Stats>().shield.GetComponent<Stats>().takingdamage(1);
    140.  
    141.  
    142.                     }
    143.                     if (player.GetComponent<Attackdirection>().attackingright == true && other.gameObject.GetComponent<Attackdirection>().attackingright == true)
    144.                     {
    145.                         anim.SetTrigger("blockedright");
    146.                         playeranim.SetTrigger("parriedright");
    147.                     candamage = false;
    148.                     other.GetComponent<Stats>().shield.GetComponent<Stats>().takingdamage(1);
    149.  
    150.  
    151.                     }
    152.                     if (player.GetComponent<Attackdirection>().attackingdown == true && other.GetComponent<Attackdirection>().attackingdown == true)
    153.                     {
    154.                         anim.SetTrigger("blockeddown");
    155.                         playeranim.SetTrigger("parrieddown");
    156.                     candamage = false;
    157.                     other.GetComponent<Stats>().shield.GetComponent<Stats>().takingdamage(1);
    158.  
    159.  
    160.                     }
    161.                 }
    162.  
    163.  
    164.                 if (candamage == true && player.GetComponent<Attackdirection>().attacknumber != other.gameObject.GetComponent<Attackdirection>().attacknumber  || other.GetComponent<Stats>().sword.GetComponent<Gettindamaged>().noshield == true && candamage == true)
    165.                 {
    166.                     if (player.GetComponent<Attackdirection>().attackingleft == true && other.gameObject.GetComponent<Attackdirection>().attackingleft == false)
    167.                     {
    168.  
    169.                     anim.SetTrigger("gethit");
    170.                     enemy.GetComponent<Stats>().takingdamage(player.GetComponent<Stats>().damage);
    171.                     candamage = false;
    172.  
    173.  
    174.                 }
    175.                     if (player.GetComponent<Attackdirection>().attackingup == true && other.gameObject.GetComponent<Attackdirection>().attackingup == false || other.GetComponent<Stats>().sword.GetComponent<Gettindamaged>().noshield == true)
    176.                     {
    177.                     anim.SetTrigger("gethit");
    178.                     enemy.GetComponent<Stats>().takingdamage(player.GetComponent<Stats>().damage);
    179.                     candamage = false;
    180.  
    181.                 }
    182.                     if (player.GetComponent<Attackdirection>().attackingright == true && other.gameObject.GetComponent<Attackdirection>().attackingright == false || other.GetComponent<Stats>().sword.GetComponent<Gettindamaged>().noshield == true)
    183.                     {
    184.                     anim.SetTrigger("gethit");
    185.                     enemy.GetComponent<Stats>().takingdamage(player.GetComponent<Stats>().damage);
    186.                     candamage = false;
    187.  
    188.                 }
    189.                     if (player.GetComponent<Attackdirection>().attackingdown == true && other.gameObject.GetComponent<Attackdirection>().attackingdown == false || other.GetComponent<Stats>().sword.GetComponent<Gettindamaged>().noshield == true)
    190.                     {
    191.                     anim.SetTrigger("gethit");
    192.                     other.GetComponent<Stats>().takingdamage(player.GetComponent<Stats>().damage);
    193.                     candamage = false;
    194.  
    195.                 }
    196.                 }
    197.  
    198.                 candamage = false;
    199.             }
    200.         }
    201.  
    202.  
    203.  
    204.  
    205.  
    206.  
    207.     // Use this for initialization
    208.     void Start () {
    209.         playeranim = player.GetComponent<Animator>();
    210.     }
    211.  
    212.     // Update is called once per frame
    213.     void Update () {
    214.      
    215.     }
    216. }
    217.  
     
    Last edited: May 26, 2017