Ran into a weird problem while testing out my shooting function in 2D game. Apparently only the servers host has a working raycast + only the host is able to deal damage (because of the working raycast). Meanwhile the clients raycast is all bonkers. Any idea what is causing this?
This is the script handling the shooting.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class Shoot : NetworkBehaviour {
public int dmg = 1;
[SerializeField]
private RaycastHit2D hit;
GameObject player = GameObject.FindGameObjectWithTag("Player");
Transform Shootpoint;
// Use this for initialization
void Awake () {
Shootpoint = transform.FindChild("Shootpoint");
if (Shootpoint == null)
{
Debug.Log("No 'shootpoint' set!");
}
}
// Update is called once per frame
void Update ()
{
CheckShooting();
}
void Shooting()
{
//Debug.Log("Testi testi");
Vector2 ShootPointPos = new Vector2(Shootpoint.position.x, Shootpoint.position.y);
Vector2 ShootDirection = (Vector2)GameObject.FindGameObjectWithTag("Player").transform.position - ShootPointPos;
RaycastHit2D hit = Physics2D.Raycast(ShootPointPos, ShootDirection.normalized, -100);
Debug.DrawLine(ShootPointPos, ShootDirection.normalized * -100);
Debug.Log(hit.transform.tag);
if (hit.transform.tag == "Player")
{
string uIdentity = hit.transform.name;
CmdTellServerWhoWasShot(uIdentity, dmg);
}
}
void CheckShooting()
{
if (!isLocalPlayer)
{
return;
}
if (Input.GetKey(KeyCode.A))
{
Shooting();
}
}
[Command]
void CmdTellServerWhoWasShot(string uniqueID, int dmg)
{
GameObject go = GameObject.Find(uniqueID);
go.GetComponent<hp>().lowerHealth(dmg);
}
}
Here is also a gif about the problem (the raycast is swaying under the client, but the hosts raycast is pointing straight to facing direction).
Edit: The gif is recorded on host, but the problem is on client side. The gif is only there to show you the problem.
Make sure the FindGameObjectWithTag(âPlayerâ) is finding the right GameObject. If your client joins when your host is already instantiated youâd have two GameObjects with the âPlayerâ tag.
I think youâre overthinking this. I donât know what your setup is but if your âShootâ NetworkBehaviour is attached to the player game object you could use the âgameObjectâ variable to reference your playerâs game object.
Iâm not quite sure what you mean (as Iâm fairly new with Unity and Unet), but Iâm definitely overthinking this problem. I tried a not-so-great solution by simply checking if the player name in the hierarchy was âPlayer1â or âPlayer2â and gave both of them separate Void shooting(). This is by no means a great way to do this, but I just wanted to try what would happen. Player2 (client) now has two raycast lines, one working as intended and one broken (the one in the gif I posted before).
if (Input.GetKey(KeyCode.A))
{
if(GameObject.Find("Player1"))
{
Shooting();
}
if (GameObject.Find("Player2"))
{
Shooting2();
}
}
This is not something I want to do, but at least it did something.
âtransformâ here indicates the transform of the game object that the script is attached to. This way you should always get the current playerâs game object without having to look it up.
Thank you Scrorr, that did the trick. I believe at some point I had a similar line of code, but somehow missed the problem itself. Like you said, I was overthinking it!