• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by SUPPEAR · Nov 15, 2015 at 03:27 AM · damageenemydamage

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"); 
             }
         }
     }
Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dinosaurs · Nov 16, 2015 at 03:03 AM 0
Share

You need to be more specific. Are you encountering errors? What aspect of this is giving you issues?

avatar image Le-Pampelmuse · Nov 16, 2015 at 03:19 AM 1
Share

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.

avatar image SUPPEAR Le-Pampelmuse · Nov 16, 2015 at 03:29 AM 0
Share

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;
                
     } 
     
     }
  }
avatar image Le-Pampelmuse SUPPEAR · Nov 16, 2015 at 04:17 AM 1
Share

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.

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make your player health subtract when enemies come into contact with you 3 Answers

How To Make Floating Combat Text In new UI System of 4.6 1 Answer

Restricting horizontal slash attack 0 Answers

Urgent help for Unity Game with deadline tomorrow 0 Answers

Damage Meter 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges