Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Mar 11, 2014 at 12:21 AM by iamthecoolguy11 for the following reason:

Other

avatar image
-1
Question by iamthecoolguy11 · Mar 09, 2014 at 07:57 PM · c#networkshootinghealth

How to do health thru network

I am making a simple shooting multiplayer game and I tried to set up the health and shooting script and it should work but it dose not seem to work.

heres the shooting script

using UnityEngine;

using System.Collections;

 public class ShootingScript : MonoBehaviour {
 
     public int speed = 5;
     public Transform hold;
     public Transform aim;
     public bool aiming = false;
     public float FireRate;
 
     public GameObject Barrel;
     public int dammage;
 
     
     void FixedUpdate () 
     {
         
         if(Input.GetKey(KeyCode.Mouse1))
         {
             float step = speed * Time.deltaTime;
             transform.position = Vector3.MoveTowards (transform.position, aim.position, step);
         }
         else
         {
             float step = speed * Time.deltaTime;
             transform.position = Vector3.MoveTowards (transform.position, hold.position, step);
         }
 
         
         
         if(Input.GetKeyDown(KeyCode.Mouse0))
         {
             networkView.RPC("shoot",RPCMode.All);
         }
         }
     [RPC]
     void shoot()
     {
         RaycastHit hit;
         if (Physics.Raycast(transform.position, -Vector3.up, out hit, 1000))
         {
 
             Health hitter = hit.transform.gameObject.GetComponent<Health>();
             if(hitter != null)
             {
                 networkView.RPC("hit",RPCMode.AllBuffered, 1);
             }
         }
     }
 
     }

 heres the health script

 

 using UnityEngine;
 using System.Collections;
 
 public class Health : MonoBehaviour {
 public float hp = 2.0f;
 
 [RPC]
 public void hit(int dammage)
 {
     hp-=dammage;
     if(hp<=0)
     {
         Network.Destroy(gameObject);
     }
 }
 }

heres the scean

alt text

Comment
Add comment · Show 1
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
avatar image iamthecoolguy11 · Mar 10, 2014 at 03:40 AM 0
Share

:):):):):):)

1 Reply

  • Sort: 
avatar image
1

Answer by xt-xylophone · Mar 09, 2014 at 10:51 PM

What you are currently doing is when you click to shoot, you and all your 'copies' are shooting and then if each of those hit, it will send the message of a hit to everyone.

This has the problem of doubling up damage and things like that.

A few tips to change:

  • Dont need to RPC to yourself to shoot, just call 'shoot' locally.

  • You dont want your clones in other player's games to be able to deal damage too. Do an isMine on the shoot function before it RPCs the damage. Leave any effects you want to happen outside that check though so it looks like the clones are shooting, but its the owner who does the real calulations.

  • Only send the RPC of damage to the owner of the target hit rather than RPCMode.AllBuffered

Of course this is a non authoritative setup but it seems like thats what you are going for which is fine since its easier. But it does allow cheating very easily.

EDIT: Just realised you didnt say what was wrong. That ray cast with the -Vector3.down looks wrong, I assume you mean transform.forward? Right now the raycast goes straight up, not forward.

Comment
Add comment · Show 1 · Share
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
avatar image iamthecoolguy11 · Mar 09, 2014 at 11:31 PM 0
Share

everything with the raycast works fine also there is only one of this script in the scean because the multiplayer deactivates all the scripts that are not its own. Im trying to get the raycast to send a message threw the network to the players script.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unet Not Syncing Health for Host 1 Answer

Why does the wrong enemy die with this raycast? C# 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Bullets flying in a random direction. 2 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