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;
}
}
}
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...
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
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.
no ... thats the point the damage wont change ... in another script this random number picking works just fine ...
Your answer
Follow this Question
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