- Home /
Question by
SirZok · Feb 26, 2017 at 01:23 PM ·
unity 5networkingraycastlinerenderer
Raycasts in Multiplayer Network
I recently started a FPS Project and I watched some tutorials on YouTube. Because everything worked perfect, I had the idea, to make it Multiplayer playable.
So here is my code: (Some variables have German names because I am from Germany)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class Waffenscript : NetworkBehaviour
{
public bool automatisch;
[SyncVar] public int gunDamage;
public int ammoCapacity;
public int ammo;
public float reloadTime;
public float fireRate;
public float weaponRange;
public float hitForce;
public Transform shotSpawn;
public GameObject shotSound;
public GameObject reloadSound;
public GameObject bluteffekt;
[SyncVar] Vector3 rayOrigin = new Vector3();
Text ammotext;
Text reloadtext;
Text distanztext;
private Camera kamera;
private WaitForSeconds shotDuration = new WaitForSeconds(.05f);
private float nächsterSchuss;
private LineRenderer schussLinie;
private bool reloading;
private float zwischenzeit;
RaycastHit hit;
void Start()
{
ammotext = GameObject.Find("/Canvas/Ammo/").GetComponent<Text>();
reloadtext = GameObject.Find("/Canvas/Reload/").GetComponent<Text>();
distanztext = GameObject.Find("/Canvas/Distanz/").GetComponent<Text>();
reloading = false;
reloadtext.text = ("");
ammotext.text = ammo.ToString();
ammo = ammoCapacity;
schussLinie = GetComponent<LineRenderer>();
kamera = GetComponentInParent<Camera>();
}
void Update()
{
if (reloading)
{
reloadtext.text = (reloadTime + zwischenzeit - Time.time).ToString("F2");
}
if (reloadTime + zwischenzeit - Time.time < 0)
{
reloadtext.text = "";
}
if (Input.GetButtonDown("Reload"))
{
StartCoroutine(Reload());
Instantiate(reloadSound, transform.position, transform.rotation);
}
if (Input.GetButtonDown("Fire1") && Time.time > nächsterSchuss && (ammo > 0) && !reloading && !automatisch)
{
ShootNotAuto();
}
if (Input.GetButton("Fire1") && Time.time > nächsterSchuss && (ammo > 0) && !reloading && automatisch)
{
ShootAuto();
}
}
private void FixedUpdate()
{
RaycastHit distanz;
schussLinie.SetPosition(0, shotSpawn.position);
Vector3 rayOrigin = kamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
Debug.DrawRay(rayOrigin, kamera.transform.forward * weaponRange, Color.green);
Physics.Raycast(rayOrigin, kamera.transform.forward, out distanz);
distanztext.text = (distanz.distance.ToString("F2") + " m");
}
[Client]
void ShootNotAuto()
{
nächsterSchuss = Time.time + fireRate;
StartCoroutine(SchussEffekt());
Instantiate(shotSound, transform.position, transform.rotation);
rayOrigin = kamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
schussLinie.SetPosition(0, shotSpawn.position);
if (Physics.Raycast(rayOrigin, kamera.transform.forward, out hit, weaponRange))
{
schussLinie.SetPosition(1, hit.point);
if (hit.transform.gameObject.tag == "Gegenstand_Bewegbar")
{
hit.rigidbody.AddForce(-hit.normal * hitForce);
}
if (hit.transform.gameObject.tag == "Spieler")
{
CmdDamager(hit.transform.gameObject);
}
}
else
{
schussLinie.SetPosition(1, rayOrigin + (kamera.transform.forward * weaponRange));
}
ammo = ammo - 1;
ammotext.text = ammo.ToString();
}
[Client]
void ShootAuto()
{
nächsterSchuss = Time.time + fireRate;
StartCoroutine(SchussEffekt());
Instantiate(shotSound, transform.position, transform.rotation);
rayOrigin = kamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
RaycastHit hit;
schussLinie.SetPosition(0, shotSpawn.position);
if (Physics.Raycast(rayOrigin, kamera.transform.forward, out hit, weaponRange))
{
schussLinie.SetPosition(1, hit.point);
if (hit.transform.gameObject.tag == "Gegenstand_Bewegbar")
{
hit.rigidbody.AddForce(-hit.normal * hitForce);
}
if (hit.transform.gameObject.tag == "Spieler")
{
Instantiate(bluteffekt, hit.point, transform.rotation);
CmdDamager(hit.transform.gameObject);
}
}
else
{
schussLinie.SetPosition(1, rayOrigin + (kamera.transform.forward * weaponRange));
}
ammo = ammo - 1;
ammotext.text = ammo.ToString();
}
[Command]
void CmdDamager(GameObject ziel)
{
ziel.GetComponent<HealthManager>().RpcSchaden(gunDamage);
}
private IEnumerator SchussEffekt()
{
schussLinie.enabled = true;
yield return shotDuration;
schussLinie.enabled = false;
}
private IEnumerator Reload()
{
zwischenzeit = Time.time;
reloading = true;
yield return new WaitForSeconds(reloadTime);
ammo = ammoCapacity;
ammotext.text = ammo.ToString();
reloading = false;
}
}
This script is on the gun the Player holds in his hand. When it detects the tagged "Player" it should do this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
using UnityEngine;
public class HealthManager : NetworkBehaviour {
public int maxHealth = 100;
[SyncVar] public int health = 100;
void Awake () {
health = maxHealth;
}
[ClientRpc]
public void RpcSchaden(int damage)
{
health = health - damage;
Debug.Log(transform.name + " hat noch " + health + "Leben.");
if (health <= 0)
{
Die();
}
}
[Client]
void Die()
{
transform.position = Vector3.zero;
}
}
I don't know why it won't work and I am actually very confused because of all the things that needed to be changed. It would be nice if somebody could help me.
Comment
Your answer
Follow this Question
Related Questions
[C#] Line Renderer beam not going straight 1 Answer
Raycast.distance giving me the wrong number 2 Answers
OnServerStart not working in Build. Works in editor 1 Answer
Help with LineRenderer 1 Answer