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 Julian-Dicken · Feb 02, 2016 at 05:19 PM · shootingraycastingnot working

Having trouble with player health

Heres my code ... it isnt working unfortunatly

Bullet : using UnityEngine; using System.Collections;

public class Ak47_BulletPhysics : MonoBehaviour {

 public float speed = 1f;
 public float range = 6;
 public float Damage;
 public int MinDamage = 3;
 public int MaxDamage = 6;

   
 public GameObject Bullet;
 public GameObject bulletHole;
 public GameObject Player;

 RaycastHit hit;

 void Start ()
 {
 
 }
 
 
 void Update ()
 {
     
     range -= 0.1f;
     if (range <= 0)
     {
         Destroy(Bullet);
     }
     transform.Translate(0, 0, speed);
     
 }
 void FixedUpdate()
 {
     Ray ray = new Ray(transform.position, transform.forward);
     if (Physics.Raycast(ray, out hit, range))
     {
         System.Random rnddamage = new System.Random();
         float Damage = rnddamage.Next(MinDamage, MaxDamage);
         PlayerHealth PlayerHealth = Player.GetComponent<PlayerHealth>();
         if (hit.transform.tag == "Explosive")
         {
             range = 0.1f;
         }
         if (hit.transform.tag == "Player")
         {
             range = 0.1f;
             print("hit");
             
             
             PlayerHealth.Health -= Damage;

            
         }
         if (hit.transform.tag == "World")
         {
             range = 0.1f;
             Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
         }
     }
 }

}

Health :

using UnityEngine; using System.Collections;

public class PlayerHealth : MonoBehaviour { public float Health; public float MaxHealth = 100; public float LastHealth;

 public GameObject Player;

 void Start ()
 {
     Health = MaxHealth;
 }
 
 void Update()
 {
    // LastHealth = Health;
 }

 void LateUpdate ()
 {
     if (Health > LastHealth)
     {

     }
     if(Health <= 0)
     {
         Player.active = false;
     }
     if (Health > 0)
     {
         Player.active = true;
     }
 }

}

Comment
Add comment · Show 7
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 Julian-Dicken · Jan 31, 2016 at 02:09 PM -1
Share

pls help XD

avatar image gjf · Jan 31, 2016 at 02:52 PM 0
Share

almost everyone here is a volunteer, so it may take some time to get answers. no need to post additional comments to bump...

it's helpful if you have formatted ALL of the code too - that way any line numbers from error messages will be accurate and make it more likely for the people helping to spot the problem/provide answers/etc.

finally, and this is the most important, stating "it isnt working unfortunatly" (sic) isn't enough information for us to work out what your problem might be.

what does it do?

what doesn't it do?

what do you expect it to do?

help us, help you...

avatar image Julian-Dicken · Jan 31, 2016 at 05:15 PM 0
Share

So : what the script SHOULD do : If my bullet object hits a player it shoul substract the damage (which is a random number between 3 and 6) from the players life

What the script ISNT doing : subsracting life

avatar image gjf Julian-Dicken · Jan 31, 2016 at 09:21 PM 0
Share

as i noted before - almost everyone here is a volunteer, so it may take some time to get answers. NO need to post additional comments to bump - that usually ends in people skipping to the next question.

back to your problem - does it print "hit" on collision?

have you printed the value for Damage to check that it's being changed from zero?

btw, your random number will be 3, 4 or 5 so if it's being set, you should see it reduced.

avatar image Julian-Dicken gjf · Feb 01, 2016 at 01:54 PM 0
Share

no ... thats the point the damage wont change ... in another script this random number picking works just fine ...

avatar image Julian-Dicken · Jan 31, 2016 at 05:57 PM -1
Share

-pushed- :D

avatar image Julian-Dicken · Jan 31, 2016 at 07:04 PM -1
Share

re pushed-

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I define the direction and speed of a projectile separately? (JS) 0 Answers

am trying to instantiate a 3D object to fly in the direction where my mouse is pointing 0 Answers

Enemy ignore walls in sight! Help! 0 Answers

How to make raycast range on this script? 0 Answers

How do you make a projectrile go the direction the player is facing? 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