- Home /
Enemy deals damage to player on contact
Hey I want to know how to make it so when my enemy (cube) collides with my player (tag: Player) it deals 10 damage. This is my player code please help
using UnityEngine;
using System.Collections;
public class PlayerVitals : MonoBehaviour {
public GameObject guiObject;
public int curHealth = 100;
public int maxHealth = 100;
public int recoveryAmount = 1;
public float recoverySpeed = 3;
public Color bloodColor = new Color(1.0f, 0.0f, 0.0f, 1.0f);
public Texture2D bloodyScreen;
public float fallDamageHeight = 8;
public AudioClip fallDamage;
[HideInInspector] public float percent;
private float alpha;
private float iv2;
private float minimumHealth = 40;
private float bmhRatio;
private PlayerMovement pm;
private CharacterController controller;
void Start() {
minimumHealth = 40;
pm = GetComponent<PlayerMovement>();
if(recoveryAmount != 0) {
StartCoroutine(Regenerate(0));
}
controller = GetComponent<CharacterController>();
}
void OnControllerColliderHit(ControllerColliderHit hit) {
Rigidbody rigid = hit.collider.attachedRigidbody;
if(rigid == null || rigid.isKinematic || hit.moveDirection.y < -0.3f) {
return;
}
Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
rigid.velocity = pushDir * controller.velocity.magnitude * pm.pushPower / rigid.mass;
}
void OnGUI() {
GUI.color = new Color(bloodColor.r, bloodColor.g, bloodColor.b, alpha);
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), bloodyScreen);
}
void FixedUpdate() {
alpha = Mathf.Lerp(alpha, 0.9f - (percent * 0.9f), Time.deltaTime * 7);
curHealth = Mathf.Clamp(curHealth, 0, maxHealth);
percent = (float)curHealth / (float)maxHealth;
iv2 = pm.impactVelocity;
if(curHealth <= 0) {
Die();
}
Repud.GetChild(guiObject, "health_text").GetComponent<TextMesh>().text = curHealth.ToString() + "/" + maxHealth.ToString();
Repud.GetChild(guiObject, "health_bar").SendMessage("UpdateSize", percent);
bmhRatio = maxHealth / minimumHealth;
if(pm.grounded && pm.floating && iv2 > 1) {
SendMessage("FallAnimation", iv2);
}
if(pm.grounded && pm.floating && iv2 > fallDamageHeight) {
FallDamage(((iv2 - (fallDamageHeight * 0.8f)) * ((iv2 - (fallDamageHeight * 0.8f)) * 0.5f)));
iv2 = 0;
pm.floating = false;
}
}
public IEnumerator Regenerate(int amount) {
curHealth += amount;
yield return new WaitForSeconds(recoverySpeed);
StartCoroutine(Regenerate(recoveryAmount));
}
private void FallDamage(float prc) {
if((int)(prc) <= 0) {return;}
ApplyDamage((int)prc);
audio.clip = fallDamage;
audio.volume = 0.02f;
audio.Play();
}
public void ApplyDamage(float amount) {
curHealth -= (int)amount;
}
public void Die() {
Destroy(gameObject);
}
}
I have this but it doesn't seem to work: function OnCollisionEnter(collision : Collision){
if(collision.CompareTag("Player")){
//Reduce global health var
curHealth =- 10;
}
}
I want the enemies to be kind of like in this game:
Answer by Rasheedh20 · Feb 19, 2014 at 05:24 PM
Have you given the cube a tag such as enemy to detect a collision?
Answer by AnXgotta · Feb 19, 2014 at 05:38 PM
At first glance, I would say that checking if a "Player" has collided with your player in your OnCollisionEnter(...)
method isn't what you want.
Tag your enemies with "Enemy" and check for that tag in your player's OnCollisionEnter(...)
method.
function OnCollisionEnter(collision : Collision){
if(collision.gameObject.tag == "Enemy")){
//Reduce global health var
curHealth =- 10;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Enemy Collision Wont Work! PLEASE HELP 0 Answers
Enemy death help 1 Answer
Damage problem 4 Answers
Bullet not destroying on collision 1 Answer