Search Unity

Player Attacks Self, Not Other Players

Discussion in 'Scripting' started by unity_8g7XwYU6kpaB-Q, Aug 23, 2019.

  1. unity_8g7XwYU6kpaB-Q

    unity_8g7XwYU6kpaB-Q

    Joined:
    Aug 17, 2019
    Posts:
    9
    I am trying to be able to attack other players in range, please give me any suggestion you think would help.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.Networking;
    7.  
    8. public class PlayerUnit : NetworkBehaviour
    9. {
    10.  
    11.    public float speed;
    12.    public int startingHealth = 100;
    13.    public int currentHealth;
    14.    public int damage;
    15.    public GameObject playerCamera;
    16.    public bool isPlayer = true;
    17.    public bool enemyCheck;
    18.    public bool playerCheck;
    19.  
    20.  
    21.    private Rigidbody2D myRigidbody;
    22.    private Vector3 displacement;
    23.    private Animator animator;
    24.    private float lastAttackTime;
    25.    private readonly float attackCooldown = 1f;
    26.  
    27.    bool isDead;
    28.  
    29.  
    30.    [SerializeField]
    31.    GameObject PlayerUIPrefab;
    32.  
    33.    [HideInInspector]
    34.    public GameObject playerUIInstance;
    35.  
    36.    void Awake()
    37.    {
    38.        currentHealth = startingHealth;
    39.    }
    40.     // Start is called before the first frame update
    41.     void Start()
    42.     {
    43.        animator = GetComponent<Animator>();
    44.        myRigidbody = GetComponent<Rigidbody2D>();
    45.        GetComponent<NetworkAnimator>().SetParameterAutoSend(0, true);
    46.        if (isLocalPlayer == true)
    47.        {
    48.            playerCamera.SetActive(true);
    49.            //Create Player UI
    50.            playerUIInstance = Instantiate(PlayerUIPrefab);
    51.            playerUIInstance.name = PlayerUIPrefab.name;
    52.    
    53.            //Configure Player UI
    54.            PlayerUI ui = playerUIInstance.GetComponent<PlayerUI>();
    55.            if (ui == null)
    56.                Debug.LogError("No PlayerUI Component on PlayerUI Prefab.");
    57.            ui.SetPlayer(GetComponent<PlayerUnit>());
    58.        }else
    59.            {
    60.                playerCamera.SetActive(false);
    61.            }
    62.    }
    63.  
    64.    void Update()
    65.    {
    66.        GetHealth();
    67.    
    68.    }
    69.  
    70.     // Update is called once per frame
    71.     void FixedUpdate()
    72.     {
    73.    
    74.        if (isLocalPlayer)
    75.        {
    76.         displacement = Vector3.zero;
    77.        displacement.x = Input.GetAxisRaw("Horizontal");
    78.        displacement.y = Input.GetAxisRaw("Vertical");
    79.        UpdateAnimation();
    80.  
    81.        }
    82.    }
    83.  
    84.    void UpdateAnimation()
    85.    {
    86.    if (displacement != Vector3.zero)
    87.    {
    88.        MoveCharacter();
    89.        animator.SetFloat("MoveX", displacement.x);
    90.        animator.SetFloat("MoveY", displacement.y);
    91.        animator.SetBool("Moving", true);
    92.    }else{
    93.        animator.SetBool("Moving", false);
    94.    }
    95.    if (Input.GetKeyDown(KeyCode.K)) {
    96.        //kick animation called
    97.        animator.SetBool("Kicked", true);
    98.    }else{
    99.        animator.SetBool("Kicked", false);
    100.    }
    101.    }
    102.  
    103.    void MoveCharacter()
    104.    {
    105.  
    106.        myRigidbody.MovePosition(
    107.            transform.position + displacement * speed * Time.deltaTime
    108.            );
    109.    }
    110.  
    111.    public void OnTriggerStay2D(Collider2D other)
    112.         {
    113.             if (Input.GetKeyDown(KeyCode.K) && Time.time - lastAttackTime > attackCooldown ){
    114.                 lastAttackTime = Time.time;
    115.                 PlayerUnit playerHealth;
    116.            
    117.                 playerHealth = other.gameObject.GetComponent<PlayerUnit>();
    118.            
    119.                if (playerHealth.currentHealth > 0)
    120.                {
    121.                    playerHealth.TakeDamage(damage);
    122.                }
    123.             }
    124.        
    125.         }
    126.  
    127.  
    128.    public void TakeDamage(int damage)
    129.    {
    130.    
    131.        currentHealth -= damage;
    132.    
    133.        if (currentHealth <= 0 && !isDead)
    134.        {
    135.            Death();
    136.        }
    137.    }
    138.  
    139.    public float GetHealth()
    140.    {
    141.        return (float) currentHealth/startingHealth;
    142.    }
    143.    void Death()
    144.    {
    145.        isDead = true;
    146.    
    147.    }
    148.  
    149. }
    150.  
    The player when attacking only affects their own health
     
    Last edited: Aug 23, 2019
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    In OnTriggerStay2D you're not checking if the Player object this is called on is the Player object for this client, so you're running the same code for all Player objects, not just the client's Player object.

    Secondly, you're not doing any syncing of any of the script's data between the clients and the server, so the values on the server and all the clients are going to be different. So if you deal damage on a client, it isn't telling the server, and the server isn't telling the other clients, etc, etc.

    Remember, unless you specifically tell your code not to, then all code is being run on all objects on all connected computers. No data is being shared between them unless you specifically sync it, or use one of the built in syncing features like SyncVar's.

    Lastly, Unet has been deprecated over a year, was never really finished, and ended in a still buggy state. Switch to another network API if you are this early in your project's development.
     
    Last edited: Aug 23, 2019