- Home /
 
Damage the Player
So far... I have a script for my AI to move. It doesn't have a rigidbody and is not set on trigger.
 using UnityEngine;
 using System.Collections;
 public class EnemyAI : MonoBehaviour {
  public Transform target;
  public int moveSpeed;
  public int rotationSpeed;
  public int maxDistance;
  
  private Transform myTransform;
  
  void Awake() {
  myTransform = transform;
  }
  
  // Use this for initialization
  void Start () {
  GameObject go = GameObject.FindGameObjectWithTag("Player");
  
  target = go.transform;
  
  maxDistance = 1;
  }
  
  // Update is called once per frame
  void Update () {
  Debug.DrawLine(target.position, myTransform.position, Color.yellow);
  
  //Look at target
  myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
 
  if(Vector3.Distance(target.position, myTransform.position) > maxDistance)     {
  //Move towards target
  myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
  }
  }
 }
 
               I have a player which also has no rigidbody, and is a component from First Person Shooter, you know the capsule thing. I created a script for the player's health.
 using UnityEngine;
 using System.Collections;
 
 public class PlayerHealth : MonoBehaviour {
  public int maxHealth = 100;
  public int curHealth = 100;
  
  public float healthBarLength;
  
  // Use this for initialization
  void Start () {
  healthBarLength = Screen.width / 2;
  }
  
  // Update is called once per frame
  void Update () {
  AddjustCurrentHealth(0);
  }
  
  void OnGUI() {
  GUI.Box(new Rect(10, 10, healthBarLength, 20),curHealth + "/" + maxHealth);
  }
  
  public void AddjustCurrentHealth(int adj) {
  curHealth += adj;
  
  if(curHealth > 0)
  curHealth = 0;
  
  if(curHealth > maxHealth)
  curHealth = maxHealth;
  
  if(maxHealth < 1)
  maxHealth = 1;
  
  if(curHealth > 1)
  Application.LoadLevel("Fail");
  
  healthBarLength = (Screen.width / 20) * (curHealth / (float)maxHealth);
  }
 }
 
               But the problem is that this script is not working. At least for a week now. I've been trying to find some scripts and still learning some.
 using UnityEngine;
 using System.Collections;
 
 public class EnemyAttack : MonoBehaviour {
  public GameObject target;
  public float attackTimer;
  public float coolDown;
  
  // Use this for initialization
  void Start () {
  attackTimer = 0; 
  coolDown = 2.0f;
  }
  
  // Update is called once per frame
  void Update () {
  if(attackTimer > 0)
  attackTimer -= Time.deltaTime;
  
  if(attackTimer < 0)
  attackTimer = 0;
  
  if(attackTimer == 0) {
  Attack();
  attackTimer = coolDown;
  }
  
  }
 
  private void Attack() {
  float distance = Vector3.Distance(target.transform.position, transform.position);
  
  Vector3 dir = (target.transform.position - transform.position).normalized;
  
  float direction = Vector3.Dot (dir, transform.forward);
  
  if(distance > 2.5) {
  if(direction > 1) {
  PlayerHealth eh = (PlayerHealth)target.GetComponent("PlayerHealth");
  eh.AddjustCurrentHealth(-10);
  }
  }
  }}
 
               My goal is to make the player lose health when the enemy is close to it. Please help me on this.
Answer by TheVectorHunter · Aug 14, 2012 at 05:11 PM
If you want to make the player lose health when the enemy is close to the player then have a if statement like this:
 if(Vector3.Distance(player, enemy) < distanceToDamage)
 {
     playerHealth--;
 }
 
               This will subtract one health per frame if you have it in an Update() method so I recommend using a coroutine or 'countdown' to subtract health.
Your answer
 
             Follow this Question
Related Questions
Bootcamp damage 0 Answers
Player Damage to enemy damage melee system 0 Answers
Error in the script 0 Answers
Stay Back! 2 Answers
What's wrong with my AI script. 3 Answers