I have lots of Code here but basically i have followed a tutorial and created an energy/health/shield bar. They reduce based on their health % value (so 100 of 1000 hp will display 10% HP).
My damage code is not working. I need it to deal damage and react with the above features, and destroy the player when at 0 hp.
You forgot a lot of things, like does it give you errors
(I’m sure this does: Destroy.this.gameObject, it’s Destroy(gameObject), parentheses and you don’t need the this)
< - 0 ? you mean < 0?
Ok making a little progress, i think i have the damage stuff down, but im getting issues.
im not dealing damage
I get an error after the first shot
Please Advise
here is all the code i am using
For Firing
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class firemechanic : MonoBehaviour {
public Rigidbody projectile;
public float speed = 20;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1")) {
Rigidbody instantiatedProjectile = Instantiate (projectile,
transform.position,
transform.rotation)
as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection (new Vector3 (0, 0, speed));
}
}
}
for the projectile
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class fire : MonoBehaviour
{
public float damage = 100f;
public float speed;
public Rigidbody rigidbody;
void Start ()
{
rigidbody = GetComponent<Rigidbody>();
rigidbody.velocity = transform.forward * speed;
}
void Update(){
}
void OnCollisionEnter (Collision collision)
{
if (collision.gameObject.tag == "Enemy")
{
collision.gameObject.SendMessage("TakeDamage", damage, SendMessageOptions.DontRequireReceiver);
}
}
}
For Life
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class lifesystem : MonoBehaviour {
public float hitPoints = 5000f;
public float currentHitPoints;
public GameObject destroyfx;
// Use this for initialization
void Start () {
currentHitPoints = hitPoints;
}
public void TakeDamage(float amt)
{
currentHitPoints -= amt;
if(currentHitPoints <= 0)
{
currentHitPoints = 0;
Die();
}
}
public void Die()
{
Debug.Log ("Enemy Has died");
}
// Update is called once per frame
void Update () {
}
}
[B]
Im thinking it has something to do with the reference to the object.
When i fire i get an error referencing to another HP Script i have. I tried removing it but it does not seem to resolve the issue (Strangely).
Does the collision / rigged body need to be active on the prefab, or the mesh itself?
I think the issue is here. I noticed that some times the objects hit the other mesh target, absorbing the shot, yet not dealing damage.
No they don’t, we asked you what the error was, did you answer? NO, we’re not compilers, we won’t look at your 150 lines of code one by one just to see what kind of problem you have if you don’t even take the time to tell us the error you’re getting
If someone posts 20 questions and none of them gets answered, then he can say this, but this was your first post, why?
I’m not sure exactly how your code is written. But i would guess that your enemy has the lifesystem attached to it. Try to get the lifesystem component from the enemy when the collision occures and call the TakeDamage method from there, instead of sending a message.