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 /
  • Help Room /
avatar image
0
Question by ThePixelatedSword · Jan 14, 2018 at 08:32 AM · raycastmultiplayerfpsshooting

Raycast Weapon not cause damage

I'm making a fps multiplayer shooter an I've come across an issue. The player shoots the shot fine and the raycast is shot but the player that is hit doesn't take damage. I've worked on this for days trying many answers found on other forums and questions but they didn't work.

Weapons Scripts

 void Fire () {
         int id = Animator.StringToHash("Ani");
         if (isAiming == true)
             ani.SetInteger (id, 4);
         else
             ani.SetInteger (id, 5);
         ammo -= 1;
         isCooled = false;
         Invoke ("MuzzleReset", .05f);
         Invoke ("Cool", coolDownTime);
         muzzleFlash.SetActive (true);
         Debug.Log ("Fired");
         ass.PlayOneShot (shoot);
         RaycastHit hit;
         if (isAiming == true) {
             shotPos = Shooter.transform.position;
         }
         if (isAiming == false) {
             shotPos = Camera.main.transform.position;
         }
         
         if (Physics.Raycast(shotPos, transform.forward ,out hit ,range))
             Debug.DrawRay(shotPos, transform.forward, Color.green);  // draw the debug;
         
         if (hit.transform.tag == "Body") {
             Player playerr = hit.transform.gameObject.GetComponentInParent<Player> ();
             playerr.health -= damage;
         }
         hit.rigidbody.AddForceAtPosition (transform.forward * bulletForce, hit.point);
         if (hit.transform.tag == "Hit") {
             Debug.Log ("HIT!!!");
         }
     }

Player Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class Player : NetworkBehaviour {
     public int[] weaponInv;
     [SyncVar]
     public int currentWeapon;
     public GameObject[] weapons;
     public AudioClip failPickUp;
     public AudioClip switchGun;
     public Animator anim;
     public AudioListener al;
     public Camera cam;
     public Text pickupText;
     public Canvas canvas;
     public Image healthBar;
     [SyncVar]
     public float health = 100;
     float maxHealth =100;
     float healthRatio;
     public Sprite[] gunPics;
     public Image[] slots;
     AudioSource audio;
     Vector3 shotPos;
     KillPrint KillPrin;
 
     void Start () {
         audio = GetComponent<AudioSource> ();
         if (isLocalPlayer == false) {
             al.enabled = false;
             cam.enabled = false;
             canvas.enabled = false;
         }
         KillPrin = GameObject.FindGameObjectWithTag ("Killfeed").GetComponent<KillPrint> ();
 
     } 
 
     void Update () {
         healthRatio = health / maxHealth;
         Vector3 bar = healthBar.rectTransform.localScale;
         bar.x = healthRatio;
         healthBar.rectTransform.localScale = bar;
 
         slots[0].sprite = gunPics[weaponInv[0]];
         slots[1].sprite = gunPics[weaponInv[1]];
         slots[2].sprite = gunPics[weaponInv[2]];
         slots[3].sprite = gunPics[weaponInv[3]];
         slots[4].sprite = gunPics[weaponInv[4]];

 This is only parts of the code because the whole script is to long. Please help thanks in advance!


Comment
Add comment · Show 2
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 AshwinTheGammer · Jan 14, 2018 at 10:37 AM 0
Share

why u r not seeing this? 3 $$anonymous$$s ago. Do u have any problem?

avatar image meat5000 ♦ AshwinTheGammer · Jan 17, 2018 at 02:15 PM 0
Share

It happens.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AshwinTheGammer · Jan 14, 2018 at 08:42 AM

Remove this line from code

 if (hit.transform.tag == "Body") {
              Player playerr = hit.transform.gameObject.GetComponentInParent<Player> ();
              playerr.health -= damage;
          }

and add this

 public float damage = 3f;
 
 IEnumerator ApplyDamage()
         {
             //Reduce the enemy's health
             //Does NOT travel up the heirarchy.  
             if (hit.transform.tag != friendlyTag)
             {
                 if (hit.collider.gameObject.GetComponent<Player>())
                 {
                     hit.collider.gameObject.GetComponent<Player>().ApplyDamage(damage);
                 }
 
                 hit.collider.SendMessage(damageMethodName, damage, SendMessageOptions.DontRequireReceiver);
             }


And change Player HealthScript

 public float CurrentHP; // Current amount of Hp.
     public float maxHP = 100; // Max amount of Hp.
 
 public void ApplyDamage (float damage)
     {
         if (CurrentHP > 0 && damage > 0)
         {
             CurrentHP -= damage;
          
         }
        
     }

Don`t forget to add above code. Remove this

 healthRatio = health / maxHealth;











Comment
Add comment · 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

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

172 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Raycast over Photon 0 Answers

Client can damage host but not vice versa? 0 Answers

Disable Damage Raycast? 1 Answer

Help with photon being used to make a mulitplayer fps 0 Answers

FPS Raycasting vs Instantiate 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