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 /
avatar image
0
Question by mradamw · Oct 14, 2014 at 02:07 PM · raycasthitpoints

Help me fix this basic HP script

I'm working on a basic HP script (copying the one found in this video - http://www.youtube.com/watch?v=-3c9Eohwdvw - just with some modified names). However, when I'm in game the object I shoot isn't being destroyed once it's taken 'lethal' damage.

Here is the HasHealth.cs snippet:

 using UnityEngine;
 using System.Collections;
 
 public class HasHealth : MonoBehaviour {
 
     public float objectHitPoints = 100f;
 
     public void receiveDamage (float damageAmount) {
         // Decrement the HP each time damage is taken
         objectHitPoints -= damageAmount;
         // When the object has no more HP...
         if (objectHitPoints <= 0) {
             Die();
         }
 
     }
 
     void Die (){
         Destroy (gameObject);
     }
 
 }

and here is the excerpt from my Player_Shoots.cs file.

 using UnityEngine;
 using System.Collections;
 
 public class Player_Shoots : MonoBehaviour {
     
     public float damage = 50f;
     public GameObject bulletPrefab;
 
     // Use this for initialization
     void Start () {
 
     }
 
     // Update is called once per frame
     void Update () {
 
         if (Input.GetMouseButtonDown (0) && clipSize > 0) {
             Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
             RaycastHit hitInfo;
 
             // Raycast to detect hits
             if(Physics.Raycast(ray, out hitInfo)){
                 Vector3 hitPoint = hitInfo.point;
 
                 // Deal damage to target, IF it has health
                 HasHealth h = gameObject.GetComponent<HasHealth>();
 
                 if (h != null) {
                     h.receiveDamage(damage);
                 }
 
                 // Instantiate our bullet at the hitPoint
                 if(bulletPrefab != null){
                     GameObject bullet = (GameObject)Instantiate(bulletPrefab, hitPoint, Camera.main.transform.rotation);
                     bullet.rigidbody.AddForce(0, 400 , 0);
                 }
             }
         }
     }
 }

I've also attached a screenshot of the Inspector panel of my target cube - the object that should be destroyed once shot twice.

alt text

screen shot 2014-10-14 at 11.23.18.png (49.9 kB)
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 SaraCecilia · Oct 14, 2014 at 01:00 PM 1
Share

Hi, please edit the title of this post so users can see what you are asking for specifically. Also, please write where you think the problem lies and what you've attempted to do to try and fix it.

avatar image _dns_ · Oct 14, 2014 at 02:15 PM 0
Share

Hi, using Debug.Log("some text telling where you are in the execution of the code"); you can check what condition was true or false: it will help you narrow the problem and find why/where it doesn't work. Debug.DrawLine or .DrawRay is also very useful to display rays or vectors and see if everything is where is should be.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Landern · Oct 14, 2014 at 02:12 PM

If Player_Shoots.cs is attached to your player and HasHealth.cs is attached to the cube(what the player object is firing at), then your code is trying to get your HasHealth.cs from the player and not the gameobject the raycast it.

Current update with possible issue:

  void Update () {
  
         if (Input.GetMouseButtonDown (0) && clipSize > 0) {
             Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
             RaycastHit hitInfo;
  
             // Raycast to detect hits
             if(Physics.Raycast(ray, out hitInfo)){
                 Vector3 hitPoint = hitInfo.point;
  
                 // Deal damage to target, IF it has health
                 HasHealth h = gameObject.GetComponent<HasHealth>(); //<-- Your problem, gameObject refers to the gameObject that this is attached to.
  
                 if (h != null) {
                     h.receiveDamage(damage);
                 }
  
                 // Instantiate our bullet at the hitPoint
                 if(bulletPrefab != null){
                     GameObject bullet = (GameObject)Instantiate(bulletPrefab, hitPoint, Camera.main.transform.rotation);
                     bullet.rigidbody.AddForce(0, 400 , 0);
                 }
             }
         }
     }

Make sure you're referencing the gameobject that was hit that has the HasHealth script attached. I'm basing this off the screen shot you included and making assumptions:

  void Update () {
  
         if (Input.GetMouseButtonDown (0) && clipSize > 0) {
             Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
             RaycastHit hitInfo;
  
             // Raycast to detect hits
             if(Physics.Raycast(ray, out hitInfo)){
                 Vector3 hitPoint = hitInfo.point;
  
                 // Deal damage to target, IF it has health
                 HasHealth h = hitInfo.collider.gameObject.GetComponent<HasHealth>();
  
                 if (h != null) {
                     h.receiveDamage(damage);
                 } else {
                     Debug.Log("Couldn't Get HasHealth Script after raycast hit");
                 }
 
  
                 // Instantiate our bullet at the hitPoint
                 if(bulletPrefab != null){
                     GameObject bullet = (GameObject)Instantiate(bulletPrefab, hitPoint, Camera.main.transform.rotation);
                     bullet.rigidbody.AddForce(0, 400 , 0);
                 }
             }
         }
     }

Also adding some Debug.Log statements can help you narrow thing down a bit. I added one just in case HasHealth couldn't be found.

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 mradamw · Oct 14, 2014 at 03:12 PM 0
Share

Thank you SO much for your help, I really appreciate it.

It's weird that my behaviour was so different to that in the video, but I'm going to write that off as different versions of Unity?

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

accuracy problem with ray cast 2 Answers

RayCast hit point. 0 Answers

How should I make a custom suspension system? 1 Answer

Draw Line In GameView Point to point 1 Answer

casting a raycast from the raycast hit.point 1 Answer


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