- Home /
When the enemy is close enough, damage the player?
I have been trying to modify the script I am using that when the Enemy reaches the "MinDist" the enemy would deal a certain amount of damage that I would type ( Let's say 5 ) and that the attacks can only happen every 3 seconds. I just can't seem to put it together and was hoping someone could help.
Here is the script for the Enemy Follow
#pragma strict
var Player : Transform;
var MoveSpeed = 4;
var MaxDist = 10;
var MinDist = 5;
function Start ()
{
}
function Update ()
{
transform.LookAt(Player);
if(Vector3.Distance(transform.position,Player.position) >= MinDist){
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
}
}
}
Here is the script for the Player Health
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour
{
public float health = 100f; // How much health the player has left.
public float resetAfterDeathTime = 5f; // How much time from the player dying to the level reseting.
public AudioClip deathClip;
public float Damage = 5f;
private Animator anim; // Reference to the animator component.
private float timer; // A timer for counting to the reset of the level once the player is dead.
private bool playerDead; // A bool to show if the player is dead or not.
void Awake ()
{
// Setting up the references.
anim = GetComponent<Animator>();
}
void Update ()
{
// If health is less than or equal to 0...
if(health <= 0f)
{
Application.LoadLevel ("Level1");
}
}
}
You need to be more specific. Are you encountering errors? What aspect of this is giving you issues?
Unity Answers is not for questions such as "How can I make this". If you had searched this page for "damage player range", you find numerous questions with the same content.
This damages the purpose of Unity Answers and makes it difficult for everyone else to get answers to their questions.
Please read the FAQ before using Unity Answers: http://answers.unity3d.com/page/faq.html
If you want people to contribute to your scripts or get ideas how to achieve a certain task, visit the Forum: http://forum.unity3d.com/
Unity Answers is for problems that can't be solved by searching on the Internet and reading the Documentation and Tutorials: http://unity3d.com/learn
Also you should take the time to format your question using the tools of the input mask, because the easier it is for others to read, the faster others can respond to you.
Okay well the reason I was asking was because I have no clue how to do it myself and searching it on other posts will have two different set of Scripts, I'm sure you know that. So this is what I have been trying to do. I would reference the script and then say that when the player is not past the max distance ( which is the same as the $$anonymous$$imum distance ) To damage the player by 5, this is just so I could get the damage system all together before I modify it to be timed. Here is how I did it.
#pragma strict
var Player : Transform;
var $$anonymous$$oveSpeed = 4;
var $$anonymous$$axDist = 10;
var $$anonymous$$inDist = 5;
var damage = 10;
var PlayerHealth;
function Start ()
{
}
function Update ()
{
transform.LookAt(Player);
if(Vector3.Distance(transform.position,Player.position) >= $$anonymous$$inDist){
transform.position += transform.forward*$$anonymous$$oveSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position,Player.position) <= $$anonymous$$axDist)
{
PlayerHealth =-5;
}
}
}
Yes, that's very common for a beginner to "not know how to do it myself". But that doesn't change the fact that you are asking on the wrong forum. ;)
Looking at your code, you already know how to find out if something is in range of something else.
What you do in that case, and especially how you implement it, is up to your creativity and the limitations of Unity.
You can read up on in the documentation regarding the latter.
Vector3.Distance
is the classic approach when obstacles are not important.
Again, how you make the player's health, and how something affects it, is up to you.
I, for example, create a function void Attack()
and call it every time the enemy is within 1 unit of the player. Then I count down a specific time and only when that timer is at 0, another Attack() is possible.
Take this as a hint. Read up on how to do timers, how to use and call functions and that should do it for this case.
In the future, please consider all the information in the FAQ.