Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by nicoolsen10 · Jun 16, 2019 at 12:34 PM · enemytargetattack

Enemy attack target

How do i make so when the enemy comes within a attack radius its damaging the target there is 2 different targets there is a building and there is a player so how do i make it attack the target there is within attack radius this is the script

 void Update()
 {
     TargetFinder();

     if (currentTargetDistance < attackRadius)
         Attack();

     if (myTarget == null)
         currentTargetDistance = searchRadius;
     else
         Move(myTarget.transform);

     if (CurrentHealth < DeathHealth)
     {
         print("Enemy Has died!!!");
         Destroy(GroundEnemy);
     }

     print("Enemy took some damage");
 }

 void TargetFinder()
 {
     foreach (var x in Targets)
     {
         float distance = (transform.position - x.transform.position).magnitude;

         if (distance < currentTargetDistance)
         {
             myTarget = x;
             displayText.text = "My target: " + x + "\n Distance to Target: " + distance; //skal fjernes nå det er helt klart
             currentTargetDistance = distance;
         }
     }
 }

 void Move(Transform t)
 {
     transform.LookAt(t);
     transform.position += transform.forward * MoveSpeed * Time.deltaTime;
 }

 void Attack()
 {
     Targets.Remove(myTarget);
     Destroy(myTarget);
     myTarget = null;
     currentTargetDistance = searchRadius;
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by camdenlink7 · Jun 17, 2019 at 02:25 PM

Hi, one of the problems may be that your top two if statements do not have curly brackets. However, I would recommend a different approach to the whole thing. I would put a collider on your player (you can change the size to your liking) as the radius. Make sure it is checked as a trigger, and then use

  void OnTriggerEnter(Collision col)
  {
      if (col.gameObject.name == ("Enemy's name")
          {
              //Attack code
          }
  }

Put this code directly on your player and don't forget to put the collider on. I hope any of this helps!

Comment
Add comment · Show 3 · Share
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 nicoolsen10 · Jun 17, 2019 at 02:40 PM 0
Share

The thing is there is going to be slot og building and maybe 4 players so how do I do so it find the script of the specific taget and da age that one?

avatar image camdenlink7 · Jun 17, 2019 at 02:47 PM 0
Share

To find the script on an object, you can do

 private scriptName script;
 
 void Start()
  {
      script = objectScriptIsOn.GetComponent<scriptName>();
  }
 
 

avatar image nicoolsen10 camdenlink7 · Jun 17, 2019 at 05:12 PM 0
Share

what is the best replacement here for raycasthit?

    void DoAttack()
     {
         if (hit.collider.tag == "Player")
         {
             PlayerController eHealth = hit.collider.GetComponent<PlayerController>();
             PlayerController.instance.CurrentHealth -= Random.Range($$anonymous$$WeaponDamage, maxWeaponDamage + 1);
             print("Player took some damage");
         }
         myTarget = null;
         currentTargetDistance = searchRadius;
     }

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.

Follow this Question

Answers Answers and Comments

108 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 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 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 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

Need Help With My AI Script 1 Answer

Attack/Targeting Script Issue 1 Answer

Is this the most efficient way to attack the closest enemy? 0 Answers

Attacking enemy script problem 0 Answers

How to rig an enemy? 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