Hello all, I am making a small MOBA game in which the main player attacks enemy with a fireball and enemy health decreases.
The problem I am facing is that when I am throwing fireballs, the main player's health is also decreasing. I have added collider on both fireball and player, and on enemy have called OnCollisionEnter() on player script as I am changing the tag of other player to 'Enemy' once both are in network.
I tried to shift spawn point ahead of the player but still this is the issue. You can see the script, this script is used across the network.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using Mirror;
using UnityEngine.Assertions;
public class Mage : NetworkBehaviour
{
private Transform targetedEnemy;
private bool enemyClicked = false;
private bool walking;
private Animator anim;
private NavMeshAgent navAgent;
private float nextShot;
private float timeBetweenShots = 2f;
private bool isAttacking = false;
[SyncVar(hook="OnHealthChanged")] private int health = 100;
private int fireDamage = 20;
private Vector3 startingPosition;
[SerializeField] private float shootDistance;
[SerializeField] private Transform spawnPoint;
[SerializeField] private GameObject fireballPrefab;
[SerializeField] private GameObject playerHalo;
[SerializeField] private TextMesh healthText;
public override void OnStartLocalPlayer()
{
playerHalo.SetActive(true);
tag = "Player";
}
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
navAgent = GetComponent<NavMeshAgent>();
Assert.IsNotNull(spawnPoint);
Assert.IsNotNull(fireballPrefab);
Assert.IsNotNull(playerHalo);
Assert.IsNotNull(healthText);
startingPosition = transform.position;
}
// Update is called once per frame
void Update()
{
healthText.text = health.ToString(); //visible to all
if(!isLocalPlayer)
{
return;
}
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Input.GetButtonDown("Fire2"))
{
if(Physics.Raycast(ray,out hit,100))
{
if(hit.collider.CompareTag("Enemy"))
{
targetedEnemy = hit.transform;
enemyClicked = true;
}
else{
isAttacking = false;
enemyClicked = false;
walking = true;
navAgent.destination = hit.point;
navAgent.Resume();
}
}
}
if(enemyClicked)
{
MoveAndShoot();
}
if(navAgent.remainingDistance <= navAgent.stoppingDistance)
{
walking = false;
}else{
if(!isAttacking)
walking = true;
}
anim.SetBool("IsWalking",walking);
}
private void MoveAndShoot()
{
if(targetedEnemy == null)
{
return;
}
navAgent.destination = targetedEnemy.position;
if(navAgent.remainingDistance >= shootDistance)
{
navAgent.Resume();
walking = true;
}
if(navAgent.remainingDistance <= shootDistance)
{
transform.LookAt(targetedEnemy);
if(Time.time > nextShot)
{
isAttacking = true;
nextShot = Time.time + timeBetweenShots;
CmdFire();
}
navAgent.Stop();
walking = false;
}
}
[Command]
void CmdFire()
{
anim.SetTrigger("Attack");
GameObject fireball = Instantiate(fireballPrefab,spawnPoint.position,spawnPoint.rotation) as GameObject;
fireball.GetComponent<Rigidbody>().velocity = fireball.transform.forward*3;
NetworkServer.Spawn(fireball);
Destroy(fireball,2.5f);
}
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Fireball"))
{
TakeDamage();
}
}
void OnHealthChanged(int health,int updatedHealth)
{
healthText.text = updatedHealth.ToString();
}
void TakeDamage()
{
if(!isServer)
{
return;
}
health -= fireDamage;
if(health <= 0)
{
health = 100;
RpcRespawn();
}
}
[ClientRpc]
void RpcRespawn()
{
if(isLocalPlayer)
{
transform.position = startingPosition;
}
}
}