Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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

uNet horrible lagg (even on local servers) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges